From 5b0365ed449eee760e0129f416734b30e11407d2 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 2 Jan 2020 17:50:27 +0200 Subject: [PATCH] fsharp(todolist-api): feat: todo listing Signed-off-by: prescientmoon --- fsharp/todolist-api/src/App.fs | 18 +++++++++++++++--- fsharp/todolist-api/src/Db.fs | 6 ++++++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index 661e939..0b06be7 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -1,6 +1,6 @@ // Learn more about F# at http://fsharp.org open System - +open FSharpPlus.Operators open Suave open Suave.Operators open Suave.Successful @@ -62,13 +62,25 @@ module App = return! respondWithTodo todo ctx }) + let listTodos _ = + Context.getContext() + |> Queries.getAllTodos + |>> todoToRecord + |> Json.serialize + |> Json.format + |> OK + + let mainWebPart: WebPart = choose [ - pathScan "/todos/%i" (fun (id) -> choose [ + pathScan "/todos/%i" (fun (id) -> choose [ GET >=> todoById id PUT >=> updateTodo id PATCH >=> patchTodo id DELETE >=> deleteTodo id - ])] + ]) + path "/todos/" >=> choose [ + GET >=> warbler listTodos + ]] [] let main _ = diff --git a/fsharp/todolist-api/src/Db.fs b/fsharp/todolist-api/src/Db.fs index c8d50c4..83a3b0b 100644 --- a/fsharp/todolist-api/src/Db.fs +++ b/fsharp/todolist-api/src/Db.fs @@ -78,6 +78,12 @@ module Queries = } |> Seq.tryHead + let getAllTodos (ctx: DbContext): DbTodo list = + query { + for todo in ctx.Public.Todos do + select todo + } + |> Seq.toList let updateTodoById (todo: DbTodo) (details: TodoDetails) (ctx: DbContext) = todo.Name <- details.name