Working TodoRepository
This commit is contained in:
@@ -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"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
))
|
))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -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(
|
||||||
|
|||||||
Reference in New Issue
Block a user