-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #109 from modern-agile-team/feature/comment
Add(2swo/comment-reComment) 대댓글 기능 추가
- Loading branch information
Showing
18 changed files
with
555 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class CreateReCommentDto { | ||
@IsString() | ||
content: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { UserImage } from 'src/users/entities/user-image.entity'; | ||
|
||
export class reCommentResponseDTO { | ||
id: number; | ||
content: string; | ||
reCommentowner: boolean; | ||
user: { | ||
name: string; | ||
userImage: UserImage | UserImage[]; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { IsString } from 'class-validator'; | ||
|
||
export class UpdateReCommentDto { | ||
@IsString() | ||
content: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { EntityManager } from 'typeorm'; | ||
import { ReComment } from '../entities/recomment.entity'; | ||
import { CreateReCommentDto } from '../dto/create-recomment-dto'; | ||
|
||
@Injectable() | ||
export class ReCommentsRepository { | ||
constructor(private readonly entityManager: EntityManager) {} | ||
async createReComment( | ||
recommentData: CreateReCommentDto, | ||
userId: number, | ||
commentId: number, | ||
): Promise<ReComment> { | ||
const reComment = new ReComment(); | ||
reComment.content = recommentData.content; | ||
reComment.userId = userId; | ||
reComment.commentId = commentId; | ||
return await this.entityManager.save(ReComment, reComment); | ||
} | ||
|
||
async findReCommentByCommentId(commentId: number): Promise<ReComment[]> { | ||
const query = this.entityManager | ||
.createQueryBuilder(ReComment, 'reComment') | ||
.innerJoinAndSelect('reComment.user', 'user') | ||
.innerJoinAndSelect('user.userImage', 'userImage') | ||
.where('reComment.commentId = :commentId', { commentId }); | ||
return query.getMany(); | ||
} | ||
|
||
async findOneReComment(id: number): Promise<ReComment> { | ||
return this.entityManager.findOne(ReComment, { | ||
relations: ['user', 'user.userImage'], | ||
where: { id }, | ||
}); | ||
} | ||
|
||
async updateReComment( | ||
id: number, | ||
commentData: Partial<CreateReCommentDto>, | ||
): Promise<ReComment> { | ||
const existingReComment = await this.entityManager.findOne(ReComment, { | ||
relations: ['user', 'user.userImage'], | ||
where: { id }, | ||
}); | ||
for (const key in commentData) { | ||
if (commentData.hasOwnProperty(key)) { | ||
existingReComment[key] = commentData[key]; | ||
} | ||
} | ||
await this.entityManager.save(ReComment, existingReComment); | ||
return existingReComment; | ||
} | ||
|
||
async deleteReComment(reComment: ReComment): Promise<void> { | ||
await this.entityManager.remove(ReComment, reComment); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { CreateReCommentDto } from '../dto/create-recomment-dto'; | ||
import { ReComment } from '../entities/recomment.entity'; | ||
import { ReCommentsRepository } from '../repository/recomments.repository'; | ||
import { UpdateReCommentDto } from '../dto/update-recomment-dto'; | ||
import { reCommentResponseDTO } from '../dto/get-all-recomment-dto'; | ||
|
||
@Injectable() | ||
export class ReCommentsService { | ||
constructor(private reCommentRepository: ReCommentsRepository) {} | ||
async create( | ||
recommentData: CreateReCommentDto, | ||
userId: number, | ||
commentId: number, | ||
): Promise<ReComment> { | ||
try { | ||
return await this.reCommentRepository.createReComment( | ||
recommentData, | ||
userId, | ||
commentId, | ||
); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
} | ||
|
||
async updateReComment( | ||
reCommentId: number, | ||
reCommentData: Partial<UpdateReCommentDto>, | ||
): Promise<ReComment | undefined> { | ||
const existingComment = | ||
await this.reCommentRepository.findOneReComment(reCommentId); | ||
for (const key in reCommentData) { | ||
if (reCommentData.hasOwnProperty(key)) { | ||
existingComment[key] = reCommentData[key]; | ||
} | ||
} | ||
const updatedReComment = await this.reCommentRepository.updateReComment( | ||
reCommentId, | ||
existingComment, | ||
); | ||
return updatedReComment; | ||
} | ||
|
||
async findAllReComments( | ||
commentId: number, | ||
userId: number, | ||
): Promise<reCommentResponseDTO[]> { | ||
const reComments = | ||
await this.reCommentRepository.findReCommentByCommentId(commentId); | ||
if (!reComments) { | ||
return []; | ||
} | ||
return reComments.map((reComment) => ({ | ||
id: reComment.id, | ||
content: reComment.content, | ||
reCommentowner: reComment.userId === userId, | ||
user: { | ||
name: reComment.user.name, | ||
userImage: reComment.user.userImage ? reComment.user.userImage : [], | ||
}, | ||
})); | ||
} | ||
|
||
async deleteReComment(reCommentId: number, userId: number): Promise<void> { | ||
const reComment = | ||
await this.reCommentRepository.findOneReComment(reCommentId); | ||
|
||
if (!reComment) { | ||
throw new Error('존재하지 않는 댓글입니다.'); | ||
} | ||
|
||
if (reComment.userId !== userId) { | ||
throw new Error('작성한 댓글이 아닙니다.'); | ||
} | ||
await this.reCommentRepository.deleteReComment(reComment); | ||
} | ||
} |
Oops, something went wrong.