Skip to content

Commit c342f8e

Browse files
authored
Merge pull request #451 from boostcampwm-2024/refactor/api
♻️ refactor: API Response, Request DTO 구현, ApiProperty 추가
2 parents 657c786 + 8b579c1 commit c342f8e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+931
-411
lines changed

server/src/activity/controller/activity.controller.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
import { Controller, Get, Param, Query } from '@nestjs/common';
1+
import {
2+
Controller,
3+
Get,
4+
HttpCode,
5+
HttpStatus,
6+
Param,
7+
Query,
8+
} from '@nestjs/common';
29
import { ApiTags } from '@nestjs/swagger';
3-
410
import { ActivityService } from '../service/activity.service';
511
import { ApiResponse } from '../../common/response/common.response';
612
import { ActivityParamRequestDto } from '../dto/request/activity-param.dto';
@@ -14,6 +20,7 @@ export class ActivityController {
1420

1521
@ApiReadActivities()
1622
@Get(':userId')
23+
@HttpCode(HttpStatus.OK)
1724
async readActivities(
1825
@Param() paramDto: ActivityParamRequestDto,
1926
@Query() queryDto: ActivityQueryRequestDto,

server/src/activity/dto/request/activity-param.dto.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export class ActivityParamRequestDto {
1010
@IsInt({
1111
message: '정수를 입력해주세요.',
1212
})
13-
@Min(1, { message: '사용자 ID는 1보다 커야합니다.' })
13+
@Min(1, { message: '사용자 ID는 1 이상이어야 합니다.' })
1414
@Type(() => Number)
1515
userId: number;
1616

server/src/activity/dto/response/activity-read.dto.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
import { ApiProperty } from '@nestjs/swagger';
2+
import { User } from '../../../user/entity/user.entity';
23

34
export class DailyActivityDto {
4-
@ApiProperty({
5-
example: '2024-01-15',
6-
description: '활동 날짜 (YYYY-MM-DD)',
7-
})
85
date: string;
9-
10-
@ApiProperty({
11-
example: 5,
12-
description: '해당 날짜의 조회수',
13-
})
146
viewCount: number;
157

168
constructor(partial: Partial<DailyActivityDto>) {
@@ -43,7 +35,16 @@ export class ActivityReadResponseDto {
4335
})
4436
totalViews: number;
4537

46-
constructor(partial: Partial<ActivityReadResponseDto>) {
38+
private constructor(partial: Partial<ActivityReadResponseDto>) {
4739
Object.assign(this, partial);
4840
}
41+
42+
static toResponseDto(dailyActivities: DailyActivityDto[], user: User) {
43+
return new ActivityReadResponseDto({
44+
dailyActivities: dailyActivities,
45+
maxStreak: user.maxStreak,
46+
currentStreak: user.currentStreak,
47+
totalViews: user.totalViews,
48+
});
49+
}
4950
}

server/src/activity/service/activity.service.ts

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
1-
import { Injectable, NotFoundException } from '@nestjs/common';
1+
import { Injectable } from '@nestjs/common';
22
import { ActivityRepository } from '../repository/activity.repository';
3-
import { UserRepository } from '../../user/repository/user.repository';
43
import {
54
ActivityReadResponseDto,
65
DailyActivityDto,
76
} from '../dto/response/activity-read.dto';
7+
import { UserService } from '../../user/service/user.service';
88

99
@Injectable()
1010
export class ActivityService {
1111
constructor(
1212
private readonly activityRepository: ActivityRepository,
13-
private readonly userRepository: UserRepository,
13+
private readonly userService: UserService,
1414
) {}
1515

1616
async readActivities(
1717
userId: number,
1818
year: number,
1919
): Promise<ActivityReadResponseDto> {
20-
const user = await this.userRepository.findOneBy({ id: userId });
21-
if (!user) {
22-
throw new NotFoundException('존재하지 않는 사용자입니다.');
23-
}
20+
const user = await this.userService.getUser(userId);
2421

2522
const activities =
2623
await this.activityRepository.findActivitiesByUserIdAndYear(userId, year);
@@ -33,12 +30,7 @@ export class ActivityService {
3330
}),
3431
);
3532

36-
return new ActivityReadResponseDto({
37-
dailyActivities,
38-
maxStreak: user.maxStreak,
39-
currentStreak: user.currentStreak,
40-
totalViews: user.totalViews,
41-
});
33+
return ActivityReadResponseDto.toResponseDto(dailyActivities, user);
4234
}
4335

4436
async upsertActivity(userId: number) {

server/src/admin/controller/admin.controller.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ export class AdminController {
5757
@ApiCreateAdmin()
5858
@UseGuards(AdminAuthGuard)
5959
@Post('/register')
60+
@HttpCode(HttpStatus.CREATED)
6061
async createAdmin(@Body() registerAdminBodyDto: RegisterAdminRequestDto) {
6162
await this.adminService.createAdmin(registerAdminBodyDto);
6263
return ApiResponse.responseWithNoContent(

server/src/chat/service/chat.service.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const CHAT_HISTORY_LIMIT = 20;
1111

1212
@Injectable()
1313
export class ChatService {
14-
private dayInit: boolean = false;
15-
1614
constructor(private readonly redisService: RedisService) {}
1715

1816
isMaxClientExceeded(userCount: number) {

server/src/comment/dto/request/create-comment.dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
2+
import { IsInt, IsNotEmpty, IsString, Min } from 'class-validator';
33

44
export class CreateCommentRequestDto {
55
@ApiProperty({
@@ -13,13 +13,13 @@ export class CreateCommentRequestDto {
1313
comment: string;
1414

1515
@ApiProperty({
16-
example: '1',
16+
example: 1,
1717
description: '게시글 번호를 입력해주세요.',
1818
})
1919
@IsInt({
2020
message: '숫자로 입력해주세요.',
2121
})
22-
@IsNotEmpty({ message: '피드 아이디를 입력하세요.' })
22+
@Min(1, { message: '게시글 ID는 1 이상이어야 합니다.' })
2323
feedId: number;
2424

2525
constructor(partial: Partial<CreateCommentRequestDto>) {

server/src/comment/dto/request/delete-comment.dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsInt, IsNotEmpty } from 'class-validator';
2+
import { IsInt, Min } from 'class-validator';
33

44
export class DeleteCommentRequestDto {
55
@ApiProperty({
6-
example: '1',
6+
example: 1,
77
description: '댓글 번호를 입력해주세요.',
88
})
99
@IsInt({
1010
message: '숫자로 입력해주세요.',
1111
})
12-
@IsNotEmpty({ message: '댓글 아이디를 입력하세요.' })
12+
@Min(1, { message: '댓글 ID는 1 이상이어야 합니다.' })
1313
commentId: number;
1414

1515
constructor(partial: Partial<DeleteCommentRequestDto>) {

server/src/comment/dto/request/get-comment.dto.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import { ApiProperty } from '@nestjs/swagger';
22
import { Type } from 'class-transformer';
3-
import { IsInt } from 'class-validator';
3+
import { IsInt, Min } from 'class-validator';
44

55
export class GetCommentRequestDto {
66
@ApiProperty({
7-
example: '게시글 ID',
7+
example: 1,
88
description: '게시글 ID를 입력해주세요',
99
})
1010
@IsInt({
1111
message: '숫자로 입력해주세요.',
1212
})
1313
@Type(() => Number)
14+
@Min(1, { message: '댓글 ID는 1 이상이어야 합니다.' })
1415
feedId: number;
1516

1617
constructor(partial: Partial<GetCommentRequestDto>) {

server/src/comment/dto/request/update-comment.dto.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { ApiProperty } from '@nestjs/swagger';
2-
import { IsInt, IsNotEmpty, IsString } from 'class-validator';
2+
import { IsInt, IsNotEmpty, IsString, Min } from 'class-validator';
33

44
export class UpdateCommentRequestDto {
55
@ApiProperty({
6-
example: '댓글 번호',
6+
example: 1,
77
description: '댓글 번호를 입력해주세요.',
88
})
99
@IsInt({
1010
message: '정수로 입력하세요.',
1111
})
12-
@IsNotEmpty({ message: '댓글 아이디를 입력하세요.' })
12+
@Min(1, { message: '댓글 ID는 1 이상이어야 합니다.' })
1313
commentId: number;
1414

1515
@ApiProperty({

0 commit comments

Comments
 (0)