From 4951b6c2cad71a5b9f9f5054471a72eeaf5c9cb3 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 2 Jan 2020 14:24:11 +0200 Subject: [PATCH] fsharp(todolist-api): feat: json output for the todo by id route Signed-off-by: prescientmoon --- fsharp/todolist-api/paket.dependencies | 3 ++- fsharp/todolist-api/paket.lock | 2 ++ fsharp/todolist-api/paket.references | 5 +++-- fsharp/todolist-api/src/App.fs | 10 +++++++++- fsharp/todolist-api/src/Db.fs | 22 +++++++++++++++++++++- 5 files changed, 37 insertions(+), 5 deletions(-) diff --git a/fsharp/todolist-api/paket.dependencies b/fsharp/todolist-api/paket.dependencies index e5fd088..2a81ced 100644 --- a/fsharp/todolist-api/paket.dependencies +++ b/fsharp/todolist-api/paket.dependencies @@ -6,4 +6,5 @@ framework: netcore3.1 nuget FSharp.Core nuget Suave nuget SQLProvider -nuget Npgsql \ No newline at end of file +nuget Npgsql +nuget FSharpPlus 1.1.0-CI00272 \ No newline at end of file diff --git a/fsharp/todolist-api/paket.lock b/fsharp/todolist-api/paket.lock index 2e06b1b..24a861e 100644 --- a/fsharp/todolist-api/paket.lock +++ b/fsharp/todolist-api/paket.lock @@ -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) diff --git a/fsharp/todolist-api/paket.references b/fsharp/todolist-api/paket.references index b5c75be..7ca9b01 100644 --- a/fsharp/todolist-api/paket.references +++ b/fsharp/todolist-api/paket.references @@ -1,4 +1,5 @@ FSharp.Core Suave -nuget SQLProvider -nuget Npgsql \ No newline at end of file +SQLProvider +Npgsql +FSharpPlus \ No newline at end of file diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index 8c8aa56..adc665e 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -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 [ diff --git a/fsharp/todolist-api/src/Db.fs b/fsharp/todolist-api/src/Db.fs index a8615e3..0f13d4e 100644 --- a/fsharp/todolist-api/src/Db.fs +++ b/fsharp/todolist-api/src/Db.fs @@ -15,10 +15,29 @@ module Context = module Types = open Context + open System.Runtime.Serialization + + type DbTodo = DbContext.``public.todosEntity`` + + + [] + type Todo = + { [] + id: int + [] + description: string + [] + 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 \ No newline at end of file