Skip to content

Instantly share code, notes, and snippets.

@samueleresca
Created April 14, 2018 09:22
Show Gist options
  • Save samueleresca/936c3cc307b67ffebb0b46f96cd7b95b to your computer and use it in GitHub Desktop.
Save samueleresca/936c3cc307b67ffebb0b46f96cd7b95b to your computer and use it in GitHub Desktop.
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