Began model migration to RavenDB format

This commit is contained in:
Daniel J. Summers
2019-07-13 22:55:53 -05:00
parent 10a5c26903
commit cc5dd3bd7f
6 changed files with 330 additions and 169 deletions

View File

@@ -40,6 +40,7 @@ module Error =
module private Helpers =
open Microsoft.AspNetCore.Http
open Raven.Client.Documents
open System.Threading.Tasks
open System.Security.Claims
@@ -47,6 +48,10 @@ module private Helpers =
let db (ctx : HttpContext) =
ctx.GetService<AppDbContext> ()
/// Create a RavenDB session
let session (ctx : HttpContext) =
ctx.GetService<IDocumentStore>().OpenAsyncSession ()
/// Get the user's "sub" claim
let user (ctx : HttpContext) =
ctx.User.Claims |> Seq.tryFind (fun u -> u.Type = ClaimTypes.NameIdentifier)
@@ -54,7 +59,7 @@ module private Helpers =
/// Get the current user's ID
// NOTE: this may raise if you don't run the request through the authorize handler first
let userId ctx =
((user >> Option.get) ctx).Value
((user >> Option.get) ctx).Value |> UserId
/// Return a 201 CREATED response
let created next ctx =
@@ -163,28 +168,28 @@ module Request =
>=> fun next ctx ->
task {
let! r = ctx.BindJsonAsync<Models.Request> ()
let db = db ctx
let reqId = Cuid.Generate ()
use sess = session ctx
let reqId = (Cuid.Generate >> Domain.Cuid >> RequestId) ()
let usrId = userId ctx
let now = jsNow ()
{ Request.empty with
requestId = reqId
userId = usrId
enteredOn = now
showAfter = now
recurType = r.recurType
recurCount = r.recurCount
}
|> db.AddEntry
{ History.empty with
requestId = reqId
asOf = now
status = "Created"
text = Some r.requestText
}
|> db.AddEntry
let! _ = db.SaveChangesAsync ()
match! db.TryJournalById reqId usrId with
let now = (jsNow >> Ticks) ()
do! sess.AddRequest
{ Request.empty with
Id = string reqId
userId = usrId
enteredOn = now
showAfter = now
recurType = Recurrence.fromString r.recurType
recurCount = r.recurCount
history = [
{ History.empty with
asOf = now
status = "Created"
text = Some r.requestText
}
]
}
do! sess.SaveChangesAsync ()
match! sess.TryJournalById reqId usrId with
| Some req -> return! (setStatusCode 201 >=> json req) next ctx
| None -> return! Error.notFound next ctx
}
@@ -194,23 +199,23 @@ module Request =
authorize
>=> fun next ctx ->
task {
let db = db ctx
match! db.TryRequestById reqId (userId ctx) with
use sess = session ctx
let reqId = (Domain.Cuid >> RequestId) reqId
match! sess.TryRequestById reqId (userId ctx) with
| Some req ->
let! hist = ctx.BindJsonAsync<Models.HistoryEntry> ()
let now = jsNow ()
let now = (jsNow >> Ticks) ()
{ History.empty with
requestId = reqId
asOf = now
status = hist.status
text = match hist.updateText with null | "" -> None | x -> Some x
asOf = now
status = hist.status
text = match hist.updateText with null | "" -> None | x -> Some x
}
|> db.AddEntry
|> sess.AddHistory reqId
match hist.status with
| "Prayed" ->
db.UpdateEntry { req with showAfter = now + (recurrence.[req.recurType] * int64 req.recurCount) }
sess.UpdateEntry { req with showAfter = now + (recurrence.[req.recurType] * int64 req.recurCount) }
| _ -> ()
let! _ = db.SaveChangesAsync ()
do! sess.SaveChangesAsync ()
return! created next ctx
| None -> return! Error.notFound next ctx
}
@@ -225,9 +230,8 @@ module Request =
| Some _ ->
let! notes = ctx.BindJsonAsync<Models.NoteEntry> ()
{ Note.empty with
requestId = reqId
asOf = jsNow ()
notes = notes.notes
asOf = (jsNow >> Ticks) ()
notes = notes.notes
}
|> db.AddEntry
let! _ = db.SaveChangesAsync ()
@@ -248,7 +252,8 @@ module Request =
authorize
>=> fun next ctx ->
task {
match! (db ctx).TryJournalById reqId (userId ctx) with
use sess = session ctx
match! sess.TryJournalById reqId (userId ctx) with
| Some req -> return! json req next ctx
| None -> return! Error.notFound next ctx
}
@@ -258,7 +263,8 @@ module Request =
authorize
>=> fun next ctx ->
task {
match! (db ctx).TryFullRequestById reqId (userId ctx) with
use sess = session ctx
match! sess.TryFullRequestById reqId (userId ctx) with
| Some req -> return! json req next ctx
| None -> return! Error.notFound next ctx
}
@@ -281,7 +287,7 @@ module Request =
match! db.TryRequestById reqId (userId ctx) with
| Some req ->
let! show = ctx.BindJsonAsync<Models.Show> ()
{ req with showAfter = show.showAfter }
{ req with showAfter = Ticks show.showAfter }
|> db.UpdateEntry
let! _ = db.SaveChangesAsync ()
return! setStatusCode 204 next ctx
@@ -297,7 +303,7 @@ module Request =
match! db.TryRequestById reqId (userId ctx) with
| Some req ->
let! until = ctx.BindJsonAsync<Models.SnoozeUntil> ()
{ req with snoozedUntil = until.until; showAfter = until.until }
{ req with snoozedUntil = Ticks until.until; showAfter = Ticks until.until }
|> db.UpdateEntry
let! _ = db.SaveChangesAsync ()
return! setStatusCode 204 next ctx
@@ -313,7 +319,7 @@ module Request =
match! db.TryRequestById reqId (userId ctx) with
| Some req ->
let! recur = ctx.BindJsonAsync<Models.Recurrence> ()
{ req with recurType = recur.recurType; recurCount = recur.recurCount }
{ req with recurType = Recurrence.fromString recur.recurType; recurCount = recur.recurCount }
|> db.UpdateEntry
let! _ = db.SaveChangesAsync ()
return! setStatusCode 204 next ctx