1
Fork 0

fsharp(todolist-api): refactor: removed duplicate json formating

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2020-01-02 17:32:16 +02:00 committed by prescientmoon
parent f3a4b3c957
commit 83cf14c2d0
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
2 changed files with 11 additions and 22 deletions

View file

@ -19,6 +19,8 @@ module Utils =
let inline parseJson (input: byte array) = input |> Encoding.UTF8.GetString |> Json.parse |> Json.deserialize let inline parseJson (input: byte array) = input |> Encoding.UTF8.GetString |> Json.parse |> Json.deserialize
let respondWithTodo = todoToRecord >> Json.serialize >> Json.format >> OK
module App = module App =
open Utils open Utils
open Db open Db
@ -32,7 +34,7 @@ module App =
| None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND | None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND
let todoById = let todoById =
withTodoById (fun (inner, _, _) -> inner |> todoToRecord |> Json.serialize |> Json.format |> OK) withTodoById (fun (inner, _, _) -> respondWithTodo inner)
let updateTodo = let updateTodo =
withTodoById (fun (todo, dbContext, id) -> withTodoById (fun (todo, dbContext, id) ->
@ -41,15 +43,7 @@ module App =
do! Queries.updateTodoById todo body dbContext do! Queries.updateTodoById todo body dbContext
let newBody: Types.Todo = { return! respondWithTodo todo ctx
name = body.name
description = body.description
id = id
}
let writeBody = newBody |> Json.serialize |> Json.format |> OK
return! writeBody ctx
}) })
let patchTodo = withTodoById (fun (todo, dbContext, id) -> let patchTodo = withTodoById (fun (todo, dbContext, id) ->
@ -60,15 +54,7 @@ module App =
do! Queries.patchTodoById todo body dbContext do! Queries.patchTodoById todo body dbContext
let newBody: Types.Todo = { return! respondWithTodo todo ctx
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
}) })
let mainWebPart: WebPart = choose [ let mainWebPart: WebPart = choose [

View file

@ -67,7 +67,6 @@ module Types =
module Queries = module Queries =
open FSharpPlus.Builders
open Context open Context
open Types open Types
@ -94,3 +93,7 @@ module Queries =
then ctx.SubmitUpdatesAsync() then ctx.SubmitUpdatesAsync()
else Async.result() else Async.result()
let deleteTodoById (todo: DbTodo) (ctx: DbContext) =
todo.Delete()
ctx.SubmitUpdatesAsync()