1
Fork 0

fsharp(todolist-api): refactor: refactored to use Chiron for json stuff

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2020-01-02 16:51:22 +02:00 committed by prescientmoon
parent 6400fd753b
commit 3023f6c410
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
5 changed files with 50 additions and 21 deletions

View file

@ -7,4 +7,5 @@ nuget FSharp.Core
nuget Suave nuget Suave
nuget SQLProvider nuget SQLProvider
nuget Npgsql nuget Npgsql
nuget FSharpPlus 1.1.0-CI00272 nuget FSharpPlus 1.1.0-CI00272
nuget Chiron

View file

@ -2,6 +2,15 @@ STORAGE: NONE
RESTRICTION: == netcoreapp3.1 RESTRICTION: == netcoreapp3.1
NUGET NUGET
remote: https://api.nuget.org/v3/index.json 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) FSharp.Core (4.7)
FSharpPlus (1.1.0-CI00272) FSharpPlus (1.1.0-CI00272)
FSharp.Core (>= 4.6.2) FSharp.Core (>= 4.6.2)

View file

@ -2,4 +2,5 @@ FSharp.Core
Suave Suave
SQLProvider SQLProvider
Npgsql Npgsql
FSharpPlus FSharpPlus
Chiron

View file

@ -7,19 +7,19 @@ open Suave
open Suave.Operators open Suave.Operators
open Suave.Successful open Suave.Successful
open Suave.RequestErrors open Suave.RequestErrors
open Suave.Json
open Suave.Filters open Suave.Filters
open Chiron
module Utils = module Utils =
open System.Text open System.Text
open Db.Types open Db.Types
let jsonToString json = json |> toJson |> Encoding.UTF8.GetString
let todoToRecord (todo: DbTodo) = let todoToRecord (todo: DbTodo) =
{ id = todo.Id { id = todo.Id
description = todo.Description description = todo.Description
name = todo.Name } name = todo.Name }
let parseJson (input: byte array) = input |> Encoding.UTF8.GetString |> Json.parse |> Json.deserialize
module App = module App =
open Utils open Utils
@ -34,12 +34,12 @@ 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 =
(fun (inner, _, _) -> inner |> todoToRecord |> jsonToString |> OK) |> withTodoById withTodoById (fun (inner, _, _) -> inner |> todoToRecord |> Json.serialize |> Json.format |> OK)
let updateTodo = let updateTodo =
(fun (todo, dbContext, id) -> withTodoById (fun (todo, dbContext, id) ->
fun ctx -> async { 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 do! Queries.updateTodosById todo body dbContext
@ -49,10 +49,11 @@ module App =
id = id id = id
} }
let withNewBody = newBody |> toJson |> ok let writeBody = newBody |> Json.serialize |> Json.format |> OK
return! withNewBody ctx
return! writeBody ctx
} }
) |> withTodoById )
let mainWebPart: WebPart = choose [ let mainWebPart: WebPart = choose [
GET >=> pathScan "/todos/%i" todoById GET >=> pathScan "/todos/%i" todoById

View file

@ -15,27 +15,44 @@ module Context =
module Types = module Types =
open Context open Context
open System.Runtime.Serialization open Chiron
type DbTodo = DbContext.``public.todosEntity`` type DbTodo = DbContext.``public.todosEntity``
[<DataContract>]
type Todo = type Todo =
{ [<field:DataMember(Name = "id")>] { id: int
id: int
[<field:DataMember(Name = "description")>]
description: string description: string
[<field:DataMember(Name = "name")>]
name: 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 }
}
[<DataContract>]
type TodoDetails = type TodoDetails =
{ [<field:DataMember(Name = "description")>] { description: string
description: string
[<field:DataMember(Name = "name")>]
name: string } name: string }
static member FromJson(_: TodoDetails) =
json {
let! name = Json.read "name"
let! description = Json.read "description"
return { name = name
description = description }
}