28 lines
1 KiB
TypeScript
28 lines
1 KiB
TypeScript
import { NextResponse } from 'next/server';
|
|
import { apiWrite } from '@/lib/server/guard';
|
|
import { createPost, listPostsAdmin, postInputSchema } from '@/lib/server/content';
|
|
|
|
export const dynamic = 'force-dynamic';
|
|
|
|
export async function GET(request: Request) {
|
|
const g = await apiWrite();
|
|
if (g.error) return g.error;
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const status = searchParams.get('status') || undefined;
|
|
const posts = await listPostsAdmin(status);
|
|
return NextResponse.json({ posts });
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
const g = await apiWrite();
|
|
if (g.error) return g.error;
|
|
|
|
const parsed = postInputSchema.safeParse(await request.json().catch(() => ({})));
|
|
if (!parsed.success) return NextResponse.json({ error: 'Invalid post', issues: parsed.error.flatten() }, { status: 400 });
|
|
|
|
const result = await createPost(parsed.data, g.user);
|
|
if (!result.ok) return NextResponse.json({ error: result.error }, { status: 400 });
|
|
return NextResponse.json({ post: result.post }, { status: 201 });
|
|
}
|