import { Elysia, t } from "elysia"; import prisma from "../lib/prisma"; export const heroRoute = new Elysia({ prefix: "/hero" }) .get("/", async () => { return await prisma.hero.findFirst({ where: { isActive: true }, orderBy: { updatedAt: 'desc' } }) || await prisma.hero.findFirst({ orderBy: { updatedAt: 'desc' } }); }) .get("/all", async () => { return await prisma.hero.findMany({ orderBy: { updatedAt: 'desc' } }); }) .post("/", async ({ body, set }) => { try { const data = body; const { id, ...heroData } = data; if (id) { return await prisma.hero.update({ where: { id: Number(id) }, data: heroData }); } else { return await prisma.hero.create({ data: heroData }); } } catch (err) { console.error("[Hero] Error:", err); set.status = 500; return { error: "Failed to save hero data" }; } }, { body: t.Object({ id: t.Optional(t.Number()), tagline: t.String(), title: t.String(), description: t.String(), imageUrl: t.String(), buttonText: t.String(), buttonLink: t.String(), isActive: t.Boolean() }) }) .patch("/:id/activate", async ({ params: { id }, set }) => { try { await prisma.$transaction([ prisma.hero.updateMany({ data: { isActive: false } }), prisma.hero.update({ where: { id: Number(id) }, data: { isActive: true } }) ]); return { success: true }; } catch (err) { set.status = 500; return { error: "Failed to activate hero" }; } }) .delete("/:id", async ({ params: { id }, set }) => { try { await prisma.hero.delete({ where: { id: Number(id) } }); return { success: true }; } catch (err) { set.status = 500; return { error: "Failed to delete hero" }; } }) .patch("/:id/translations", async ({ params, body, set }) => { const id = Number(params.id); if (isNaN(id)) { set.status = 400; return { error: "Invalid hero ID" }; } const payload = body; const results = []; const LANGS = ['th', 'en', 'ja', 'zh', 'vi', 'ko']; for (const lang of LANGS) { const tagline = typeof payload.tagline === 'string' ? (lang === 'th' ? payload.tagline : null) : payload.tagline?.[lang]; const title = typeof payload.title === 'string' ? (lang === 'th' ? payload.title : null) : payload.title?.[lang]; const description = typeof payload.description === 'string' ? (lang === 'th' ? payload.description : null) : payload.description?.[lang]; const buttonText = typeof payload.buttonText === 'string' ? (lang === 'th' ? payload.buttonText : null) : payload.buttonText?.[lang]; if (!tagline && !title && !description && !buttonText) continue; const existing = await prisma.heroTranslation.findUnique({ where: { heroId_langCode: { heroId: id, langCode: lang } } }); const updateData = {}; if (tagline) updateData.tagline = tagline; if (title) updateData.title = title; if (description) updateData.description = description; if (buttonText) updateData.buttonText = buttonText; if (existing) { const updated = await prisma.heroTranslation.update({ where: { id: existing.id }, data: updateData }); results.push(updated); } else { const created = await prisma.heroTranslation.create({ data: { heroId: id, langCode: lang, ...updateData } }); results.push(created); } } const updated = await prisma.hero.findUnique({ where: { id }, include: { translations: true } }); return { success: true, translations: results, hero: updated }; }); //# sourceMappingURL=hero.js.map