From 6400fd753baaf21f32a23cd7a6b2fb2726df96df Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 2 Jan 2020 15:55:40 +0200 Subject: [PATCH] fsharp(todolist-api): refactor: removed duplicate Some checks Signed-off-by: prescientmoon --- fsharp/todolist-api/src/App.fs | 28 +++++++++++----------------- 1 file changed, 11 insertions(+), 17 deletions(-) diff --git a/fsharp/todolist-api/src/App.fs b/fsharp/todolist-api/src/App.fs index bb6b5f4..11ab61b 100644 --- a/fsharp/todolist-api/src/App.fs +++ b/fsharp/todolist-api/src/App.fs @@ -25,22 +25,19 @@ module App = open Utils open Db - let todoById (id): WebPart = - let todo = - Context.getContext() - |> Queries.getTodosById id - |>> todoToRecord + let withTodoById f (id): WebPart = + let ctx = Context.getContext() + let dbTodo = ctx |> Queries.getTodosById id - match todo with - | Some inner -> inner |> jsonToString |> OK + match dbTodo with + | Some inner -> f (inner, ctx, id) | None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND - let updateTodo (id): WebPart = - let dbContext = Context.getContext() - let todo = dbContext |> Queries.getTodosById id + let todoById = + (fun (inner, _, _) -> inner |> todoToRecord |> jsonToString |> OK) |> withTodoById - match todo with - | Some todo -> + let updateTodo = + (fun (todo, dbContext, id) -> fun ctx -> async { let body: Types.TodoDetails = ctx.request.rawForm |> fromJson @@ -54,11 +51,8 @@ module App = let withNewBody = newBody |> toJson |> ok return! withNewBody ctx - } - - | None -> id |> sprintf "Cannot find todo with id %i" |> NOT_FOUND - - + } + ) |> withTodoById let mainWebPart: WebPart = choose [ GET >=> pathScan "/todos/%i" todoById