diff --git a/packages/common/src/traits/Identifiable.trait.ts b/packages/common/src/traits/Identifiable.trait.ts index 34fc704..ff7aeac 100644 --- a/packages/common/src/traits/Identifiable.trait.ts +++ b/packages/common/src/traits/Identifiable.trait.ts @@ -19,7 +19,7 @@ export module Identifiable { ): boolean { // Two elements can only be equal if they both have a defined ID return Option.isSome(that.id) && Option.isSome(to.id) - ? Equal.equals(that.id, to.id) + ? Equal.equals(that.id.value, to.id.value) : false } } diff --git a/packages/server/src/TodoRepository.ts b/packages/server/src/TodoRepository.ts index c532ac3..3411103 100644 --- a/packages/server/src/TodoRepository.ts +++ b/packages/server/src/TodoRepository.ts @@ -6,24 +6,33 @@ export class TodoRepository extends Context.Tag("TodoRepository") + readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some })[]> ) {} get(id: string) { return this.ref.get.pipe( - Effect.map(Array.findFirst(todo => todo.id === id)) + Effect.map(Array.findFirst(todo => todo.id.value === id)) ) } getIndex(id: string) { return this.ref.get.pipe( - Effect.map(Array.findFirstIndex(todo => todo.id === id)) + Effect.map(Array.findFirstIndex(todo => todo.id.value === id)) ) } - update(todo: Todo) { - return Effect.gen(this, function*() { - const todoWithSameIDIndex = yield* this.getIndex(todo.id) + add(todo: Todo & { id: Option.None }) { + return Ref.update(this.ref, todos => + Array.append(todos, new Todo({ + ...todo, + id: Option.some(""), + })) + ) + } + update(todo: Todo & { id: Option.Some }) { + return Effect.gen(this, function*() { + const index = yield* yield* this.getIndex(todo.id.value) + yield* Ref.update(this.ref, Array.replace(index, todo)) }) } } @@ -33,7 +42,7 @@ export const createDefaultTodos = TodoRepository.pipe(Effect.flatMap(repo => Ref.update(repo, todos => Array.appendAll(todos, [ new Todo({ - id: "1", + id: Option.some("1"), title: "A test todo", content: "Lorem ipsum", due: Option.none(),