Created
October 18, 2017 17:52
-
-
Save gerardtoconnor/96b753032d5399c4f6e85922ac340a8a to your computer and use it in GitHub Desktop.
basic CRUD example in giraffe based on https://blogs.msdn.microsoft.com/dotnet/2017/09/26/build-a-web-service-with-f-and-net-core-2-0/
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
namespace CRUDSample | |
open System.IO | |
open NPoco | |
open Microsoft.Data.Sqlite | |
type LunchSpot() = | |
member val ID = 0 with get, set | |
member val Name = "" with get, set | |
member val Latitude = 0.0 with get, set | |
member val Longitude = 0.0 with get, set | |
member val Cuisine = "" with get, set | |
member val VegetarianOptions = false with get, set | |
member val VeganOptions = false with get, set | |
module LunchAccess = | |
let private connString = "Filename=" + Path.Combine(Directory.GetCurrentDirectory(), "Sample.db") | |
let private getLunchFetchingQuery filter = | |
let cuisinePart, hasCuisine = | |
match filter.Cuisine with | |
| Some c -> (sprintf "Cuisine = \"%s\"" c, true) | |
| None -> ("", false) | |
let vegetarianPart, hasVegetarianPart = | |
match filter.VegetarianOptions with | |
| Some v -> (sprintf "VegetarianOptions = \"%d\"" (if v then 1 else 0), true) // Sqlite uses ints 0 and 1 for bools. | |
| None -> ("", false) | |
let veganPart, hasVeganPart = | |
match filter.VeganOptions with | |
| Some v -> (sprintf "VeganOptions = \"%d\"" (if v then 1 else 0), true) // Sqlite uses ints 0 and 1 for bools. | |
| None -> ("", false) | |
if hasCuisine || hasVegetarianPart || hasVeganPart then | |
"where " + cuisinePart + | |
(if hasCuisine && hasVegetarianPart then " and " + vegetarianPart else vegetarianPart) + | |
(if (hasCuisine || hasVegetarianPart) && hasVeganPart then " and " + veganPart else veganPart) | |
|> Some | |
else | |
None | |
let getLunches (filter: LunchFilter) = | |
use db = new Database(connString) | |
match getLunchFetchingQuery filter with | |
| Some query -> db.Fetch<LunchSpot>(query) | |
| None -> db.Fetch<LunchSpot>() | |
let createLunch (lunchSpot: LunchSpot) = | |
let db = new Database(connString) | |
db.Insert(lunchSpot) | |
let updateLunch (lunchSpot: LunchSpot) = | |
use db = new Database(connString) | |
db.Update(lunchSpot) | |
let deleteLunch (lunchSpot: LunchSpot) = | |
use db = new Database(connString) | |
db.Delete(lunchSpot) | |
let webApp = | |
choose [ | |
route "home" >=> text "You're Home!!" | |
route "/lunchspot" >=> choose [ | |
GET >=> (fun next ctx -> | |
let filter = ctx.BindQueryString<LunchFilter>() | |
let lunchSpots = LunchAccess.getLunches filter | |
json lunchSpots next ctx | |
) | |
POST >=> (fun next ctx -> task { | |
let! lunch = ctx.BindJson<LunchSpot>() | |
LunchAccess.createLunch lunch | |
return! text (sprintf "Added %s to the lunch spots." lunch.Name) next ctx | |
}) | |
PUT >=> (fun next ctx -> task { | |
let! lunch = ctx.BindJson<LunchSpot>() | |
LunchAccess.updateLunch lunch | |
return! text (sprintf "updated %s in lunch spots." lunch.Name) next ctx | |
}) | |
DELETE >=> (fun next ctx -> task { | |
let! lunch = ctx.BindJson<LunchSpot>() | |
LunchAccess.deleteLunch lunch | |
return! text (sprintf "dleted %s from lunch spots." lunch.Name) next ctx | |
}) | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment