-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
55a49f9
commit c82f548
Showing
15 changed files
with
224 additions
and
17 deletions.
There are no files selected for viewing
46 changes: 40 additions & 6 deletions
46
apps/admin-api/src/api/article/comment/comment.controller.ts
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 |
---|---|---|
@@ -1,24 +1,58 @@ | ||
import { Controller, Delete, Get, Param, Post } from '@nestjs/common'; | ||
import { | ||
Body, | ||
Controller, | ||
Delete, | ||
Get, | ||
Param, | ||
Post, | ||
SerializeOptions, | ||
} from '@nestjs/common'; | ||
import { ApiTags } from '@nestjs/swagger'; | ||
import { CurrentUser } from '@repo/api'; | ||
import { ApiAuth } from '@repo/api/decorators/http.decorators'; | ||
import { CommentService } from './comment.service'; | ||
import { CommentListResDto } from './dto/comment-list.dto'; | ||
import { CommentResDto } from './dto/comment.dto'; | ||
import { CreateCommentReqDto } from './dto/create-comment.dto'; | ||
|
||
@ApiTags('Comment') | ||
@Controller('articles/:slug/comments') | ||
export class CommentController { | ||
constructor(private readonly commentService: CommentService) {} | ||
|
||
@Post() | ||
async create(@Param('slug') slug: string) { | ||
return await this.commentService.create(slug); | ||
@SerializeOptions({ type: CommentResDto }) | ||
@ApiAuth({ | ||
summary: 'Add Comments to an Article', | ||
type: CommentResDto, | ||
}) | ||
async create( | ||
@Param('slug') slug: string, | ||
@Body('comment') commentData: CreateCommentReqDto, | ||
@CurrentUser('id') userId: number, | ||
): Promise<CommentResDto> { | ||
return await this.commentService.create(slug, commentData, userId); | ||
} | ||
|
||
@Get() | ||
async list(@Param('slug') slug: string) { | ||
@SerializeOptions({ type: CommentListResDto }) | ||
@ApiAuth({ | ||
summary: 'Get Comments from an Article', | ||
type: CommentListResDto, | ||
isAuthOptional: true, | ||
}) | ||
async list(@Param('slug') slug: string): Promise<CommentListResDto> { | ||
return await this.commentService.list(slug); | ||
} | ||
|
||
@Delete(':id') | ||
async delete(@Param('slug') slug: string, @Param('id') id: string) { | ||
return await this.commentService.delete(slug, id); | ||
@ApiAuth({ | ||
summary: 'Delete Comment', | ||
}) | ||
async delete( | ||
@CurrentUser('id') userId: number, | ||
@Param('id') commentId: number, | ||
) { | ||
return await this.commentService.delete(commentId, userId); | ||
} | ||
} |
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
32 changes: 31 additions & 1 deletion
32
apps/admin-api/src/api/article/comment/comment.service.spec.ts
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 |
---|---|---|
@@ -1,16 +1,92 @@ | ||
import { ProfileDto } from '@/api/profile/dto/profile.dto'; | ||
import { ErrorCode } from '@/constants/error-code.constant'; | ||
import { Injectable } from '@nestjs/common'; | ||
import { InjectRepository } from '@nestjs/typeorm'; | ||
import { ValidationException } from '@repo/api'; | ||
import { | ||
ArticleEntity, | ||
CommentEntity, | ||
UserEntity, | ||
} from '@repo/database-typeorm'; | ||
import { Repository } from 'typeorm'; | ||
import { CommentListResDto } from './dto/comment-list.dto'; | ||
import { CommentResDto } from './dto/comment.dto'; | ||
import { CreateCommentReqDto } from './dto/create-comment.dto'; | ||
|
||
@Injectable() | ||
export class CommentService { | ||
async create(_slug: string) { | ||
throw new Error('Method not implemented.'); | ||
constructor( | ||
@InjectRepository(ArticleEntity) | ||
private readonly articleRepository: Repository<ArticleEntity>, | ||
@InjectRepository(UserEntity) | ||
private readonly userRepository: Repository<UserEntity>, | ||
@InjectRepository(CommentEntity) | ||
private readonly commentRepository: Repository<CommentEntity>, | ||
) {} | ||
|
||
async create( | ||
slug: string, | ||
commentData: CreateCommentReqDto, | ||
userId: number, | ||
): Promise<CommentResDto> { | ||
const article = await this.articleRepository.findOneBy({ slug: slug }); | ||
|
||
if (!article) { | ||
throw new ValidationException(ErrorCode.E201); | ||
} | ||
|
||
const user = await this.userRepository.findOneByOrFail({ id: userId }); | ||
const comment = new CommentEntity({ | ||
body: commentData.body, | ||
articleId: article.id, | ||
authorId: userId, | ||
}); | ||
|
||
const savedComment = await this.commentRepository.save(comment); | ||
|
||
return { | ||
comment: { | ||
...savedComment, | ||
author: user.toDto(ProfileDto), | ||
}, | ||
}; | ||
} | ||
|
||
async list(_slug: string) { | ||
throw new Error('Method not implemented.'); | ||
async list(slug: string): Promise<CommentListResDto> { | ||
const article = await this.articleRepository.findOneBy({ slug: slug }); | ||
|
||
if (!article) { | ||
throw new ValidationException(ErrorCode.E201); | ||
} | ||
|
||
const comments = await this.commentRepository.find({ | ||
where: { articleId: article.id }, | ||
relations: ['author'], | ||
}); | ||
|
||
return { | ||
comments: comments.map((comment) => { | ||
return { | ||
...comment, | ||
author: comment.author.toDto(ProfileDto), | ||
}; | ||
}), | ||
}; | ||
} | ||
|
||
async delete(_slug: string, _id: string) { | ||
throw new Error('Method not implemented.'); | ||
async delete(commentId: number, userId: number) { | ||
const comment = await this.commentRepository.findOneBy({ | ||
id: commentId, | ||
}); | ||
|
||
if (!comment) { | ||
throw new ValidationException(ErrorCode.E301); | ||
} | ||
|
||
if (comment.authorId !== userId) { | ||
throw new ValidationException(ErrorCode.E302); | ||
} | ||
|
||
await this.commentRepository.remove(comment); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
apps/admin-api/src/api/article/comment/dto/comment-list.dto.ts
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,7 @@ | ||
import { ClassField } from '@repo/api'; | ||
import { CommentDto } from './comment.dto'; | ||
|
||
export class CommentListResDto { | ||
@ClassField(() => CommentDto, { isArray: true, expose: true }) | ||
comments: CommentDto[]; | ||
} |
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,24 @@ | ||
import { ProfileDto } from '@/api/profile/dto/profile.dto'; | ||
import { ClassField, DateField, NumberField, StringField } from '@repo/api'; | ||
|
||
export class CommentDto { | ||
@NumberField({ expose: true }) | ||
id: number; | ||
|
||
@StringField({ expose: true }) | ||
body: string; | ||
|
||
@DateField({ expose: true }) | ||
createdAt: Date; | ||
|
||
@DateField({ expose: true }) | ||
updatedAt: Date; | ||
|
||
@ClassField(() => ProfileDto, { expose: true }) | ||
author: ProfileDto; | ||
} | ||
|
||
export class CommentResDto { | ||
@ClassField(() => CommentDto, { expose: true }) | ||
comment: CommentDto; | ||
} |
6 changes: 6 additions & 0 deletions
6
apps/admin-api/src/api/article/comment/dto/create-comment.dto.ts
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 { StringField } from '@repo/api'; | ||
|
||
export class CreateCommentReqDto { | ||
@StringField({ minLength: 1, maxLength: 1000 }) | ||
readonly body: 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
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