From 3023f6c41062fca26d0581ad61214c0e7cdc98e7 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 2 Jan 2020 16:51:22 +0200 Subject: [PATCH] fsharp(todolist-api): refactor: refactored to use Chiron for json stuff Signed-off-by: prescientmoon --- fsharp/todolist-api/paket.dependencies | 3 ++- fsharp/todolist-api/paket.lock | 9 +++++++ fsharp/todolist-api/paket.references | 3 ++- fsharp/todolist-api/src/App.fs | 19 ++++++------- fsharp/todolist-api/src/Db.fs | 37 +++++++++++++++++++------- 5 files changed, 50 insertions(+), 21 deletions(-) diff --git a/fsharp/todolist-api/paket.dependencies b/fsharp/todolist-api/paket.dependencies index 2a81ced..6c07b47 100644 --- a/fsharp/todolist-api/paket.dependencies +++ b/fsharp/todolist-api/paket.dependencies @@ -7,4 +7,5 @@ nuget FSharp.Core nuget Suave nuget SQLProvider nuget Npgsql -nuget FSharpPlus 1.1.0-CI00272 \ No newline at end of file +nuget FSharpPlus 1.1.0-CI00272 +nuget Chiron \ No newline at end of file diff --git a/fsharp/todolist-api/paket.lock b/fsharp/todolist-api/paket.lock index 24a861e..654a297 100644 --- a/fsharp/todolist-api/paket.lock +++ b/fsharp/todolist-api/paket.lock @@ -2,6 +2,15 @@ STORAGE: NONE RESTRICTION: == netcoreapp3.1 NUGET remote: https://api.nuget.org/v3/index.json + Aether (8.3.1) + FSharp.Core (>= 4.3.4) + Chiron (6.3.1) + Aether (>= 8.2) + FParsec (>= 1.0.3) + FSharp.Core (>= 4.3.4) + FParsec (1.0.3) + FSharp.Core (>= 4.2.3) + NETStandard.Library (>= 1.6.1) FSharp.Core (4.7) FSharpPlus (1.1.0-CI00272) FSharp.Core (>= 4.6.2) diff --git a/fsharp/todolist-api/paket.references b/fsharp/todolist-api/paket.references index 7ca9b01..6f9e1f4 100644 --- a/fsharp/todolist-api/paket.references +++ b/fsharp/todolist-api/paket.references @@ -2,4 +2,5 @@ FSharp.Core Suave SQLProvider Npgsql -FSharpPlus \ No newline at end of file +FSharpPlus +Chiron \ No newline at end of file diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index 11ab61b..178b388 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -7,19 +7,19 @@ open Suave open Suave.Operators open Suave.Successful open Suave.RequestErrors -open Suave.Json open Suave.Filters +open Chiron module Utils = open System.Text open Db.Types - let jsonToString json = json |> toJson |> Encoding.UTF8.GetString - let todoToRecord (todo: DbTodo) = { id = todo.Id description = todo.Description name = todo.Name } + + let parseJson (input: byte array) = input |> Encoding.UTF8.GetString |> Json.parse |> Json.deserialize module App = open Utils @@ -34,12 +34,12 @@ module App = | None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND let todoById = - (fun (inner, _, _) -> inner |> todoToRecord |> jsonToString |> OK) |> withTodoById + withTodoById (fun (inner, _, _) -> inner |> todoToRecord |> Json.serialize |> Json.format |> OK) let updateTodo = - (fun (todo, dbContext, id) -> + withTodoById (fun (todo, dbContext, id) -> fun ctx -> async { - let body: Types.TodoDetails = ctx.request.rawForm |> fromJson + let body: Types.TodoDetails = parseJson ctx.request.rawForm do! Queries.updateTodosById todo body dbContext @@ -49,10 +49,11 @@ module App = id = id } - let withNewBody = newBody |> toJson |> ok - return! withNewBody ctx + let writeBody = newBody |> Json.serialize |> Json.format |> OK + + return! writeBody ctx } - ) |> withTodoById + ) let mainWebPart: WebPart = choose [ GET >=> pathScan "/todos/%i" todoById diff --git a/fsharp/todolist-api/src/Db.fs b/fsharp/todolist-api/src/Db.fs index a57faee..26ed0d5 100644 --- a/fsharp/todolist-api/src/Db.fs +++ b/fsharp/todolist-api/src/Db.fs @@ -15,27 +15,44 @@ module Context = module Types = open Context - open System.Runtime.Serialization + open Chiron type DbTodo = DbContext.``public.todosEntity`` - [] type Todo = - { [] - id: int - [] + { id: int description: string - [] name: string } + static member ToJson(todo: Todo) = + json { + do! Json.write "name" todo.name + do! Json.write "description" todo.description + do! Json.write "id" todo.id + } + + static member FromJson(_: Todo) = + json { + let! name = Json.read "name" + let! description = Json.read "description" + let! id = Json.read "id" + + return { name = name + description = description + id = id } + } - [] type TodoDetails = - { [] - description: string - [] + { description: string name: string } + static member FromJson(_: TodoDetails) = + json { + let! name = Json.read "name" + let! description = Json.read "description" + return { name = name + description = description } + }