From 534ee1594a0900c029a8d70faa2898791285af19 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 2 Jan 2020 18:11:49 +0200 Subject: [PATCH] fsharp(todolist-api): feat: a route to create new todos Signed-off-by: prescientmoon --- fsharp/todolist-api/db/create.sql | 4 ++-- fsharp/todolist-api/src/App.fs | 9 +++++++++ fsharp/todolist-api/src/Db.fs | 10 ++++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/fsharp/todolist-api/db/create.sql b/fsharp/todolist-api/db/create.sql index 68127af..012ebb3 100644 --- a/fsharp/todolist-api/db/create.sql +++ b/fsharp/todolist-api/db/create.sql @@ -6,8 +6,8 @@ DROP TABLE IF EXISTS "todos" CASCADE; CREATE TABLE "todos" ( "id" SERIAL PRIMARY KEY NOT NULL, - "name" varchar(120) NULL, - "description" varchar(4000) NULL); + "name" varchar(120), + "description" varchar(4000)); INSERT INTO "todos" ("name", "description") VALUES ('Example', 'I wonder if you are reading this.'); diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index 0b06be7..bf29a65 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -70,6 +70,14 @@ module App = |> Json.format |> OK + let createTodo ctx = async { + let dbContext = Context.getContext() + let details: Types.TodoDetails = ctx.request.rawForm |> parseJson + + let! todo = Queries.createTodo details dbContext + + return! respondWithTodo todo ctx + } let mainWebPart: WebPart = choose [ pathScan "/todos/%i" (fun (id) -> choose [ @@ -80,6 +88,7 @@ module App = ]) path "/todos/" >=> choose [ GET >=> warbler listTodos + POST >=> createTodo ]] [] diff --git a/fsharp/todolist-api/src/Db.fs b/fsharp/todolist-api/src/Db.fs index 83a3b0b..8acbc2a 100644 --- a/fsharp/todolist-api/src/Db.fs +++ b/fsharp/todolist-api/src/Db.fs @@ -103,3 +103,13 @@ module Queries = todo.Delete() ctx.SubmitUpdatesAsync() + + let createTodo (details: TodoDetails) (ctx: DbContext) = + async { + let todo = ctx.Public.Todos.Create() + + do! updateTodoById todo details ctx + + return todo + } + \ No newline at end of file