TodoRepository work

This commit is contained in:
Julien Valverdé
2024-07-08 05:38:00 +02:00
parent c609de341a
commit 5f85462645
2 changed files with 24 additions and 5 deletions

View File

@@ -15,10 +15,12 @@ export module TodoRepository {
export class TodoRepositoryService {
constructor(
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
) {}
getByID(id: string) {
return this.todos.get.pipe(
Effect.map(Array.findFirst(todo => Equal.equals(todo.id.value, id)))
@@ -31,6 +33,7 @@ export class TodoRepositoryService {
)
}
add(todo: Todo) {
return Effect.gen(this, function*() {
if (Option.isSome(todo.id))
@@ -69,6 +72,12 @@ export class TodoRepositoryService {
))
})
}
sort() {
return Ref.update(this.todos, Array.sort())
}
}
export class TodoHasID extends Data.TaggedError("TodoHasID")<{ todo: Todo }> {}
@@ -80,8 +89,8 @@ export const createDefaultTodos = Effect.gen(function*() {
yield* todos.add(new Todo({
id: Option.none(),
title: "A test todo",
content: "Lorem ipsum",
order: 1,
content: "Sort the socks",
due: Option.none(),
completed: false,
createdAt: new Date(),
@@ -90,8 +99,18 @@ export const createDefaultTodos = Effect.gen(function*() {
yield* todos.add(new Todo({
id: Option.none(),
title: "Another one",
content: "Lorem ipsum",
order: 2,
content: "Sort the dog",
due: Option.none(),
completed: false,
createdAt: new Date(),
updatedAt: new Date(),
}))
yield* todos.add(new Todo({
id: Option.none(),
order: 2,
content: "Sort the car",
due: Option.none(),
completed: false,
createdAt: new Date(),