From 5ef0579f0f2c6c45be6f136fbc15310e36116cd3 Mon Sep 17 00:00:00 2001 From: Daniel Bachler Date: Fri, 1 Mar 2024 19:05:36 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=A8=20clean=20up=20posts=20access=20vi?= =?UTF-8?q?a=20knex?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- adminSiteServer/apiRouter.ts | 4 +-- db/model/Post.ts | 49 ++++++++++++++++++++---------------- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index af9c73756de..50f2e8692af 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -2218,10 +2218,10 @@ apiRouter.get("/posts.json", async (req) => { return { posts: rows } }) -apiRouter.post("/posts/:postId/setTags", async (req, res) => { +postRouteWithRWTransaction(apiRouter, "/posts/:postId/setTags", async (req, res, trx) => { const postId = expectInt(req.params.postId) - await setTagsForPost(postId, req.body.tagIds) + await setTagsForPost(trx, postId, req.body.tagIds) return { success: true } }) diff --git a/db/model/Post.ts b/db/model/Post.ts index bfd96d7e19b..bbdd7a2750d 100644 --- a/db/model/Post.ts +++ b/db/model/Post.ts @@ -71,32 +71,34 @@ export const getTagsByPostId = async ( } export const setTags = async ( + trx: db.KnexReadWriteTransaction, postId: number, tagIds: number[] -): Promise => - await db.transaction(async (t) => { - const tagRows = tagIds.map((tagId) => [tagId, postId]) - await t.execute(`DELETE FROM post_tags WHERE post_id=?`, [postId]) - if (tagRows.length) - await t.execute( - `INSERT INTO post_tags (tag_id, post_id) VALUES ?`, - [tagRows] - ) - }) +): Promise => { + const tagRows = tagIds.map((tagId) => [tagId, postId]) + await db.knexRaw(trx, `DELETE FROM post_tags WHERE post_id=?`, [postId]) + if (tagRows.length) + await db.knexRaw( + trx, + `INSERT INTO post_tags (tag_id, post_id) VALUES ?`, + [tagRows] + ) +} export const setTagsForPost = async ( + trx: db.KnexReadWriteTransaction, postId: number, tagIds: number[] -): Promise => - await db.transaction(async (t) => { - const tagRows = tagIds.map((tagId) => [tagId, postId]) - await t.execute(`DELETE FROM post_tags WHERE post_id=?`, [postId]) - if (tagRows.length) - await t.execute( - `INSERT INTO post_tags (tag_id, post_id) VALUES ?`, - [tagRows] - ) - }) +): Promise => { + const tagRows = tagIds.map((tagId) => [tagId, postId]) + await db.knexRaw(trx, `DELETE FROM post_tags WHERE post_id=?`, [postId]) + if (tagRows.length) + await db.knexRaw( + trx, + `INSERT INTO post_tags (tag_id, post_id) VALUES ?`, + [tagRows] + ) +} export const getPostRawBySlug = async ( trx: db.KnexReadonlyTransaction, @@ -156,6 +158,8 @@ export const getFullPostByIdFromSnapshot = async ( return getFullPost(trx, postEnriched.wpApiSnapshot) } +// TODO: I suggest that in the place where we define SiteNavigationStatic we create a Set with all the leaves and +// then this one becomes a simple lookup in the set. Probably nicest to do the set creation as a memoized function. export const isPostSlugCitable = (slug: string): boolean => { const entries = SiteNavigationStatic.categories return entries.some((category) => { @@ -267,6 +271,7 @@ export const getBlogIndex = memoize( knex, [WP_PostType.Post], selectHomepagePosts + // TODO: consider doing this as a join instead of a 1+N query ).then((posts) => posts.map((post) => getFullPost(knex, post, true)) ) @@ -328,7 +333,7 @@ export const getWordpressPostReferencesByChartId = async ( ): Promise => { const relatedWordpressPosts: PostReference[] = await db.knexRaw( knex, - ` + `-- sql SELECT DISTINCT p.title, p.slug, @@ -376,7 +381,7 @@ export const getGdocsPostReferencesByChartId = async ( ): Promise => { const relatedGdocsPosts: PostReference[] = await db.knexRaw( knex, - ` + `-- sql SELECT DISTINCT pg.content ->> '$.title' AS title, pg.slug AS slug,