TodoRepository work
This commit is contained in:
@@ -10,7 +10,7 @@ export class Todo
|
||||
_tag: Tag("Todo"),
|
||||
|
||||
id: S.OptionFromSelf(S.String),
|
||||
title: S.String,
|
||||
order: S.Number,
|
||||
content: S.String,
|
||||
|
||||
due: S.OptionFromSelf(S.DateFromSelf),
|
||||
|
||||
@@ -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(),
|
||||
|
||||
Reference in New Issue
Block a user