fsharp(todolist-api): feat: json output for the todo by id route
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
a1043bfa40
commit
4951b6c2ca
|
@ -7,3 +7,4 @@ nuget FSharp.Core
|
|||
nuget Suave
|
||||
nuget SQLProvider
|
||||
nuget Npgsql
|
||||
nuget FSharpPlus 1.1.0-CI00272
|
|
@ -3,6 +3,8 @@ RESTRICTION: == netcoreapp3.1
|
|||
NUGET
|
||||
remote: https://api.nuget.org/v3/index.json
|
||||
FSharp.Core (4.7)
|
||||
FSharpPlus (1.1.0-CI00272)
|
||||
FSharp.Core (>= 4.6.2)
|
||||
Microsoft.NETCore.Platforms (3.1)
|
||||
Microsoft.NETCore.Targets (3.1)
|
||||
Microsoft.Win32.Registry (4.7)
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
FSharp.Core
|
||||
Suave
|
||||
nuget SQLProvider
|
||||
nuget Npgsql
|
||||
SQLProvider
|
||||
Npgsql
|
||||
FSharpPlus
|
|
@ -5,13 +5,21 @@ open Suave.Successful
|
|||
open Suave.Operators
|
||||
open Suave.Filters
|
||||
open Suave.RequestErrors
|
||||
open Suave.Json
|
||||
|
||||
module Utils =
|
||||
open System.Text
|
||||
|
||||
let jsonToString json = json |> toJson |> Encoding.UTF8.GetString
|
||||
|
||||
module App =
|
||||
open Utils
|
||||
|
||||
let todoById (id) =
|
||||
let todo = Db.Context.getContext() |> Db.Queries.getTodosById id
|
||||
|
||||
match todo with
|
||||
| Some inner -> OK <| sprintf "%A" inner.Name
|
||||
| Some inner -> inner |> jsonToString |> OK
|
||||
| None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND
|
||||
|
||||
let mainWebPart: WebPart = choose [
|
||||
|
|
|
@ -15,10 +15,29 @@ module Context =
|
|||
|
||||
module Types =
|
||||
open Context
|
||||
open System.Runtime.Serialization
|
||||
|
||||
type DbTodo = DbContext.``public.todosEntity``
|
||||
|
||||
|
||||
[<DataContract>]
|
||||
type Todo =
|
||||
{ [<field:DataMember(Name = "id")>]
|
||||
id: int
|
||||
[<field:DataMember(Name = "description")>]
|
||||
description: string
|
||||
[<field:DataMember(Name = "name")>]
|
||||
name: string }
|
||||
|
||||
|
||||
let todoToRecord (todo: DbTodo) =
|
||||
{ id = todo.Id
|
||||
description = todo.Description
|
||||
name = todo.Name }
|
||||
|
||||
type Todo = DbContext.``public.todosEntity``
|
||||
|
||||
module Queries =
|
||||
open FSharpPlus.Operators
|
||||
open Context
|
||||
open Types
|
||||
|
||||
|
@ -29,4 +48,5 @@ module Queries =
|
|||
select todo
|
||||
}
|
||||
|> Seq.tryHead
|
||||
|>> todoToRecord
|
||||
|
Loading…
Reference in a new issue