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