mirror of
https://github.com/DefectingCat/DefectingCat.github.io
synced 2025-07-16 01:01:38 +00:00
54 lines
1.1 KiB
TypeScript
54 lines
1.1 KiB
TypeScript
import type { NextApiRequest, NextApiResponse } from 'next';
|
|
import { PrismaClient } from '@prisma/client';
|
|
import 'dotenv/config';
|
|
|
|
const prisma = new PrismaClient();
|
|
|
|
export default async function handler(
|
|
req: NextApiRequest,
|
|
res: NextApiResponse
|
|
) {
|
|
const search = async () => {
|
|
const totalNum = await prisma.posts.count();
|
|
const posts = await prisma.posts.findMany({
|
|
orderBy: {
|
|
date: 'desc',
|
|
},
|
|
take: 10,
|
|
select: {
|
|
id: true,
|
|
title: true,
|
|
date: true,
|
|
desc: true,
|
|
index_img: true,
|
|
url: true,
|
|
tags: {
|
|
select: {
|
|
name: true,
|
|
},
|
|
},
|
|
},
|
|
});
|
|
const pagingSize = 10;
|
|
const allPages = Math.ceil(totalNum / pagingSize);
|
|
|
|
try {
|
|
return res.status(200).json({
|
|
allPages,
|
|
posts,
|
|
});
|
|
} catch {
|
|
return res.status(404).json({
|
|
message: 'Not found.',
|
|
});
|
|
}
|
|
};
|
|
|
|
switch (req.method) {
|
|
case 'GET':
|
|
return search();
|
|
default:
|
|
return res.status(405).end(`Method ${req.method} Not Allowed`);
|
|
}
|
|
}
|