namespace MyWebLog.Data.Postgres open BitBadger.Npgsql.FSharp.Documents open Microsoft.Extensions.Logging open MyWebLog open MyWebLog.Data /// PostgreSQL myWebLog web log data implementation type PostgresWebLogData (log : ILogger) = /// Add a web log let add (webLog : WebLog) = log.LogTrace "WebLog.add" insert Table.WebLog (WebLogId.toString webLog.Id) webLog /// Retrieve all web logs let all () = log.LogTrace "WebLog.all" Find.all Table.WebLog /// Delete a web log by its ID let delete webLogId = log.LogTrace "WebLog.delete" Custom.nonQuery $"""DELETE FROM {Table.PostComment} WHERE data ->> '{nameof Comment.empty.PostId}' IN (SELECT id FROM {Table.Post} WHERE {Query.whereDataContains "@criteria"}); {Query.Delete.byContains Table.Post}; {Query.Delete.byContains Table.Page}; {Query.Delete.byContains Table.Category}; {Query.Delete.byContains Table.TagMap}; {Query.Delete.byContains Table.WebLogUser}; DELETE FROM {Table.Upload} WHERE web_log_id = @webLogId; DELETE FROM {Table.WebLog} WHERE id = @webLogId""" [ webLogIdParam webLogId; webLogContains webLogId ] /// Find a web log by its host (URL base) let findByHost (url : string) = log.LogTrace "WebLog.findByHost" Custom.single (selectWithCriteria Table.WebLog) [ "@criteria", Query.jsonbDocParam {| UrlBase = url |} ] fromData /// Find a web log by its ID let findById webLogId = log.LogTrace "WebLog.findById" Find.byId Table.WebLog (WebLogId.toString webLogId) /// Update settings for a web log let updateSettings (webLog : WebLog) = log.LogTrace "WebLog.updateSettings" Update.full Table.WebLog (WebLogId.toString webLog.Id) webLog /// Update RSS options for a web log let updateRssOptions (webLog : WebLog) = backgroundTask { log.LogTrace "WebLog.updateRssOptions" match! findById webLog.Id with | Some _ -> do! Update.partialById Table.WebLog (WebLogId.toString webLog.Id) {| Rss = webLog.Rss |} | None -> () } interface IWebLogData with member _.Add webLog = add webLog member _.All () = all () member _.Delete webLogId = delete webLogId member _.FindByHost url = findByHost url member _.FindById webLogId = findById webLogId member _.UpdateSettings webLog = updateSettings webLog member _.UpdateRssOptions webLog = updateRssOptions webLog