From a032abbc9b42ed366db8afce8c18ced605092e39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B3=E5=85=83=E7=9A=93?= Date: Wed, 18 Jun 2025 20:22:26 +0800 Subject: [PATCH] I mean this should work now? (Like the archiving feat. --- server/api/news/get/lt/[slug].ts | 38 +++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/server/api/news/get/lt/[slug].ts b/server/api/news/get/lt/[slug].ts index 811e5ea..1fb2ce0 100644 --- a/server/api/news/get/lt/[slug].ts +++ b/server/api/news/get/lt/[slug].ts @@ -38,20 +38,32 @@ function cleanUpSlug(orgslug: string) { } // Archive articles. For future use? -async function storeArticlesIfItDoesNotExists(data, RequestId) { - const checkDataIsInDatabase = await sql` - SELECT * FROM news_articles - WHERE jsondata = ${data} - `; - if (checkDataIsInDatabase.length > 0) { - return; +function storeArticlesIfItDoesNotExists(data, RequestId) { + try { + setImmediate(async () => { + try { + const checkDataIsInDatabase = await sql` + SELECT uuid FROM news_articles + WHERE article_id = ${RequestId} + LIMIT 1 + `; + if (checkDataIsInDatabase.length > 0) { + console.log(`Article ${RequestId} already exists in database`); + return; + } + const jsonData = JSON.stringify(data); + await sql` + INSERT INTO news_articles (uuid, article_id, jsondata) + VALUES (${uuidv4()}, ${RequestId}, ${jsonData}::JSON) + `; + console.log(`Successfully archived article ${RequestId} in background`); + } catch (error) { + console.error("Failed to archive article in background:", error); + } + }); + } catch (error) { + console.error("Failed to initiate background archiving:", error); } - const storeData = await sql` - INSERT INTO news_articles (uuid, article_id, jsondata) - VALUES (${uuidv4()}, ${RequestId}, ${data}::JSON) - `; - console.log(storeData); - return; } export default defineEventHandler(async (event) => {