37 lines
939 B
TypeScript
37 lines
939 B
TypeScript
import { Elysia, t } from "elysia";
|
|
import prisma from "../lib/prisma";
|
|
|
|
export const newsletterRoute = new Elysia({ prefix: "/newsletter" })
|
|
.post("/subscribe", async ({ body, set }) => {
|
|
try {
|
|
const { email } = body as { email: string };
|
|
|
|
const existing = await prisma.newsletterSubscriber.findUnique({
|
|
where: { email }
|
|
});
|
|
|
|
if (existing) {
|
|
return { message: "Email already subscribed" };
|
|
}
|
|
|
|
await prisma.newsletterSubscriber.create({
|
|
data: { email }
|
|
});
|
|
|
|
return { message: "Successfully subscribed" };
|
|
} catch (err) {
|
|
console.error(err);
|
|
set.status = 500;
|
|
return { error: "Failed to subscribe" };
|
|
}
|
|
}, {
|
|
body: t.Object({
|
|
email: t.String({ format: "email" })
|
|
})
|
|
})
|
|
.get("/subscribers", async () => {
|
|
return await prisma.newsletterSubscriber.findMany({
|
|
orderBy: { createdAt: "desc" }
|
|
});
|
|
});
|