80 lines
2.1 KiB
JavaScript
80 lines
2.1 KiB
JavaScript
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" };
|
|
}
|
|
});
|
|
//# sourceMappingURL=hero.js.map
|