Added recurrence SQL; updated API

API should support recurrence (#16); also updated for new F# match! statement
This commit is contained in:
Daniel J. Summers
2018-08-14 21:01:21 -05:00
parent 2bf3bc4865
commit 96f2f2f7e0
4 changed files with 112 additions and 45 deletions

View File

@@ -6,6 +6,14 @@ open Giraffe
open MyPrayerJournal
open System
/// Handler to return Vue files
module Vue =
/// The application index page
let app : HttpHandler = htmlFile "wwwroot/index.html"
/// Handlers for error conditions
module Error =
open Microsoft.Extensions.Logging
@@ -23,7 +31,7 @@ module Error =
|> List.length
|> function
| 0 -> (setStatusCode 404 >=> json ([ "error", "not found" ] |> dict)) next ctx
| _ -> htmlFile "wwwroot/index.html" next ctx
| _ -> Vue.app next ctx
/// Handler helpers
@@ -92,6 +100,10 @@ module Models =
type Request =
{ /// The text of the request
requestText : string
/// The recurrence type
recurType : string
/// The recurrence count
recurCount : int16 option
}
/// The time until which a request should not appear in the journal
@@ -119,6 +131,15 @@ module Request =
open NCuid
/// Ticks per recurrence
let private recurrence =
[ "immediate", 0L
"hours", 3600000L
"days", 86400000L
"weeks", 604800000L
]
|> Map.ofList
/// POST /api/request
let add : HttpHandler =
authorize
@@ -130,10 +151,12 @@ module Request =
let usrId = userId ctx
let now = jsNow ()
{ Request.empty with
requestId = reqId
userId = usrId
enteredOn = now
snoozedUntil = 0L
requestId = reqId
userId = usrId
enteredOn = now
showAfter = now
recurType = r.recurType
recurCount = defaultArg r.recurCount 0s
}
|> db.AddEntry
{ History.empty with
@@ -144,9 +167,8 @@ module Request =
}
|> db.AddEntry
let! _ = db.SaveChangesAsync ()
let! req = db.TryJournalById reqId usrId
match req with
| Some rqst -> return! (setStatusCode 201 >=> json rqst) next ctx
match! db.TryJournalById reqId usrId with
| Some req -> return! (setStatusCode 201 >=> json req) next ctx
| None -> return! Error.notFound next ctx
}
@@ -155,18 +177,22 @@ module Request =
authorize
>=> fun next ctx ->
task {
let db = db ctx
let! req = db.TryRequestById reqId (userId ctx)
match req with
| Some _ ->
let db = db ctx
match! db.TryRequestById reqId (userId ctx) with
| Some req ->
let! hist = ctx.BindJsonAsync<Models.HistoryEntry> ()
let now = jsNow ()
{ History.empty with
requestId = reqId
asOf = jsNow ()
asOf = now
status = hist.status
text = match hist.updateText with null | "" -> None | x -> Some x
}
|> db.AddEntry
match hist.status with
| "Prayed" ->
db.UpdateEntry { req with showAfter = now + (recurrence.[req.recurType] * int64 req.recurCount) }
| _ -> ()
let! _ = db.SaveChangesAsync ()
return! created next ctx
| None -> return! Error.notFound next ctx
@@ -177,15 +203,14 @@ module Request =
authorize
>=> fun next ctx ->
task {
let db = db ctx
let! req = db.TryRequestById reqId (userId ctx)
match req with
let db = db ctx
match! db.TryRequestById reqId (userId ctx) with
| Some _ ->
let! notes = ctx.BindJsonAsync<Models.NoteEntry> ()
{ Note.empty with
requestId = reqId
asOf = jsNow ()
notes = notes.notes
asOf = jsNow ()
notes = notes.notes
}
|> db.AddEntry
let! _ = db.SaveChangesAsync ()
@@ -206,9 +231,8 @@ module Request =
authorize
>=> fun next ctx ->
task {
let! req = (db ctx).TryJournalById reqId (userId ctx)
match req with
| Some r -> return! json r next ctx
match! (db ctx).TryJournalById reqId (userId ctx) with
| Some req -> return! json req next ctx
| None -> return! Error.notFound next ctx
}
@@ -217,9 +241,8 @@ module Request =
authorize
>=> fun next ctx ->
task {
let! req = (db ctx).TryCompleteRequestById reqId (userId ctx)
match req with
| Some r -> return! json r next ctx
match! (db ctx).TryCompleteRequestById reqId (userId ctx) with
| Some req -> return! json req next ctx
| None -> return! Error.notFound next ctx
}
@@ -228,9 +251,8 @@ module Request =
authorize
>=> fun next ctx ->
task {
let! req = (db ctx).TryFullRequestById reqId (userId ctx)
match req with
| Some r -> return! json r next ctx
match! (db ctx).TryFullRequestById reqId (userId ctx) with
| Some req -> return! json req next ctx
| None -> return! Error.notFound next ctx
}
@@ -248,12 +270,11 @@ module Request =
authorize
>=> fun next ctx ->
task {
let db = db ctx
let! req = db.TryRequestById reqId (userId ctx)
match req with
| Some r ->
let db = db ctx
match! db.TryRequestById reqId (userId ctx) with
| Some req ->
let! until = ctx.BindJsonAsync<Models.SnoozeUntil> ()
{ r with snoozedUntil = until.until }
{ req with snoozedUntil = until.until; showAfter = until.until }
|> db.UpdateEntry
let! _ = db.SaveChangesAsync ()
return! setStatusCode 204 next ctx