diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index 5584734..83e655e 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -18,6 +18,8 @@ module Utils = name = todo.Name } let inline parseJson (input: byte array) = input |> Encoding.UTF8.GetString |> Json.parse |> Json.deserialize + + let respondWithTodo = todoToRecord >> Json.serialize >> Json.format >> OK module App = open Utils @@ -32,7 +34,7 @@ module App = | None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND let todoById = - withTodoById (fun (inner, _, _) -> inner |> todoToRecord |> Json.serialize |> Json.format |> OK) + withTodoById (fun (inner, _, _) -> respondWithTodo inner) let updateTodo = withTodoById (fun (todo, dbContext, id) -> @@ -40,16 +42,8 @@ module App = let body: Types.TodoDetails = parseJson ctx.request.rawForm do! Queries.updateTodoById todo body dbContext - - let newBody: Types.Todo = { - name = body.name - description = body.description - id = id - } - - let writeBody = newBody |> Json.serialize |> Json.format |> OK - - return! writeBody ctx + + return! respondWithTodo todo ctx }) let patchTodo = withTodoById (fun (todo, dbContext, id) -> @@ -60,15 +54,7 @@ module App = do! Queries.patchTodoById todo body dbContext - let newBody: Types.Todo = { - name = Option.defaultValue originalTodo.name body.name - description = Option.defaultValue originalTodo.description body.description - id = id - } - - let writeBody = newBody |> Json.serialize |> Json.format |> OK - - return! writeBody ctx + return! respondWithTodo todo ctx }) let mainWebPart: WebPart = choose [ diff --git a/fsharp/todolist-api/src/Db.fs b/fsharp/todolist-api/src/Db.fs index e541aa7..c8d50c4 100644 --- a/fsharp/todolist-api/src/Db.fs +++ b/fsharp/todolist-api/src/Db.fs @@ -67,7 +67,6 @@ module Types = module Queries = - open FSharpPlus.Builders open Context open Types @@ -93,4 +92,8 @@ module Queries = if Option.orElse details.name details.description |> Option.isSome then ctx.SubmitUpdatesAsync() else Async.result() - \ No newline at end of file + + let deleteTodoById (todo: DbTodo) (ctx: DbContext) = + todo.Delete() + + ctx.SubmitUpdatesAsync()