Update code
This commit is contained in:
@@ -0,0 +1,30 @@
|
||||
import { prisma } from '@/lib/prisma';
|
||||
|
||||
interface Props {
|
||||
params: { id: string };
|
||||
}
|
||||
|
||||
export default async function PastePage({ params }: Props) {
|
||||
const paste = await prisma.paste.findUnique({
|
||||
where: { id: params.id },
|
||||
select: { title: true, content: true, createdAt: true, views: true },
|
||||
});
|
||||
|
||||
if (!paste) return <div>Paste not found</div>;
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-[#0e0f13] text-white p-6">
|
||||
<h1 className="text-3xl font-bold text-blue-400 mb-4">
|
||||
{paste.title || 'Untitled'}
|
||||
</h1>
|
||||
|
||||
<pre className="bg-[#11131c] p-6 rounded-xl whitespace-pre-wrap overflow-auto text-sm border border-white/10 shadow-lg">
|
||||
{paste.content}
|
||||
</pre>
|
||||
|
||||
<p className="mt-4 text-xs text-neutral-400">
|
||||
Views: {paste.views} · Created at: {new Date(paste.createdAt).toLocaleString()}
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,68 @@
|
||||
import { prisma } from '@/lib/prisma';
|
||||
import bcrypt from 'bcryptjs';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const dynamic = 'force-dynamic';
|
||||
export const revalidate = 0;
|
||||
|
||||
export async function POST(
|
||||
request: NextRequest,
|
||||
context: { params: { id: string } }
|
||||
) {
|
||||
const { params } = context;
|
||||
const pasteId = params.id;
|
||||
|
||||
const { password = '' } = await request.json();
|
||||
|
||||
if (!pasteId) {
|
||||
return NextResponse.json({ error: 'Invalid ID' }, { status: 400 });
|
||||
}
|
||||
|
||||
const paste = await prisma.paste.findUnique({ where: { id: pasteId } });
|
||||
if (!paste) {
|
||||
return NextResponse.json({ error: 'Not found' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (paste.maxViews !== null && paste.views >= paste.maxViews) {
|
||||
await prisma.paste.delete({ where: { id: paste.id } });
|
||||
return NextResponse.json({ error: 'Max views exceeded' }, { status: 404 });
|
||||
}
|
||||
|
||||
if (paste.password) {
|
||||
const valid = await bcrypt.compare(password, paste.password);
|
||||
if (!valid) {
|
||||
return NextResponse.json({ error: 'Invalid password' }, { status: 403 });
|
||||
}
|
||||
}
|
||||
|
||||
const updated = await prisma.paste.update({
|
||||
where: { id: paste.id },
|
||||
data: { views: { increment: 1 } },
|
||||
select: {
|
||||
id: true,
|
||||
title: true,
|
||||
content: true,
|
||||
createdAt: true,
|
||||
views: true,
|
||||
maxViews: true,
|
||||
password: true,
|
||||
},
|
||||
});
|
||||
|
||||
const payload = {
|
||||
id: updated.id,
|
||||
title: updated.title,
|
||||
content: updated.content,
|
||||
createdAt: updated.createdAt,
|
||||
views: updated.views,
|
||||
protected: !!updated.password,
|
||||
};
|
||||
|
||||
if (updated.maxViews !== null && updated.views >= updated.maxViews) {
|
||||
prisma.paste.delete({ where: { id: updated.id } }).catch(() => {});
|
||||
}
|
||||
|
||||
return NextResponse.json(payload, {
|
||||
headers: { 'Cache-Control': 'no-store' },
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user