TodoRepository work
This commit is contained in:
@@ -10,7 +10,7 @@ export class Todo
|
|||||||
_tag: Tag("Todo"),
|
_tag: Tag("Todo"),
|
||||||
|
|
||||||
id: S.OptionFromSelf(S.String),
|
id: S.OptionFromSelf(S.String),
|
||||||
title: S.String,
|
order: S.Number,
|
||||||
content: S.String,
|
content: S.String,
|
||||||
|
|
||||||
due: S.OptionFromSelf(S.DateFromSelf),
|
due: S.OptionFromSelf(S.DateFromSelf),
|
||||||
|
|||||||
@@ -15,10 +15,12 @@ export module TodoRepository {
|
|||||||
|
|
||||||
|
|
||||||
export class TodoRepositoryService {
|
export class TodoRepositoryService {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
readonly todos: SubscriptionRef.SubscriptionRef<(Todo & { id: Option.Some<string> })[]>
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
|
||||||
getByID(id: string) {
|
getByID(id: string) {
|
||||||
return this.todos.get.pipe(
|
return this.todos.get.pipe(
|
||||||
Effect.map(Array.findFirst(todo => Equal.equals(todo.id.value, id)))
|
Effect.map(Array.findFirst(todo => Equal.equals(todo.id.value, id)))
|
||||||
@@ -31,6 +33,7 @@ export class TodoRepositoryService {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
add(todo: Todo) {
|
add(todo: Todo) {
|
||||||
return Effect.gen(this, function*() {
|
return Effect.gen(this, function*() {
|
||||||
if (Option.isSome(todo.id))
|
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 }> {}
|
export class TodoHasID extends Data.TaggedError("TodoHasID")<{ todo: Todo }> {}
|
||||||
@@ -80,8 +89,8 @@ export const createDefaultTodos = Effect.gen(function*() {
|
|||||||
|
|
||||||
yield* todos.add(new Todo({
|
yield* todos.add(new Todo({
|
||||||
id: Option.none(),
|
id: Option.none(),
|
||||||
title: "A test todo",
|
order: 1,
|
||||||
content: "Lorem ipsum",
|
content: "Sort the socks",
|
||||||
due: Option.none(),
|
due: Option.none(),
|
||||||
completed: false,
|
completed: false,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
@@ -90,8 +99,18 @@ export const createDefaultTodos = Effect.gen(function*() {
|
|||||||
|
|
||||||
yield* todos.add(new Todo({
|
yield* todos.add(new Todo({
|
||||||
id: Option.none(),
|
id: Option.none(),
|
||||||
title: "Another one",
|
order: 2,
|
||||||
content: "Lorem ipsum",
|
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(),
|
due: Option.none(),
|
||||||
completed: false,
|
completed: false,
|
||||||
createdAt: new Date(),
|
createdAt: new Date(),
|
||||||
|
|||||||
Reference in New Issue
Block a user