Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

[Feat] 시그니처 댓글 삭제하기 구현 #202

Merged
merged 2 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 38 additions & 1 deletion src/signature/signature.comment.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import {
Body,
Controller,
Controller, Delete,
ForbiddenException,
Get,
NotFoundException,
Expand Down Expand Up @@ -144,7 +144,44 @@ export class SignatureCommentController{
errorMessage,
null
);
}
}


@Delete('/:commentId')
@UseGuards(UserGuard)
async deleteSignatureComment( // 시그니처 수정하기
@Param('signatureId') signatureId: number,
@Param('commentId') commentId: number,
@Req() req: Request,
){
try{
const result = await this.signatureCommentService.deleteSignatureComment(req.user.id,signatureId,commentId);

return new ResponseDto(
ResponseCode.COMMENT_DELETE_SUCCESS,
true,
"시그니처 댓글 삭제하기 성공",
result
);
}
catch(error){
console.log("Err on DeleteSigComment: "+ error);
let errorMessage = "";

if(error instanceof NotFoundException) errorMessage = error.message;
else if(error instanceof ForbiddenException) errorMessage = error.message;
else errorMessage = "시그니처 댓글 삭제하기 실패";

return new ResponseDto(
ResponseCode.COMMENT_DELETE_FAIL,
false,
errorMessage,
null
);

}
}


}
101 changes: 92 additions & 9 deletions src/signature/signature.comment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,10 @@ export class SignatureCommentService{
},
relations: {
user: { profileImage: true },
parentComment: true
parentComment: true,
signature:{
user: true,
}
},
order: {
parentComment: { id: "ASC" as any,},
Expand All @@ -103,8 +106,8 @@ export class SignatureCommentService{
writerProfile._id = comment.user.id;
writerProfile.name = comment.user.nickname;

// 로그인한 사용자가 댓글 작성자인지 확인
if( userId == comment.user.id ) writerProfile.is_writer = true;
// 로그인한 사용자가 댓글 작성자 혹은 시그니처 작성자인지 확인
if( userId == comment.user.id || userId == comment.signature.user.id ) writerProfile.is_writer = true;
else writerProfile.is_writer = false;

// 작성자 프로필 이미지
Expand All @@ -123,8 +126,14 @@ export class SignatureCommentService{
getCommentDto.date = comment.updated;

// 댓글 수정 여부 구하기
if(comment.created.getTime() === comment.updated.getTime()) getCommentDto.is_edited = false;
else getCommentDto.is_edited = true;
const createdTime = comment.created.getTime();
const updatedTime = comment.updated.getTime();

if (Math.abs(createdTime - updatedTime) <= 2000) { // 두 시간 차가 2초 이하면 수정 안함
getCommentDto.is_edited = false;
} else {
getCommentDto.is_edited = true;
}

return getCommentDto;

Expand Down Expand Up @@ -160,24 +169,98 @@ export class SignatureCommentService{
patchedComment: CreateCommentDto) {

// 시그니처 유효한지 확인
const signature = await SignatureEntity.findOne({ where:{ id: signatureId }});
const signature = await SignatureEntity.findOne({
where:{ id: signatureId },
relations: { user: true }
});
if(!signature) throw new NotFoundException('존재하지 않는 시그니처입니다');

// 댓글 데이터 유효한지 확인
const comment = await SignatureCommentEntity.findOne({
where:{ id: commentId },
relations: ['user']
relations: { user: true }
},
);
if(!comment) throw new NotFoundException('존재하지 않는 댓글입니다');

// 댓글 작성자가 로그인한 사용자 본인이 맞는지 확인
if(comment.user.id != userId ) throw new ForbiddenException('댓글 수정 권한이 없습니다');

let forbiddenUser = true;
// 댓글 작성자가 로그인한 사용자 본인 혹은 시그니처 작성자가 맞는지 확인
if(signature.user){ // 시그니처 작성자가 존재한다면 시그니처 작성자와 로그인한 사용자가 일치하는지 확인
if( signature.user.id == userId ) forbiddenUser = false;
}

if(comment.user.id){ // 댓글 작성자가 존재한다면 댓글 작성자와 로그인한 사용자가 일치하는지 확인
if(comment.user.id == userId ) forbiddenUser = false;
}

if(forbiddenUser) throw new ForbiddenException('댓글 수정 권한이 없습니다');


// 댓글 수정하기
comment.content = patchedComment.content;
await comment.save();
return comment.id;

}

async deleteSignatureComment(userId: number, signatureId: number, commentId: number) {
try {
// 시그니처 유효한지 확인
const signature = await SignatureEntity.findOne({
where: { id: signatureId },
relations: { user: true }
});
if (!signature) throw new NotFoundException('존재하지 않는 시그니처입니다');

// 댓글 데이터 유효한지 확인
const comment = await SignatureCommentEntity.findOne({
where: { id: commentId },
relations: ['user', 'parentComment', 'signature']
},
);
if (!comment) throw new NotFoundException('존재하지 않는 댓글입니다');


let forbiddenUser = true;
// 댓글 작성자가 로그인한 사용자 본인 혹은 시그니처 작성자가 맞는지 확인
if(signature.user){ // 시그니처 작성자가 존재한다면 시그니처 작성자와 로그인한 사용자가 일치하는지 확인
if( signature.user.id == userId ) forbiddenUser = false;
}

if(comment.user.id){ // 댓글 작성자가 존재한다면 댓글 작성자와 로그인한 사용자가 일치하는지 확인
if(comment.user.id == userId ) forbiddenUser = false;
}

if(forbiddenUser) throw new ForbiddenException('댓글 삭제 권한이 없습니다');


// 해당 댓글이 부모 댓글인 경우 자식 댓글 모두 삭제
if (commentId == comment.parentComment.id) {

// 자식 댓글 모두 찾아오기
const replyComments: SignatureCommentEntity[] = await SignatureCommentEntity.find({
where: { parentComment: { id: commentId } }
});

// 자식 댓글 모두 삭제
for (const reply of replyComments) {
await reply.softRemove();
}

// 자식 모두 삭제했으면 부모 댓글 삭제
await comment.softRemove();

}
else{ // 자식 댓글 없는 경우 본인만 삭제
await comment.softRemove();
}

return commentId;

} catch (error) {
console.log(error);
throw error;
}
}
}