Initial commit for homaroma

This commit is contained in:
Your Name committed 2026-06-03 08:39:12 +07:00
commit f2179cfe57
269 files changed
+22635

No files matched your search

+111
View File
@@ -0,0 +1,111 @@
import { Elysia, t } from "elysia";
import prisma from "../lib/prisma";
export const articlesRoute = new Elysia({ prefix: "/articles" })
.get("/", async ({ query, set }) => {
try {
const { publishedOnly } = query as { publishedOnly?: string };
const where = publishedOnly === 'true' ? { isPublished: true } : {};
return await prisma.article.findMany({
where,
orderBy: { createdAt: 'desc' }
});
} catch (err) {
set.status = 500;
return { error: "Failed to load articles" };
}
})
.get("/:slug", async ({ params: { slug }, set }) => {
try {
const article = await prisma.article.findUnique({
where: { slug }
});
if (!article) {
set.status = 404;
return { error: "Article not found" };
}
return article;
} catch (err) {
set.status = 500;
return { error: "Failed to fetch article" };
}
})
.get("/id/:id", async ({ params: { id }, set }) => {
try {
const article = await prisma.article.findUnique({
where: { id: parseInt(id, 10) }
});
if (!article) {
set.status = 404;
return { error: "Article not found" };
}
return article;
} catch (err) {
set.status = 500;
return { error: "Failed to fetch article" };
}
})
.post("/", async ({ body, set }) => {
try {
const data = body as any;
return await prisma.article.create({
data: {
title: data.title,
slug: data.slug,
coverImage: data.coverImage,
content: data.content,
isPublished: data.isPublished
}
});
} catch (err) {
console.error(err);
set.status = 500;
return { error: "Failed to create article. Ensure slug is unique." };
}
}, {
body: t.Object({
title: t.String(),
slug: t.String(),
coverImage: t.Optional(t.String()),
content: t.String(),
isPublished: t.Boolean()
})
})
.put("/:id", async ({ params: { id }, body, set }) => {
try {
const data = body as any;
return await prisma.article.update({
where: { id: parseInt(id, 10) },
data: {
title: data.title,
slug: data.slug,
coverImage: data.coverImage,
content: data.content,
isPublished: data.isPublished
}
});
} catch (err) {
set.status = 500;
return { error: "Failed to update article" };
}
}, {
body: t.Object({
title: t.String(),
slug: t.String(),
coverImage: t.Optional(t.String()),
content: t.String(),
isPublished: t.Boolean()
})
})
.delete("/:id", async ({ params: { id }, set }) => {
try {
await prisma.article.delete({
where: { id: parseInt(id, 10) }
});
return { success: true };
} catch (err) {
set.status = 500;
return { error: "Failed to delete article" };
}
});