33 lines
776 B
TypeScript
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));
|
|
}
|