Skip to content

Commit

Permalink
Feat. 为评论增加点赞及删除功能
Browse files Browse the repository at this point in the history
  • Loading branch information
RavelloH committed Dec 20, 2024
1 parent dd64155 commit d7eec36
Show file tree
Hide file tree
Showing 4 changed files with 303 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/app/api/comment/delete/route.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export async function POST(request) {
}
limitControl.update(request);
return Response.json(
{ message: '修改成功', update: filteredObject },
{ message: '删除成功', update: filteredObject },
{ status: 200 },
);
} catch (e) {
Expand Down
125 changes: 125 additions & 0 deletions src/app/api/comment/like/route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* POST /api/comment/like
* WITH Authorization: Bearer <token>
* WITH BODY: {commentUid}
* RETURN {message}
*/

import prisma from '../../_utils/prisma';
import limitControl from '../../_utils/limitControl';
import token from '../../_utils/token';
import qs from 'qs';

export async function POST(request) {
try {
let info = await request.text();

if (typeof info == 'undefined' || info == '' || info == null) {
return Response.json(
{
message: '请传入必需的参数',
},
{ status: 400 },
);
}
if (typeof info == 'string') {
try {
info = qs.parse(info);
} catch (e) {
return Response.json(
{ message: '无法解析此请求', error: e },
{
status: 400,
},
);
}
}

const authHeader = request.headers.get('Authorization');
if (!authHeader) {
return Response.json({ message: '缺少授权头信息' }, { status: 400 });
}

if (!(await limitControl.check(request))) {
return Response.json({ message: '已触发速率限制' }, { status: 429 });
}

const tokenString = authHeader.split(' ')[1];
let tokenInfo;
try {
tokenInfo = token.verify(tokenString);
} catch (err) {
if (err.name == 'TokenExpiredError') {
return Response.json(
{
message: 'TOKEN已过期,请重新登录',
},
{ status: 410 },
);
} else {
return Response.json(
{
message: 'TOKEN无效',
error: err,
},
{ status: 400 },
);
}
}

if (tokenInfo) {
const { commentId } = info;
if (!commentId) {
return Response.json({ message: '请提供评论ID' }, { status: 400 });
}

try {
const comment = await prisma.comment.findUnique({
where: {
id: commentId,
},
});

if (!comment) {
return Response.json({ message: '评论不存在' }, { status: 404 });
}

let likeUserUid = comment.likeUserUid || [];
const userIndex = likeUserUid.indexOf(tokenInfo.uid);

if (userIndex > -1) {
likeUserUid.splice(userIndex, 1);
} else {
likeUserUid.push(tokenInfo.uid);
}

await prisma.comment.update({
where: {
id: commentId,
},
data: {
likeUserUid: likeUserUid,
},
});

limitControl.update(request);
return Response.json({ message: '操作成功', likeUserUid: likeUserUid }, { status: 200 });
} catch (error) {
return Response.json(
{ message: '点赞操作失败', error: error.message },
{ status: 500 },
);
}
}
} catch (error) {
console.error(error);
return Response.json(
{
code: 500,
message: '500 Internal server error.',
error: error,
},
{ status: 500 },
);
}
}
3 changes: 2 additions & 1 deletion src/assets/css/Comment.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,11 @@ textarea#comment-textarea {
margin-top: 2em;
}

.comment-item-header {
.comment-item-header, .comment-item-header-info {
display: flex;
flex-direction: row;
align-items: center;
justify-content: space-between;
}

.comment-item-nichname {
Expand Down
Loading

0 comments on commit d7eec36

Please sign in to comment.