1
Fork 0

fsharp(todolist-api): feat: json output for the todo by id route

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2020-01-02 14:24:11 +02:00 committed by prescientmoon
parent a1043bfa40
commit 4951b6c2ca
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
5 changed files with 37 additions and 5 deletions

View file

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

View file

@ -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)

View file

@ -1,4 +1,5 @@
FSharp.Core
Suave
nuget SQLProvider
nuget Npgsql
SQLProvider
Npgsql
FSharpPlus

View file

@ -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 [

View file

@ -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