Work
This commit is contained in:
@@ -5,11 +5,13 @@ import crypto from "node:crypto"
|
||||
|
||||
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, TodoRepositoryService>() {}
|
||||
|
||||
export const TodoRepositoryLive = Layer.effect(TodoRepository,
|
||||
SubscriptionRef.make(Array.empty<Todo & { id: Option.Some<string> }>()).pipe(
|
||||
Effect.map(ref => new TodoRepositoryService(ref))
|
||||
export module TodoRepository {
|
||||
export const Live = Layer.effect(TodoRepository,
|
||||
SubscriptionRef.make(Array.empty<Todo & { id: Option.Some<string> }>()).pipe(
|
||||
Effect.map(ref => new TodoRepositoryService(ref))
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
export class TodoRepositoryService {
|
||||
@@ -17,13 +19,13 @@ export class TodoRepositoryService {
|
||||
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||
) {}
|
||||
|
||||
get(id: string) {
|
||||
getByID(id: string) {
|
||||
return this.todos.get.pipe(
|
||||
Effect.map(Array.findFirst(todo => Equal.equals(todo.id.value, id)))
|
||||
)
|
||||
}
|
||||
|
||||
getIndex(id: string) {
|
||||
getIndexByID(id: string) {
|
||||
return this.todos.get.pipe(
|
||||
Effect.map(Array.findFirstIndex(todo => Equal.equals(todo.id.value, id)))
|
||||
)
|
||||
@@ -34,12 +36,14 @@ export class TodoRepositoryService {
|
||||
if (Option.isSome(todo.id))
|
||||
return yield* Effect.fail(new TodoHasID({ todo }))
|
||||
|
||||
yield* Ref.update(this.todos, todos =>
|
||||
Array.append(todos, new Todo({
|
||||
...todo,
|
||||
id: Option.some(crypto.randomUUID()),
|
||||
}) as Todo & { id: Option.Some<string> })
|
||||
)
|
||||
const id: string = crypto.randomUUID()
|
||||
|
||||
yield* Ref.update(this.todos, Array.append(new Todo({
|
||||
...todo,
|
||||
id: Option.some(id),
|
||||
}) as Todo & { id: Option.Some<string> }))
|
||||
|
||||
return id
|
||||
})
|
||||
}
|
||||
|
||||
@@ -49,7 +53,7 @@ export class TodoRepositoryService {
|
||||
return yield* Effect.fail(new TodoHasNoID({ todo }))
|
||||
|
||||
yield* Ref.update(this.todos, Array.replace(
|
||||
yield* yield* this.getIndex(todo.id.value),
|
||||
yield* yield* this.getIndexByID(todo.id.value),
|
||||
todo as Todo & { id: Option.Some<string> },
|
||||
))
|
||||
})
|
||||
@@ -61,7 +65,7 @@ export class TodoRepositoryService {
|
||||
return yield* Effect.fail(new TodoHasNoID({ todo }))
|
||||
|
||||
yield* Ref.update(this.todos, Array.remove(
|
||||
yield* yield* this.getIndex(todo.id.value)
|
||||
yield* yield* this.getIndexByID(todo.id.value)
|
||||
))
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user