Files
altbin.dev/app/api/user-pastes/route.ts
T

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2025-08-20 17:14:23 +02:00
import { authOptions } from '@/lib/auth';
2025-08-20 14:14:33 +02:00
import { prisma } from '@/lib/prisma';
2025-08-20 19:32:43 +02:00
import type { Prisma } from '@prisma/client';
2025-08-20 14:14:33 +02:00
import { getServerSession } from 'next-auth';
import { NextResponse } from 'next/server';
export async function GET(req: Request) {
const session = await getServerSession(authOptions);
if (!session?.user?.id) {
return NextResponse.json([], { status: 401 });
}
const { searchParams } = new URL(req.url);
const page = parseInt(searchParams.get('page') || '1', 10);
const perPage = parseInt(searchParams.get('perPage') || '10', 10);
const skip = (page - 1) * perPage;
const search = searchParams.get('q') || '';
2025-08-20 19:32:43 +02:00
const filter: Prisma.PasteWhereInput = {
2025-08-20 18:15:23 +02:00
createdBy: session.user.id,
...(search
? {
title: {
contains: search,
2025-08-20 19:32:43 +02:00
mode: 'insensitive' as Prisma.QueryMode,
2025-08-20 18:15:23 +02:00
},
}
: {}),
};
2025-08-20 14:14:33 +02:00
const [pastes, total] = await Promise.all([
prisma.paste.findMany({
where: filter,
orderBy: { createdAt: 'desc' },
skip,
take: perPage,
}),
prisma.paste.count({
where: filter,
}),
]);
return NextResponse.json({ pastes, total });
2025-08-20 19:06:37 +02:00
}