1
Fork 0

fsharp(todolist-api): feat: a route to create new todos

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2020-01-02 18:11:49 +02:00 committed by prescientmoon
parent 5b0365ed44
commit 534ee1594a
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
3 changed files with 21 additions and 2 deletions

View file

@ -6,8 +6,8 @@ DROP TABLE IF EXISTS "todos" CASCADE;
CREATE TABLE "todos" ( CREATE TABLE "todos" (
"id" SERIAL PRIMARY KEY NOT NULL, "id" SERIAL PRIMARY KEY NOT NULL,
"name" varchar(120) NULL, "name" varchar(120),
"description" varchar(4000) NULL); "description" varchar(4000));
INSERT INTO "todos" ("name", "description") VALUES ('Example', 'I wonder if you are reading this.'); INSERT INTO "todos" ("name", "description") VALUES ('Example', 'I wonder if you are reading this.');

View file

@ -70,6 +70,14 @@ module App =
|> Json.format |> Json.format
|> OK |> 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 [ let mainWebPart: WebPart = choose [
pathScan "/todos/%i" (fun (id) -> choose [ pathScan "/todos/%i" (fun (id) -> choose [
@ -80,6 +88,7 @@ module App =
]) ])
path "/todos/" >=> choose [ path "/todos/" >=> choose [
GET >=> warbler listTodos GET >=> warbler listTodos
POST >=> createTodo
]] ]]
[<EntryPoint>] [<EntryPoint>]

View file

@ -103,3 +103,13 @@ module Queries =
todo.Delete() todo.Delete()
ctx.SubmitUpdatesAsync() ctx.SubmitUpdatesAsync()
let createTodo (details: TodoDetails) (ctx: DbContext) =
async {
let todo = ctx.Public.Todos.Create()
do! updateTodoById todo details ctx
return todo
}