Files
my-portfolio/src/app/api/blogs/[slug]/route.ts
T
luna 912e5edb81
Build Check / build (pull_request) Successful in 34s
Build Check / push-image (pull_request) Has been skipped
Build Check / deploy-coolify (pull_request) Has been skipped
Add blog CMS with tracked public posts
2026-07-17 22:02:25 +00:00

33 lines
776 B
TypeScript

import { NextResponse } from "next/server";
import { prisma } from "@/lib/prisma";
import { toBlogPost } from "@/lib/blogs";
type Params = { params: Promise<{ slug: string }> };
export const dynamic = "force-dynamic";
export async function GET(_req: Request, { params }: Params) {
const { slug } = await params;
const post = await prisma.$transaction(async (tx) => {
const found = await tx.blogPost.findFirst({
where: { slug, isPublished: true },
});
if (!found) {
return null;
}
return tx.blogPost.update({
where: { id: found.id },
data: { views: { increment: 1 } },
});
});
if (!post) {
return NextResponse.json({ error: "Not found" }, { status: 404 });
}
return NextResponse.json(toBlogPost(post));
}