Add blog CMS with tracked public posts
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
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));
|
||||
}
|
||||
Reference in New Issue
Block a user