From 46a7402c8e39902f473b1fd611e6daf6fceebaee Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Fri, 17 Feb 2023 22:10:10 -0500 Subject: [PATCH] Update updates :) --- .../Postgres/PostgresCategoryData.fs | 15 ++++++------ .../Postgres/PostgresPageData.fs | 8 +++---- .../Postgres/PostgresPostData.fs | 9 ++++--- .../Postgres/PostgresUploadData.fs | 2 +- .../Postgres/PostgresWebLogData.fs | 24 +++++++++---------- .../Postgres/PostgresWebLogUserData.fs | 8 +++---- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs index 28d491a..cbed623 100644 --- a/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresCategoryData.fs @@ -88,14 +88,17 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = match! findById catId webLogId with | Some cat -> // Reassign any children to the category's parent category - let! children = Find.byContains Table.Category {| ParentId = CategoryId.toString catId |} + let! children = Find.byContains Table.Category {| ParentId = CategoryId.toString catId |} let hasChildren = not (List.isEmpty children) if hasChildren then let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.update Table.Category, - children |> List.map (fun child -> catParameters { child with ParentId = cat.ParentId }) + Query.Update.partialById Table.Category, + children |> List.map (fun child -> [ + "@id", Sql.string (CategoryId.toString child.Id) + "@data", Query.jsonbDocParam {| ParentId = cat.ParentId |} + ]) ] () // Delete the category off all posts where it is assigned @@ -108,13 +111,11 @@ type PostgresCategoryData (source : NpgsqlDataSource, log : ILogger) = let! _ = Sql.fromDataSource source |> Sql.executeTransactionAsync [ - Query.update Table.Post, + Query.Update.partialById Table.Post, posts |> List.map (fun post -> [ "@id", Sql.string (PostId.toString post.Id) "@data", Query.jsonbDocParam - { post with - CategoryIds = post.CategoryIds |> List.filter (fun cat -> cat <> catId) - } + {| CategoryIds = post.CategoryIds |> List.filter (fun cat -> cat <> catId) |} ]) ] () diff --git a/src/MyWebLog.Data/Postgres/PostgresPageData.fs b/src/MyWebLog.Data/Postgres/PostgresPageData.fs index 729381a..01182a2 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPageData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPageData.fs @@ -161,11 +161,11 @@ type PostgresPageData (source : NpgsqlDataSource, log : ILogger) = /// Update a page's prior permalinks let updatePriorPermalinks pageId webLogId permalinks = backgroundTask { log.LogTrace "Page.updatePriorPermalinks" - match! findById pageId webLogId with - | Some page -> - do! update Table.Page (PageId.toString page.Id) { page with PriorPermalinks = permalinks } + match! pageExists pageId webLogId with + | true -> + do! Update.partialById Table.Page (PageId.toString pageId) {| PriorPermalinks = permalinks |} return true - | None -> return false + | false -> return false } interface IPageData with diff --git a/src/MyWebLog.Data/Postgres/PostgresPostData.fs b/src/MyWebLog.Data/Postgres/PostgresPostData.fs index 4f5cafd..71a42a7 100644 --- a/src/MyWebLog.Data/Postgres/PostgresPostData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresPostData.fs @@ -3,7 +3,6 @@ namespace MyWebLog.Data.Postgres open Microsoft.Extensions.Logging open MyWebLog open MyWebLog.Data -open NodaTime open NodaTime.Text open Npgsql open Npgsql.FSharp @@ -230,11 +229,11 @@ type PostgresPostData (source : NpgsqlDataSource, log : ILogger) = /// Update prior permalinks for a post let updatePriorPermalinks postId webLogId permalinks = backgroundTask { log.LogTrace "Post.updatePriorPermalinks" - match! findById postId webLogId with - | Some post -> - do! update Table.Post (PostId.toString post.Id) { post with PriorPermalinks = permalinks } + match! postExists postId webLogId with + | true -> + do! Update.partialById Table.Post (PostId.toString postId) {| PriorPermalinks = permalinks |} return true - | None -> return false + | false -> return false } interface IPostData with diff --git a/src/MyWebLog.Data/Postgres/PostgresUploadData.fs b/src/MyWebLog.Data/Postgres/PostgresUploadData.fs index 7c032b3..d713a19 100644 --- a/src/MyWebLog.Data/Postgres/PostgresUploadData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresUploadData.fs @@ -50,7 +50,7 @@ type PostgresUploadData (source : NpgsqlDataSource, log : ILogger) = if Option.isSome path then let! _ = Sql.fromDataSource source - |> Sql.query $"DELETE FROM {Table.Upload} WHERE id = @id" + |> Sql.query (Documents.Query.Delete.byId Table.Upload) |> Sql.parameters idParam |> Sql.executeNonQueryAsync return Ok path.Value diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs index f3d2d2f..67da00f 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogData.fs @@ -23,19 +23,19 @@ type PostgresWebLogData (source : NpgsqlDataSource, log : ILogger) = /// Delete a web log by its ID let delete webLogId = backgroundTask { log.LogTrace "WebLog.delete" - let criteria = Query.whereDataContains "@criteria" let! _ = Sql.fromDataSource source - |> Sql.query $" + |> Sql.query $""" DELETE FROM {Table.PostComment} - WHERE data->>'{nameof Comment.empty.PostId}' IN (SELECT id FROM {Table.Post} WHERE {criteria}); - DELETE FROM {Table.Post} WHERE {criteria}; - DELETE FROM {Table.Page} WHERE {criteria}; - DELETE FROM {Table.Category} WHERE {criteria}; - DELETE FROM {Table.TagMap} WHERE {criteria}; - DELETE FROM {Table.Upload} WHERE web_log_id = @webLogId; - DELETE FROM {Table.WebLogUser} WHERE {criteria}; - DELETE FROM {Table.WebLog} WHERE id = @webLogId" + 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""" |> Sql.parameters [ webLogIdParam webLogId; webLogContains webLogId ] |> Sql.executeNonQueryAsync () @@ -58,13 +58,13 @@ type PostgresWebLogData (source : NpgsqlDataSource, log : ILogger) = /// Update settings for a web log let updateSettings (webLog : WebLog) = log.LogTrace "WebLog.updateSettings" - update Table.WebLog (WebLogId.toString webLog.Id) webLog + 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 blog -> do! update Table.WebLog (WebLogId.toString webLog.Id) { blog with Rss = webLog.Rss } + | Some _ -> do! Update.partialById Table.WebLog (WebLogId.toString webLog.Id) {| Rss = webLog.Rss |} | None -> () } diff --git a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs index 33bd3cc..b1ea453 100644 --- a/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs +++ b/src/MyWebLog.Data/Postgres/PostgresWebLogUserData.fs @@ -86,10 +86,10 @@ type PostgresWebLogUserData (source : NpgsqlDataSource, log : ILogger) = /// Set a user's last seen date/time to now let setLastSeen userId webLogId = backgroundTask { log.LogTrace "WebLogUser.setLastSeen" - match! findById userId webLogId with - | Some user -> - do! update Table.WebLogUser (WebLogUserId.toString userId) { user with LastSeenOn = Some (Noda.now ()) } - | None -> () + match! Document.existsByWebLog source Table.WebLogUser userId WebLogUserId.toString webLogId with + | true -> + do! Update.partialById Table.WebLogUser (WebLogUserId.toString userId) {| LastSeenOn = Some (Noda.now ()) |} + | false -> () } /// Save a user