TodoRepository work

This commit is contained in:
Julien Valverdé
2024-07-08 21:38:44 +02:00
parent 8f8c696e3a
commit e88a6f7b1c
3 changed files with 36 additions and 26 deletions

View File

@@ -1,5 +1,5 @@
import { Todo } from "@todo-tests/common/data"
import { Array, Context, Data, Effect, Equal, Layer, Option, Order, Ref, SubscriptionRef } from "effect"
import { Todo, type IdentifiedTodo } from "@todo-tests/common/data"
import { Array, Context, Data, Effect, Equal, Layer, Option, Order, Ref, SubscriptionRef, flow } from "effect"
import crypto from "node:crypto"
@@ -7,7 +7,7 @@ export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository
export module TodoRepository {
export const Live = Layer.effect(TodoRepository,
SubscriptionRef.make(Array.empty<Todo & { id: Option.Some<string> }>()).pipe(
SubscriptionRef.make(Array.empty<IdentifiedTodo>()).pipe(
Effect.map(ref => new TodoRepositoryService(ref))
)
)
@@ -17,7 +17,7 @@ export module TodoRepository {
export class TodoRepositoryService {
constructor(
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
readonly todos: SubscriptionRef.SubscriptionRef<IdentifiedTodo[]>
) {}
@@ -41,10 +41,15 @@ export class TodoRepositoryService {
const id: string = crypto.randomUUID()
yield* Ref.update(this.todos, Array.append(new Todo({
...todo,
id: Option.some(id),
}, { disableValidation: true }) as Todo & { id: Option.Some<string> }))
yield* Ref.update(this.todos, flow(
Array.append(new Todo({
...todo,
id: Option.some(id),
}) as IdentifiedTodo),
sortTodos,
updateTodosOrder,
))
return id
})
@@ -55,9 +60,14 @@ export class TodoRepositoryService {
if (Option.isNone(todo.id))
return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.todos, Array.replace(
yield* yield* this.getIndexByID(todo.id.value),
todo as Todo & { id: Option.Some<string> },
yield* Ref.update(this.todos, flow(
Array.replace(
yield* yield* this.getIndexByID(todo.id.value),
todo as IdentifiedTodo,
),
sortTodos,
updateTodosOrder,
))
})
}
@@ -67,27 +77,24 @@ export class TodoRepositoryService {
if (Option.isNone(todo.id))
return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.todos, Array.remove(
yield* yield* this.getIndexByID(todo.id.value)
yield* Ref.update(this.todos, flow(
Array.remove(
yield* yield* this.getIndexByID(todo.id.value)
),
updateTodosOrder,
))
})
}
sort() {
return Ref.update(this.todos, Array.sort())
}
}
const sortTodos = Array.sortBy<(Todo & { id: Option.Some<string> })[]>(
const sortTodos = Array.sortBy<IdentifiedTodo[]>(
Order.mapInput(Order.number, todo => todo.order)
)
const updateTodoOrders = Array.map<
(Todo & { id: Option.Some<string> })[],
(Todo & { id: Option.Some<string> })[]
>()
const updateTodosOrder = Array.map<IdentifiedTodo[], IdentifiedTodo>((todo, order) =>
new Todo({ ...todo, order }, { disableValidation: true }) as IdentifiedTodo
)
export class TodoHasID extends Data.TaggedError("TodoHasID")<{ todo: Todo }> {}
export class TodoHasNoID extends Data.TaggedError("TodoHasNoID")<{ todo: Todo }> {}
@@ -98,7 +105,7 @@ export const createDefaultTodos = Effect.gen(function*() {
yield* todos.add(new Todo({
id: Option.none(),
order: 1,
order: 0,
content: "Sort the socks",
due: Option.none(),
completed: false,
@@ -108,7 +115,7 @@ export const createDefaultTodos = Effect.gen(function*() {
yield* todos.add(new Todo({
id: Option.none(),
order: 2,
order: 1,
content: "Sort the dog",
due: Option.none(),
completed: false,