Skip to content

Commit

Permalink
fix: make lists unique via listName
Browse files Browse the repository at this point in the history
  • Loading branch information
alexx666 committed Aug 2, 2022
1 parent 06b6b76 commit ae5a3cf
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export default class DynamoListRepository implements ListRepository {

constructor(private readonly ddb: DynamoDB.DocumentClient = new DynamoDB.DocumentClient()) { }

public async findByName(id: string): Promise<List> {
public async findByName(id: string): Promise<List | undefined> {

const { Items: events } = await this.ddb.query({
TableName: String(process.env.DYNAMO_TABLE_NAME),
Expand All @@ -17,7 +17,7 @@ export default class DynamoListRepository implements ListRepository {
ExpressionAttributeNames: { "#stream": "stream" }
}).promise();

if (!events?.length) throw new Error("[DynamoListRepository] Error: List does not exist!");
if (!events?.length) return;

return List.buildFromStream(events as Event[]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ describe("[InMemoryTodoGateway] Test Cases", () => {

it("should return matching list", async () => {
const result = await inMemTodoGW.findByName(listName)
expect(result).toBeDefined()
expect(result).toEqual(list)
expect.assertions(1)
expect.assertions(2)
})

})
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ export default class InMemoryTodoRepository implements ListRepository {

constructor(private readonly events: Events = new Events()) { }

public async findByName(id: string): Promise<List> {
public async findByName(id: string): Promise<List | undefined> {
const listEvents = this.events.filter((event) => event.details.id === id);

if (!listEvents.length) throw new Error("[InMemoryTodoRepository] Error: List does not exist!");
if (!listEvents.length) return;

return List.buildFromStream(listEvents);
}
Expand Down
8 changes: 7 additions & 1 deletion libs/todos/src/commands/create-list/create-list.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { List } from "../../entities/";
import { ListCreated } from "../../events";
import { EventPublisher, Providers } from "../../ports";
import { EventPublisher, ListRepository, Providers } from "../../ports";

export interface CreateList {
execute(request: CreateListRequest): Promise<void>;
Expand All @@ -16,15 +16,21 @@ export interface CreateListRequest {
export class CreateListImpl implements CreateList {

private publisher: EventPublisher;
private repository: ListRepository;

constructor(config: Providers) {
this.publisher = config.publisher;
this.repository = config.repository;
}

public async execute(request: CreateListRequest): Promise<void> {

const { listName: name, allowDuplicates, maxTodos, allowExpired } = request;

const existingList = await this.repository.findByName(name);

if (existingList) throw new Error("[CreateList] Error: List aleady exist!");

const newList = new List({ name, allowDuplicates, allowExpired, maxTodos });

await this.publisher.publish(new ListCreated(newList));
Expand Down
2 changes: 2 additions & 0 deletions libs/todos/src/commands/create-todo/create-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ export class CreateTodoImpl implements CreateTodo {

const list = await this.repository.findByName(listName);

if (!list) throw new Error("[CreateTodo] Error: List does not exist!");

list.add(todo);

await this.publisher.publish(new TodoAdded(todo));
Expand Down
2 changes: 2 additions & 0 deletions libs/todos/src/commands/delete-todo/delete-todo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export class DeleteTodoImpl implements DeleteTodo {

const list = await this.repository.findByName(listName);

if (!list) throw new Error("[DeleteTodo] Error: List does not exist!");

const deletedTodo = list.remove(id)

await this.publisher.publish(new TodoRemoved(deletedTodo));
Expand Down
2 changes: 1 addition & 1 deletion libs/todos/src/ports/list.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { List } from "../entities";

export default interface ListRepository {
findByName(name: string): Promise<List>;
findByName(name: string): Promise<List | undefined>;
}

0 comments on commit ae5a3cf

Please sign in to comment.