add language query

This commit is contained in:
fiatcode 2025-10-20 14:43:52 +07:00
parent 8686861fac
commit 43810de00f
2 changed files with 24 additions and 10 deletions

View file

@ -4,22 +4,34 @@ import type { Quote, Translation } from '../types.js';
const MAX_QUOTE_ID = 250000; const MAX_QUOTE_ID = 250000;
const db = new DatabaseSync('quotes.db'); 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 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 = ?`) .prepare(`SELECT * FROM ${tableName} WHERE id = ?`)
.get(randomId); .get(randomId);
if (!row) { if (!quoteRow) {
throw new Error(`Quote with id ${randomId} not found.`); throw new Error(`Quote with id ${randomId} not found.`);
} }
return { return {
id: row['id'] as number, id: quoteRow['id'] as number,
text: row['quote'] as string, text: quoteRow['quote'] as string,
author: row['author'] 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); const row = db.prepare(`SELECT * FROM ${tableName} WHERE id = ?`).get(id);
if (!row) { if (!row) {
throw new Error(`Quote with id ${id} not found.`); throw new Error(`Quote with id ${id} not found.`);

View file

@ -2,9 +2,10 @@ import type { Request, Response } from 'express';
import { getQuote, getRandomQuote, listTranslations } from './data/quote.js'; import { getQuote, getRandomQuote, listTranslations } from './data/quote.js';
import { listRandomImages } from './data/unsplash.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 { try {
const quote = getRandomQuote(); const quote = getRandomQuote(langQuery);
res.status(200).send(quote); res.status(200).send(quote);
} catch (error: any) { } catch (error: any) {
res.status(500).send({ error: error.message }); 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 { function onGetQuoteById(req: Request, res: Response): void {
const langQuery = (req.query['lang'] as string) ?? 'en';
const idParam = req.params['id']; const idParam = req.params['id'];
if (!idParam) { if (!idParam) {
res.status(400).send({ error: 'Quote ID is required.' }); res.status(400).send({ error: 'Quote ID is required.' });
@ -20,7 +22,7 @@ function onGetQuoteById(req: Request, res: Response): void {
try { try {
const id = parseInt(idParam); const id = parseInt(idParam);
const quote = getQuote(id); const quote = getQuote(id, langQuery);
res.status(200).send(quote); res.status(200).send(quote);
} catch (e) { } catch (e) {
res.status(500).send({ error: (e as Error).message }); res.status(500).send({ error: (e as Error).message });