Add news article archiving & Add user favs.

This commit is contained in:
yuanhau 2025-06-10 21:16:27 +08:00
parent 2527056e85
commit 25760cf0d1
4 changed files with 48 additions and 7 deletions

View file

@ -33,6 +33,13 @@ if (error === null) {
errorMsg.value = error.value;
}
onMounted(() => {
if (data.length === 0) {
eerrrroorr.value = true;
errorMsg.value = "No data returned.";
}
});
async function getImageSource(image: string) {
console.log(image);
if (!image || image === "#") {

View file

@ -51,6 +51,15 @@ const createSources = await sql`
)
`;
const createArticlesArchive = await sql`
create table if not exists news_articles (
uuid text primary key,
article_id text primary key,
jsondata json not null,
archive_timestamp timestamp default CURRENT_TIMESTAMP,
)
`;
console.log("Creation Complete");
console.log(
"If the script still does not quit after 2 seconds after the 'Creation Complete' message, please stop it by using Ctrl + C or on mac Control + C",

View file

@ -1,5 +1,6 @@
import lineToday from "~/server/scrape/line_today";
import sql from "~/server/components/postgres";
import { v4 as uuidv4 } from "uuid";
interface CacheItems {
title: string;
@ -36,6 +37,23 @@ function cleanUpSlug(orgslug: string) {
return slug;
}
// 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;
}
const storeData = await sql`
INSERT INTO news_articles (uuid, article_id, jsondata)
VALUES (${uuidv4()}, ${RequestId}, ${data})
`;
console.log(storeData);
return;
}
export default defineEventHandler(async (event) => {
const translateQuery = getQuery(event).translate;
const translate = translateQuery === "true" ? true : false;
@ -54,6 +72,7 @@ export default defineEventHandler(async (event) => {
try {
const data = await lineToday(cleanSlug);
storeArticlesIfItDoesNotExists(data, cleanSlug);
cache[cleanSlug] = {
...data,
timestamp: Date.now(),

View file

@ -1,11 +1,17 @@
import getUserTokenMinusSQLInjection from "~/server/components/getUserToken";
import sql from "~/server/components/postgres";
export default defineEventHandler(async (event) => {
const userToken = await getUserTokenMinusSQLInjection(event);
if (userToken.error.length !== 0) {
return {
error: userToken.error,
};
}
const getData = await sql`
SELECT * FROM user_other_data
WHERE username = ${userToken.user}
`;
return {
items: [
{
name: "dfasdfa",
article_link: "49redjvicjwsd",
favTime: "2024-12-12",
},
],
items: getData[0].starred_news,
};
});