fsharp(todolist-api): refactor: refactored to use Chiron for json stuff
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
6400fd753b
commit
3023f6c410
|
@ -7,4 +7,5 @@ nuget FSharp.Core
|
|||
nuget Suave
|
||||
nuget SQLProvider
|
||||
nuget Npgsql
|
||||
nuget FSharpPlus 1.1.0-CI00272
|
||||
nuget FSharpPlus 1.1.0-CI00272
|
||||
nuget Chiron
|
|
@ -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)
|
||||
|
|
|
@ -2,4 +2,5 @@ FSharp.Core
|
|||
Suave
|
||||
SQLProvider
|
||||
Npgsql
|
||||
FSharpPlus
|
||||
FSharpPlus
|
||||
Chiron
|
|
@ -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
|
||||
|
|
|
@ -15,27 +15,44 @@ module Context =
|
|||
|
||||
module Types =
|
||||
open Context
|
||||
open System.Runtime.Serialization
|
||||
open Chiron
|
||||
|
||||
type DbTodo = DbContext.``public.todosEntity``
|
||||
|
||||
|
||||
[<DataContract>]
|
||||
type Todo =
|
||||
{ [<field:DataMember(Name = "id")>]
|
||||
id: int
|
||||
[<field:DataMember(Name = "description")>]
|
||||
{ id: int
|
||||
description: string
|
||||
[<field:DataMember(Name = "name")>]
|
||||
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 =
|
||||
{ [<field:DataMember(Name = "description")>]
|
||||
description: string
|
||||
[<field:DataMember(Name = "name")>]
|
||||
{ description: string
|
||||
name: string }
|
||||
static member FromJson(_: TodoDetails) =
|
||||
json {
|
||||
let! name = Json.read "name"
|
||||
let! description = Json.read "description"
|
||||
return { name = name
|
||||
description = description }
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue