perf: save chat and del chat content;UI

This commit is contained in:
archer
2023-04-30 13:18:54 +08:00
parent c971adaabd
commit f109f1cf60
19 changed files with 100 additions and 95 deletions

View File

@@ -0,0 +1,45 @@
import type { NextApiRequest, NextApiResponse } from 'next';
import { jsonRes } from '@/service/response';
import { connectToDatabase, Chat } from '@/service/mongo';
import { authToken } from '@/service/utils/auth';
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
try {
const { chatId, contentId } = req.query as { chatId: string; contentId: string };
const { authorization } = req.headers;
if (!authorization) {
throw new Error('无权操作');
}
if (!chatId || !contentId) {
throw new Error('缺少参数');
}
await connectToDatabase();
// 凭证校验
const userId = await authToken(authorization);
const chatRecord = await Chat.findById(chatId);
if (!chatRecord) {
throw new Error('找不到对话');
}
// 删除一条数据库记录
await Chat.updateOne(
{
_id: chatId,
userId
},
{ $pull: { content: { _id: contentId } } }
);
jsonRes(res);
} catch (err) {
jsonRes(res, {
code: 500,
error: err
});
}
}