Working TodoRepository

This commit is contained in:
Julien Valverdé
2024-06-28 23:40:19 +02:00
parent 5388bc139d
commit f931ea2a1c
4 changed files with 12 additions and 8 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -9,5 +9,8 @@
"@thilawyn/thilalib": "^0.1.4", "@thilawyn/thilalib": "^0.1.4",
"@todo-tests/common": "workspace:*", "@todo-tests/common": "workspace:*",
"effect": "^3.4.5" "effect": "^3.4.5"
},
"devDependencies": {
"bun-types": "^1.1.17"
} }
} }

View File

@@ -1,5 +1,6 @@
import { Todo } from "@todo-tests/common/data" import { Todo } from "@todo-tests/common/data"
import { Array, Context, Data, Effect, Layer, Option, Ref, SubscriptionRef } from "effect" import { Array, Context, Data, Effect, Layer, Option, Ref, SubscriptionRef } from "effect"
import crypto from "node:crypto"
export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, TodoRepositoryService>() {} export class TodoRepository extends Context.Tag("TodoRepository")<TodoRepository, TodoRepositoryService>() {}
@@ -13,17 +14,17 @@ export const TodoRepositoryLive = Layer.effect(TodoRepository,
export class TodoRepositoryService { export class TodoRepositoryService {
constructor( constructor(
readonly ref: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]> readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
) {} ) {}
get(id: string) { get(id: string) {
return this.ref.get.pipe( return this.todos.get.pipe(
Effect.map(Array.findFirst(todo => todo.id.value === id)) Effect.map(Array.findFirst(todo => todo.id.value === id))
) )
} }
getIndex(id: string) { getIndex(id: string) {
return this.ref.get.pipe( return this.todos.get.pipe(
Effect.map(Array.findFirstIndex(todo => todo.id.value === id)) Effect.map(Array.findFirstIndex(todo => todo.id.value === id))
) )
} }
@@ -33,10 +34,10 @@ export class TodoRepositoryService {
if (Option.isSome(todo.id)) if (Option.isSome(todo.id))
return yield* Effect.fail(new TodoHasID({ todo })) return yield* Effect.fail(new TodoHasID({ todo }))
yield* Ref.update(this.ref, todos => yield* Ref.update(this.todos, todos =>
Array.append(todos, new Todo({ Array.append(todos, new Todo({
...todo, ...todo,
id: Option.some(""), id: Option.some(crypto.randomUUID()),
}) as Todo & { id: Option.Some<string> }) }) as Todo & { id: Option.Some<string> })
) )
}) })
@@ -47,7 +48,7 @@ export class TodoRepositoryService {
if (Option.isNone(todo.id)) if (Option.isNone(todo.id))
return yield* Effect.fail(new TodoHasNoID({ todo })) return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.ref, Array.replace( yield* Ref.update(this.todos, Array.replace(
yield* yield* this.getIndex(todo.id.value), yield* yield* this.getIndex(todo.id.value),
todo as Todo & { id: Option.Some<string> }, todo as Todo & { id: Option.Some<string> },
)) ))
@@ -59,7 +60,7 @@ export class TodoRepositoryService {
if (Option.isNone(todo.id)) if (Option.isNone(todo.id))
return yield* Effect.fail(new TodoHasNoID({ todo })) return yield* Effect.fail(new TodoHasNoID({ todo }))
yield* Ref.update(this.ref, Array.remove( yield* Ref.update(this.todos, Array.remove(
yield* yield* this.getIndex(todo.id.value) yield* yield* this.getIndex(todo.id.value)
)) ))
}) })

View File

@@ -7,7 +7,7 @@ const main = Effect.gen(function*() {
yield* createDefaultTodos yield* createDefaultTodos
const todos = yield* TodoRepository const todos = yield* TodoRepository
console.log(yield* todos.ref.get) console.log(yield* todos.todos.get)
}) })
const runnableMain = main.pipe( const runnableMain = main.pipe(