Skip to content

Instantly share code, notes, and snippets.

@scionwest
Created December 13, 2020 22:42
Show Gist options
  • Save scionwest/0329b0f615a5873ebb85dd0d92ef2563 to your computer and use it in GitHub Desktop.
Save scionwest/0329b0f615a5873ebb85dd0d92ef2563 to your computer and use it in GitHub Desktop.
import 'mocha';
import { expect } from 'chai';
import { PostApp } from '../../src/apps/post-app';
import { TodoItem } from '../../src/models/todo-item';
import { TodoRepository } from '../../src/models/todo-repository';
import { ApiGatewayResponse } from '../../src/models/apigateway/apigateway-response';
import { ApiGatewayEventMock } from '../models/apigateway-event-mock';
describe('PostApp instance', () => {
describe('constructor', () => {
it('table is assigned', async () => {
const tableName = 'MY_TABLE';
let invoked = false;
const putItem = function(todoItem: TodoItem, table: string): Promise<void> {
return new Promise<void>((resolve, reject) => {
invoked = true;
resolve();
});
};
const event = new ApiGatewayEventMock();
const repo: TodoRepository = { putTodo: putItem };
const app = new PostApp(tableName, repo);
const response: ApiGatewayResponse = await app.run(event);
expect(invoked).to.equal(true);
});
});
});
import { ApiGatewayEvent } from '../models/apigateway/apigateway-event';
import { ApiGatewayResponse } from '../models/apigateway/apigateway-response';
import { TodoRepository } from '../models/todo-repository';
import { TodoItem } from '../models/todo-item';
export class PostApp {
table: string;
repository: TodoRepository;
constructor(table: string, repository: TodoRepository) {
this.table = table;
this.repository = repository;
}
async run(event: ApiGatewayEvent): Promise<ApiGatewayResponse> {
try {
const todo: TodoItem = JSON.parse(event.body);
if (!todo.title) {
return { statusCode: 422 };
} else if (!todo.isComplete) {
todo.isComplete = false;
} else if (!todo.id) {
return { statusCode: 422 };
}
await this.repository.putTodo(todo, this.table);
return { statusCode: 201, body: todo };
} catch(err) {
console.log(err);
return { statusCode: 500 };
}
}
}
import { ApiGatewayEvent } from '../models/apigateway/apigateway-event';
import { ApiGatewayResponse } from '../models/apigateway/apigateway-response';
import { PostApp } from '../apps/post-app';
import { TodoDynamoClientRepository } from '../models/todo-dynamoclient-repository';
export const handler = async (event: ApiGatewayEvent): Promise<ApiGatewayResponse> => {
if (!process.env['SAMPLE_TABLE']) {
return { statusCode: 500 };
}
const table: string = process.env['SAMPLE_TABLE'];
const repository = new TodoDynamoClientRepository();
const app = new PostApp(table, repository);
return await app.run(event);
};
import { DynamoDB } from 'aws-sdk';
import { TodoItem } from './todo-item';
import { TodoRepository } from './todo-repository';
export class TodoDynamoClientRepository implements TodoRepository {
docClient: DynamoDB.DocumentClient;
constructor() {
this.docClient = new DynamoDB.DocumentClient();
}
async putTodo(todoItem: TodoItem, table: string): Promise<void> {
const params: DynamoDB.DocumentClient.PutItemInput = {
TableName: table,
Item: todoItem
};
await this.docClient.put(params).promise();
return;
}
}
import { TodoItem } from './todo-item';
export interface TodoRepository {
putTodo(todoItem: TodoItem, table: string): Promise<void>;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment