From 43810de00fe0926f1687c8dfdca7219a7bdd8a94 Mon Sep 17 00:00:00 2001 From: fiatcode Date: Mon, 20 Oct 2025 14:43:52 +0700 Subject: [PATCH] add language query --- src/data/quote.ts | 26 +++++++++++++++++++------- src/routes.ts | 8 +++++--- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/src/data/quote.ts b/src/data/quote.ts index f36e201..1451f4e 100644 --- a/src/data/quote.ts +++ b/src/data/quote.ts @@ -4,22 +4,34 @@ import type { Quote, Translation } from '../types.js'; const MAX_QUOTE_ID = 250000; const db = new DatabaseSync('quotes.db'); -function getRandomQuote(tableName: string = 'quotes'): Quote { +function getTableNameForLang(langId: string) { + const translationRow = db + .prepare(`SELECT table_name FROM translations WHERE id = ?`) + .get(langId); + if (!translationRow) { + throw new Error(`Translation with id ${langId} not found.`); + } + return translationRow['table_name'] as string; +} + +function getRandomQuote(langId: string): Quote { const randomId = Math.floor(Math.random() * MAX_QUOTE_ID) + 1; - const row = db + const tableName = getTableNameForLang(langId); + const quoteRow = db .prepare(`SELECT * FROM ${tableName} WHERE id = ?`) .get(randomId); - if (!row) { + if (!quoteRow) { throw new Error(`Quote with id ${randomId} not found.`); } return { - id: row['id'] as number, - text: row['quote'] as string, - author: row['author'] as string, + id: quoteRow['id'] as number, + text: quoteRow['quote'] as string, + author: quoteRow['author'] as string, }; } -function getQuote(id: number, tableName: string = 'quotes'): Quote { +function getQuote(id: number, langId: string): Quote { + const tableName = getTableNameForLang(langId); const row = db.prepare(`SELECT * FROM ${tableName} WHERE id = ?`).get(id); if (!row) { throw new Error(`Quote with id ${id} not found.`); diff --git a/src/routes.ts b/src/routes.ts index b9d1c90..34c57ee 100644 --- a/src/routes.ts +++ b/src/routes.ts @@ -2,9 +2,10 @@ import type { Request, Response } from 'express'; import { getQuote, getRandomQuote, listTranslations } from './data/quote.js'; import { listRandomImages } from './data/unsplash.js'; -function onGetRandomQuote(_req: Request, res: Response): void { +function onGetRandomQuote(req: Request, res: Response): void { + const langQuery = (req.query['lang'] as string) ?? 'en'; try { - const quote = getRandomQuote(); + const quote = getRandomQuote(langQuery); res.status(200).send(quote); } catch (error: any) { res.status(500).send({ error: error.message }); @@ -12,6 +13,7 @@ function onGetRandomQuote(_req: Request, res: Response): void { } function onGetQuoteById(req: Request, res: Response): void { + const langQuery = (req.query['lang'] as string) ?? 'en'; const idParam = req.params['id']; if (!idParam) { res.status(400).send({ error: 'Quote ID is required.' }); @@ -20,7 +22,7 @@ function onGetQuoteById(req: Request, res: Response): void { try { const id = parseInt(idParam); - const quote = getQuote(id); + const quote = getQuote(id, langQuery); res.status(200).send(quote); } catch (e) { res.status(500).send({ error: (e as Error).message });