From 13270641d774736cf29f267161d9365919253859 Mon Sep 17 00:00:00 2001 From: Adedapo Aderemi Date: Tue, 17 Dec 2024 10:41:29 +0000 Subject: [PATCH] delete unrelated ids from pinboard table --- controllers/blog/api/get_articles.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/controllers/blog/api/get_articles.js b/controllers/blog/api/get_articles.js index f4a8465..3d2f86f 100644 --- a/controllers/blog/api/get_articles.js +++ b/controllers/blog/api/get_articles.js @@ -43,9 +43,9 @@ exports.get_articles = async (req, res) => { ON a.pinboard = b.id WHERE b.id = $1 AND a.db = 5 - ${page && limit ? `LIMIT $2 OFFSET $3` : ""} + ${limit ? `LIMIT $2 OFFSET $3` : ""} `, - page && limit ? [pinboard, pageLimit, offset] : [pinboard] + limit ? [pinboard, pageLimit, offset] : [pinboard] ); idList = idList.map((row) => row.pad); @@ -112,6 +112,22 @@ exports.get_articles = async (req, res) => { }; }); + // HACK: DELETE FROM PINBOARD TABLE IDS THAT DOES NOT EXIST IN BLOG DB + if (pinboard && idList && idList.length > articleIds.length) { + const unrelatedIds = idList.filter((item) => !articleIds.includes(item)); + if (unrelatedIds.length) { + await DB.general.any( + ` + DELETE FROM pinboard_contributions a + WHERE a.pinboard = $1 + AND a.pad = ANY ($2::int[]) + AND a.db = 5 + `, + [pinboard, unrelatedIds] + ); + } + } + return res.status(200).json(mergedResults); } catch (err) { console.error("Database query failed:", err);