Skip to content

Instantly share code, notes, and snippets.

@patroza
Last active May 24, 2021 08:52
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?

Revisions

  1. patroza revised this gist Mar 23, 2020. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions todos.ts
    @@ -56,7 +56,7 @@ const getCompletedTodos = (getTodos: ReturnType<typeof getTodosApi>) =>
    const completeTodo = (getTodo: ReturnType<typeof getTodoApi>, saveTodo: ReturnType<typeof saveTodoApi>) =>
    async function (todoId: number): Promise<void> {
    const todo = await getTodo(todoId)
    const updatedTodo = complete(todo)
    const updatedTodo = complete(todo, new Date())
    await saveTodo(updatedTodo)
    }

    @@ -74,13 +74,13 @@ type Todo = {
    readonly updatedAt: Date
    }

    const complete = (todo: Todo) => {
    const complete = (todo: Todo, currentDate: Date) => {
    if (todo.isCompleted) {
    throw new Error("already completed")
    }
    return {
    ...todo,
    isCompleted: true,
    updatedAt: new Date(),
    updatedAt: currentDate,
    }
    }
  2. patroza revised this gist Mar 22, 2020. No changes.
  3. patroza revised this gist Mar 18, 2020. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions todos.ts
    @@ -68,10 +68,10 @@ type TodoView = {

    // 1. Domain
    type Todo = {
    id: number
    title: string
    isCompleted: boolean
    updatedAt: Date
    readonly id: number
    readonly title: string
    readonly isCompleted: boolean
    readonly updatedAt: Date
    }

    const complete = (todo: Todo) => {
  4. patroza revised this gist Mar 18, 2020. 1 changed file with 11 additions and 7 deletions.
    18 changes: 11 additions & 7 deletions todos.ts
    @@ -24,18 +24,22 @@ const start = () => {
    //start()

    // 3. Infra
    let todoStubs = [
    { id: 1, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)},
    { id: 2, title: "Test 2", isCompleted: true, updatedAt: new Date(2020, 1, 1)},
    ]

    const getTodosApi = (url: string) => async function (userId: number): Promise<Todo[]> {
    return [
    { id: 1, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)},
    { id: 2, title: "Test 2", isCompleted: true, updatedAt: new Date(2020, 1, 1)},
    ]
    return todoStubs
    }

    const getTodoApi = (url: string) => async function (todoId: number): Promise<Todo> {
    return { id: todoId, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)}
    const getTodoApi = (url: string) => async function (todoId: number): Promise<Todo | undefined> {
    return todoStubs.find(x => x.id === todoId)
    }

    const saveTodoApi = (url: string) => async function (todo: Todo): Promise<void> {}
    const saveTodoApi = (url: string) => async function (todo: Todo): Promise<void> {
    todoStubs = todoStubs.filter(x => x.id === todo.id).concat([todo])
    }


    // 2. Application
  5. patroza revised this gist Mar 18, 2020. 1 changed file with 9 additions and 6 deletions.
    15 changes: 9 additions & 6 deletions todos.ts
    @@ -1,4 +1,4 @@
    // 4. Presentation/Root
    // 3. Presentation/Root
    const start = () => {
    const getCompletedTodosRequestHandler = async (ctx) => {
    const result = await getCompletedTodosResolved(ctx.params.id)
    @@ -23,7 +23,7 @@ const start = () => {
    }
    //start()

    // 4. Infra
    // 3. Infra
    const getTodosApi = (url: string) => async function (userId: number): Promise<Todo[]> {
    return [
    { id: 1, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)},
    @@ -52,8 +52,8 @@ const getCompletedTodos = (getTodos: ReturnType<typeof getTodosApi>) =>
    const completeTodo = (getTodo: ReturnType<typeof getTodoApi>, saveTodo: ReturnType<typeof saveTodoApi>) =>
    async function (todoId: number): Promise<void> {
    const todo = await getTodo(todoId)
    complete(todo)
    await saveTodo(todo)
    const updatedTodo = complete(todo)
    await saveTodo(updatedTodo)
    }

    type TodoView = {
    @@ -74,6 +74,9 @@ const complete = (todo: Todo) => {
    if (todo.isCompleted) {
    throw new Error("already completed")
    }
    todo.isCompleted = true
    todo.updatedAt = new Date()
    return {
    ...todo,
    isCompleted: true,
    updatedAt: new Date(),
    }
    }
  6. patroza created this gist Mar 18, 2020.
    79 changes: 79 additions & 0 deletions todos.ts
    @@ -0,0 +1,79 @@
    // 4. Presentation/Root
    const start = () => {
    const getCompletedTodosRequestHandler = async (ctx) => {
    const result = await getCompletedTodosResolved(ctx.params.id)
    ctx.body = JSON.stringify(result)
    }

    const completeTodoRequestHandler = async (ctx) => {
    await completeTodoResolved(ctx.params.id)
    ctx.status = 204
    }

    const url = "some-url"
    const getTodoApiResolved = getTodoApi(url)
    const getTodosApiResolved = getTodosApi(url)
    const saveTodoResolved = saveTodoApi(url)

    const getCompletedTodosResolved = getCompletedTodos(getTodosApiResolved)
    const completeTodoResolved = completeTodo(getTodoApiResolved, saveTodoResolved)
    app.get("/users/:id/todos", getCompletedTodosRequestHandler)
    app.post("/todos/:id/complete", completeTodoRequestHandler)
    app.listen(3000)
    }
    //start()

    // 4. Infra
    const getTodosApi = (url: string) => async function (userId: number): Promise<Todo[]> {
    return [
    { id: 1, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)},
    { id: 2, title: "Test 2", isCompleted: true, updatedAt: new Date(2020, 1, 1)},
    ]
    }

    const getTodoApi = (url: string) => async function (todoId: number): Promise<Todo> {
    return { id: todoId, title: "Test", isCompleted: false, updatedAt: new Date(2020, 1, 1)}
    }

    const saveTodoApi = (url: string) => async function (todo: Todo): Promise<void> {}


    // 2. Application
    const getCompletedTodos = (getTodos: ReturnType<typeof getTodosApi>) =>
    async function (userId: number): Promise<TodoView> {
    const allTodos = await getTodos(userId)
    const completedTodos = allTodos.filter(x => x.isCompleted)
    return {
    items: completedTodos,
    userId,
    }
    }

    const completeTodo = (getTodo: ReturnType<typeof getTodoApi>, saveTodo: ReturnType<typeof saveTodoApi>) =>
    async function (todoId: number): Promise<void> {
    const todo = await getTodo(todoId)
    complete(todo)
    await saveTodo(todo)
    }

    type TodoView = {
    items: Todo[]
    userId: number
    }


    // 1. Domain
    type Todo = {
    id: number
    title: string
    isCompleted: boolean
    updatedAt: Date
    }

    const complete = (todo: Todo) => {
    if (todo.isCompleted) {
    throw new Error("already completed")
    }
    todo.isCompleted = true
    todo.updatedAt = new Date()
    }