TodoRepository work
This commit is contained in:
@@ -19,7 +19,7 @@ export module Identifiable {
|
|||||||
): boolean {
|
): boolean {
|
||||||
// Two elements can only be equal if they both have a defined ID
|
// Two elements can only be equal if they both have a defined ID
|
||||||
return Option.isSome(that.id) && Option.isSome(to.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
|
: false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,24 +6,33 @@ export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository
|
|||||||
|
|
||||||
export class TodoRepositoryService {
|
export class TodoRepositoryService {
|
||||||
constructor(
|
constructor(
|
||||||
readonly ref: SubscriptionRef.SubscriptionRef<Todo[]>
|
readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
get(id: string) {
|
get(id: string) {
|
||||||
return this.ref.get.pipe(
|
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) {
|
getIndex(id: string) {
|
||||||
return this.ref.get.pipe(
|
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) {
|
add(todo: Todo & { id: Option.None<string> }) {
|
||||||
return Effect.gen(this, function*() {
|
return Ref.update(this.ref, todos =>
|
||||||
const todoWithSameIDIndex = yield* this.getIndex(todo.id)
|
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 =>
|
Ref.update(repo, todos =>
|
||||||
Array.appendAll(todos, [
|
Array.appendAll(todos, [
|
||||||
new Todo({
|
new Todo({
|
||||||
id: "1",
|
id: Option.some("1"),
|
||||||
title: "A test todo",
|
title: "A test todo",
|
||||||
content: "Lorem ipsum",
|
content: "Lorem ipsum",
|
||||||
due: Option.none(),
|
due: Option.none(),
|
||||||
|
|||||||
Reference in New Issue
Block a user