nuxt-prawo-jazdy/server/api/advanced.get.ts
NetMan 625c1013da Nuxt3=>4, spa-loader, css shenanigans...:
...css shenanigans: daisyui may have had a breaking change at 5.3.0, it wrecks the ui here, as tailwind forces its own styles over daisyuis overrides, for now staying at ~5.2.5, for good measure also @nuxtjs/tailwindcss stays at ^6.13.2 (6.13.2 in pnpmlock) - this may be, because (but i'm not sure) @nuxtjs/tailwind is tailwind3 and not tailwind4?

Nuxt3=>4 migration:
also changed nuxtimage settings, as the docs have been fixed, path changes, etc
2025-12-17 21:33:36 +01:00

79 lines
2.2 KiB
TypeScript

import 'dotenv/config';
import { drizzle } from 'drizzle-orm/libsql';
import { sql, eq, and } from 'drizzle-orm';
import arrayShuffle from 'array-shuffle';
import {
tasks_advanced,
questions_advanced,
categories_db,
} from '~~/db/schema';
import type { AdvancedQuestion } from '~~/shared/types';
import categories from '~~/categories';
export default defineEventHandler(async (event) => {
const query = getQuery(event);
const category = query.category;
const lang = query.lang;
if (
typeof category !== 'string' ||
!categories.includes(`${category.toUpperCase()}`)
) {
throw createError({
statusCode: 400,
statusMessage: `category argument has to be equal to either: ${categories}`,
});
}
if (
typeof lang !== 'string' ||
!['PL', 'EN', 'DE', 'UA'].includes(`${lang.toUpperCase()}`)
) {
throw createError({
statusCode: 400,
statusMessage: `lang argument has to be equal to either: pl, en, de, ua`,
});
}
async function getFromDb(
points: number,
limit: number,
category: string,
lang: string,
) {
return await db
.select({
id: tasks_advanced.id,
correct_answer: tasks_advanced.correct_answer,
media_url: tasks_advanced.media_url,
weight: tasks_advanced.weight,
text: questions_advanced.text,
answer_a: questions_advanced.answer_a,
answer_b: questions_advanced.answer_b,
answer_c: questions_advanced.answer_c,
})
.from(tasks_advanced)
.leftJoin(
questions_advanced,
eq(questions_advanced.task_id, tasks_advanced.id),
)
.leftJoin(categories_db, eq(categories_db.task_id, tasks_advanced.id))
.where(
and(
eq(categories_db.name, category.toUpperCase()),
eq(questions_advanced.lang, lang.toUpperCase()),
eq(tasks_advanced.weight, points),
),
)
.orderBy(sql`RANDOM()`)
.limit(limit);
}
const db = drizzle(process.env.DATABASE_URL!);
const randomizedQuestions: AdvancedQuestion[] = [];
for (const [key, value] of Object.entries({ 1: 2, 2: 4, 3: 6 })) {
randomizedQuestions.push(...(await getFromDb(+key, value, category, lang)));
}
return arrayShuffle(randomizedQuestions);
});