오늘은 굉장히 많은 오류가 있었따.
간단 한 것론
async DeleteComment(cardId: number, id: number, user_Id: number) {
// 코멘트를 삭제하기 전에 해당 코멘트가 유저의 것인지 확인할 수 있는 로직을 추가할 수 있습니다.
const existingComment = await this.commentRepository
.createQueryBuilder('comment')
.where('comment.id = :id', { id })
.andWhere('comment.user_Id = :user_Id', { user_Id }) // 'user_Id' 필드 대신 'userId'를 사용합니다.
.getOne();
if (!existingComment) {
console.log(user_Id);
throw new NotFoundException('코멘트를 찾을 수 없거나 권한이 없습니다.');
}
await this.commentRepository.delete(id);
} 이 코드와 //카드 삭제
@Delete('/:commentId')
@UseGuards(AuthGuard)
async DeleteCard(@Query('cardId') cardId: number, @Param('commentId') id: number, @GetUser() user: AccessPayload) {
return await this.commentsService.DeleteComment(cardId, id, user.Id);
}
// 컨트롤러
@Get('/:commentId')
async GetCommentById(@Query('cardId') cardId: number, @Param(':commentId') id: number) {
return await this.commentsService.GetCommentById(id);
}
이 코드에서 commentId 를 똑바로 불러오지 못하는 버그가 있었는데
컨트롤러 쩍어서 @Param(':commentId') 라고 쓰는 바람에 오류가 났다,
@Get('/:commentId')
async GetCommentById(@Query('cardId') cardId: number, @Param('commentId') id: number) {
return await this.commentsService.GetCommentById(id);
}
:commentId 에서 :를 빼는 것으로 해결
그리고 코멘트 쪽에서도
async CreateComment(cardId: number, userId: number, reply_id: number, comment: string) {
const card = await this.cardsService.GetCards(cardId);
이 코드에서 해당 컬럼을 찾지 못한다는 오류가 발생했는데 GetCards를 확인해보니
async GetCards(board_column_Id: number) {
const boardColumn = await this.boardColumnService.findOneBoardColumnById(board_column_Id);
const findCards = await this.cardRepository.find({ relations: ['board_column'] });
if (!boardColumn) throw new NotFoundException('해당 칼럼은 존재하지 않습니다.');
const cards = findCards.filter((card) => {
return card.board_column.id == board_column_Id;
});
이런 식으로 보드 칼럼 아이디를 가져와줘야하는데 가져 오지 않아서 생기는 오류 였다.
그리고 Getcards는 모드 카드를 가져오는 것이기때문에 해당 카드만 가져올 수 있는 GetCardById롤 수정 했으며
// 코멘트 생성
async CreateComment(board_column_Id: number, cardId: number, userId: number, reply_id: number, comment: string) {
const card = await this.cardsService.GetCardById(board_column_Id, cardId);
if (!card) {
throw new NotFoundException('카드을 찾을 수 없습니다.');
}
보드 칼럼 아이디도 가져오도록 코드를 수정해서 해결했다.