Convert Data Storage to PostgreSQL Documents #74

Merged
danieljsummers merged 11 commits from pg-doc into main 2023-10-10 02:15:39 +00:00
2 changed files with 21 additions and 20 deletions
Showing only changes of commit 559f780f8e - Show all commits

View File

@ -88,20 +88,13 @@ module Request =
let existsById (reqId : RequestId) (userId : UserId) = let existsById (reqId : RequestId) (userId : UserId) =
Exists.byContains Table.Request {| Id = reqId; UserId = userId |} Exists.byContains Table.Request {| Id = reqId; UserId = userId |}
/// Retrieve a request by its ID and user ID (includes history and notes) /// Retrieve a request by its ID and user ID
let tryByIdFull reqId userId = backgroundTask { let tryById reqId userId = backgroundTask {
match! Find.byId<Request> Table.Request (RequestId.toString reqId) with match! Find.byId<Request> Table.Request (RequestId.toString reqId) with
| Some req when req.UserId = userId -> return Some req | Some req when req.UserId = userId -> return Some req
| _ -> return None | _ -> return None
} }
/// Retrieve a request by its ID and user ID (excludes history and notes)
let tryById reqId userId = backgroundTask {
match! tryByIdFull reqId userId with
| Some req -> return Some { req with History = []; Notes = [] }
| None -> return None
}
/// Update recurrence for a request /// Update recurrence for a request
let updateRecurrence reqId userId (recurType : Recurrence) = backgroundTask { let updateRecurrence reqId userId (recurType : Recurrence) = backgroundTask {
let dbId = RequestId.toString reqId let dbId = RequestId.toString reqId
@ -134,7 +127,7 @@ module History =
/// Add a history entry /// Add a history entry
let add reqId userId hist = backgroundTask { let add reqId userId hist = backgroundTask {
let dbId = RequestId.toString reqId let dbId = RequestId.toString reqId
match! Request.tryByIdFull reqId userId with match! Request.tryById reqId userId with
| Some req -> | Some req ->
do! Update.partialById Table.Request dbId do! Update.partialById Table.Request dbId
{| History = (hist :: req.History) |> List.sortByDescending (fun it -> it.AsOf) |} {| History = (hist :: req.History) |> List.sortByDescending (fun it -> it.AsOf) |}
@ -147,22 +140,30 @@ module History =
module Journal = module Journal =
/// Retrieve a user's answered requests /// Retrieve a user's answered requests
let answered userId = backgroundTask { let answered (userId : UserId) = backgroundTask {
// TODO: only retrieve answered requests let! reqs =
let! reqs = Find.byContains<Request> Table.Request {| UserId = UserId.toString userId |} Custom.list
$"""{Query.Find.byContains Table.Request} AND {Query.whereJsonPathMatches "@stat"}"""
[ "@criteria", Query.jsonbDocParam {| UserId = userId |}
"@stat", Sql.string """$.history[0].status ? (@ == "Answered")"""
] fromData<Request>
return return
reqs reqs
|> Seq.ofList |> Seq.ofList
|> Seq.map JournalRequest.ofRequestFull |> Seq.map JournalRequest.ofRequestLite
|> Seq.filter (fun it -> it.LastStatus = Answered) |> Seq.filter (fun it -> it.LastStatus = Answered)
|> Seq.sortByDescending (fun it -> it.AsOf) |> Seq.sortByDescending (fun it -> it.AsOf)
|> List.ofSeq |> List.ofSeq
} }
/// Retrieve a user's current prayer journal (includes snoozed and non-immediate recurrence) /// Retrieve a user's current prayer journal (includes snoozed and non-immediate recurrence)
let forUser userId = backgroundTask { let forUser (userId : UserId) = backgroundTask {
// TODO: only retrieve unanswered requests let! reqs =
let! reqs = Find.byContains<Request> Table.Request {| UserId = UserId.toString userId |} Custom.list
$"""{Query.Find.byContains Table.Request} AND {Query.whereJsonPathMatches "@stat"}"""
[ "@criteria", Query.jsonbDocParam {| UserId = userId |}
"@stat", Sql.string """$.history[0].status ? (@ <> "Answered")"""
] fromData<Request>
return return
reqs reqs
|> Seq.ofList |> Seq.ofList
@ -191,7 +192,7 @@ module Note =
/// Add a note /// Add a note
let add reqId userId note = backgroundTask { let add reqId userId note = backgroundTask {
let dbId = RequestId.toString reqId let dbId = RequestId.toString reqId
match! Request.tryByIdFull reqId userId with match! Request.tryById reqId userId with
| Some req -> | Some req ->
do! Update.partialById Table.Request dbId do! Update.partialById Table.Request dbId
{| Notes = (note :: req.Notes) |> List.sortByDescending (fun it -> it.AsOf) |} {| Notes = (note :: req.Notes) |> List.sortByDescending (fun it -> it.AsOf) |}
@ -200,5 +201,5 @@ module Note =
/// Retrieve notes for a request by the request ID /// Retrieve notes for a request by the request ID
let byRequestId reqId userId = backgroundTask { let byRequestId reqId userId = backgroundTask {
match! Request.tryByIdFull reqId userId with Some req -> return req.Notes | None -> return [] match! Request.tryById reqId userId with Some req -> return req.Notes | None -> return []
} }

View File

@ -391,7 +391,7 @@ module Request =
// GET /request/[req-id]/full // GET /request/[req-id]/full
let getFull requestId : HttpHandler = requireUser >=> fun next ctx -> task { let getFull requestId : HttpHandler = requireUser >=> fun next ctx -> task {
match! Request.tryByIdFull (RequestId.ofString requestId) ctx.UserId with match! Request.tryById (RequestId.ofString requestId) ctx.UserId with
| Some req -> return! partial "Prayer Request" (Views.Request.full ctx.Clock ctx.TimeZone req) next ctx | Some req -> return! partial "Prayer Request" (Views.Request.full ctx.Clock ctx.TimeZone req) next ctx
| None -> return! Error.notFound next ctx | None -> return! Error.notFound next ctx
} }