Created
April 14, 2018 09:22
-
-
Save samueleresca/936c3cc307b67ffebb0b46f96cd7b95b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Blog.FSharpWebAPI.Handlers | |
open Blog.FSharpWebAPI.DataAccess | |
open Blog.FSharpWebAPI.RequestModels | |
open Giraffe | |
open Microsoft.AspNetCore.Http | |
let labelsHandler = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
let context = ctx.RequestServices.GetService(typeof<LabelsContext>) :?> LabelsContext | |
getAll context |> ctx.WriteJsonAsync | |
let labelHandler (id : int) = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
let context = ctx.RequestServices.GetService(typeof<LabelsContext>) :?> LabelsContext | |
getLabel context id |> function | |
| Some l -> ctx.WriteJsonAsync l | |
| None -> (setStatusCode 404 >=> json "Label not found") next ctx | |
let labelAddHandler : HttpHandler = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
task { | |
let context = ctx.RequestServices.GetService(typeof<LabelsContext>) :?> LabelsContext | |
let! label = ctx.BindJsonAsync<CreateUpdateLabelRequest>() | |
match label.HasErrors with | |
| Some msg -> return! (setStatusCode 400 >=> json msg) next ctx | |
| None -> | |
return! addLabelAsync context label.GetLabel | |
|> Async.RunSynchronously | |
|> function | |
| Some l -> Successful.CREATED l next ctx | |
| None -> (setStatusCode 400 >=> json "Label not added") next ctx | |
} | |
let labelUpdateHandler (id : int) = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
task { | |
let context = ctx.RequestServices.GetService(typeof<LabelsContext>) :?> LabelsContext | |
let! label = ctx.BindJsonAsync<CreateUpdateLabelRequest>() | |
match label.HasErrors with | |
| Some msg -> return! (setStatusCode 400 >=> json msg) next ctx | |
| None -> | |
return! updateLabel context label.GetLabel id |> function | |
| Some l -> ctx.WriteJsonAsync l | |
| None -> (setStatusCode 400 >=> json "Label not updated") next ctx | |
} | |
let labelDeleteHandler (id : int) = | |
fun (next : HttpFunc) (ctx : HttpContext) -> | |
let context = ctx.RequestServices.GetService(typeof<LabelsContext>) :?> LabelsContext | |
deleteLabel context id |> function | |
| Some l -> ctx.WriteJsonAsync l | |
| None -> (setStatusCode 404 >=> json "Label not deleted") next ctx |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment