TodoRepository work
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,24 +6,33 @@ export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository
|
||||
|
||||
export class TodoRepositoryService {
|
||||
constructor(
|
||||
readonly ref: SubscriptionRef.SubscriptionRef<Todo[]>
|
||||
readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||
) {}
|
||||
|
||||
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<string> }) {
|
||||
return Ref.update(this.ref, todos =>
|
||||
Array.append(todos, new Todo({
|
||||
...todo,
|
||||
id: Option.some(""),
|
||||
}))
|
||||
)
|
||||
}
|
||||
|
||||
update(todo: Todo & { id: Option.Some<string> }) {
|
||||
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(),
|
||||
|
||||
Reference in New Issue
Block a user