Skip to content

Commit

Permalink
[GWL-83] Record Swagger 리팩토링 (#114)
Browse files Browse the repository at this point in the history
* refacotr: dto 리팩토링

* chore: format 적용
  • Loading branch information
sjy982 authored Nov 23, 2023
1 parent a475d0f commit 1976bce
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 57 deletions.
9 changes: 1 addition & 8 deletions BackEnd/src/auth/dto/auth-response.dto.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
import { ApiProperty, PickType } from '@nestjs/swagger';

class SuccessResDto {
@ApiProperty({ description: '에러가 없는 경우 null' })
code: number;

@ApiProperty({ description: '에러가 없는 경우 null' })
errorMessage: string;
}
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';

class Token {
@ApiProperty({
Expand Down
17 changes: 17 additions & 0 deletions BackEnd/src/common/dto/SuccessRes.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { ApiProperty } from '@nestjs/swagger';

export class SuccessResDto {
@ApiProperty({
example: null,
description: '에러 코드 성공시 null',
nullable: true,
})
code: number | null;

@ApiProperty({
example: null,
description: '에러 코드 성공시 null',
nullable: true,
})
errorMessage: string | null;
}
17 changes: 0 additions & 17 deletions BackEnd/src/records/dto/create-recordResponse.dto.ts

This file was deleted.

13 changes: 0 additions & 13 deletions BackEnd/src/records/dto/get-recordResponse.dto.ts

This file was deleted.

31 changes: 31 additions & 0 deletions BackEnd/src/records/dto/record-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { ApiProperty, PickType } from '@nestjs/swagger';
import { SuccessResDto } from 'src/common/dto/SuccessRes.dto';
import { RecordModel } from '../entities/records.entity';

class GetRecord extends PickType(RecordModel, [
'id',
'workout',
'profile',
'workoutTime',
'distance',
'calorie',
'avgHeartRate',
'minHeartRate',
'maxHeartRate',
'createdAt',
]) {}

export class CreateRecordResDto extends SuccessResDto {
@ApiProperty({ example: 1, description: '운동 기록 레코드 ID' })
recordId: number;
}

export class GetUsersRecordsResDto extends SuccessResDto {
@ApiProperty({ type: () => [GetRecord] })
data: GetRecord[];
}

export class GetRecordResDto extends SuccessResDto {
@ApiProperty({ type: () => GetRecord })
data: GetRecord;
}
38 changes: 30 additions & 8 deletions BackEnd/src/records/entities/records.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,69 @@ import {
import { PostModel } from '../../posts/entities/posts.entity';
import { ProfileModel } from '../../profiles/entities/profiles.entity';
import { IsNumber, IsString } from 'class-validator';
import {ApiProperty} from "@nestjs/swagger";
import { ApiProperty } from '@nestjs/swagger';
@Entity()
export class RecordModel {
@PrimaryGeneratedColumn()
id: number;

@ApiProperty({ example: 'biking', description: '선택한 운동 종료' })
@ApiProperty({ example: '자전거', description: '선택한 운동 종료' })
@Column()
@IsString()
workout: string;

@ApiProperty({ example: '6000000', description: '운동을 한 시간 (초로 환산)' })
@ApiProperty({
example: '6000000',
description: '운동을 한 시간 (초로 환산)',
})
@Column()
@IsNumber()
workoutTime: number;

@ApiProperty( {example: '100000', description: '운동을 한 거리 (미터로 환산)'} )
@ApiProperty({
example: '100000',
description: '운동을 한 거리 (미터로 환산)',
})
@Column()
@IsNumber()
distance: number;

@ApiProperty( {example: '360', description: '운동에 소모한 칼로리 (kcal 기준)'} )
@ApiProperty({
example: '360',
description: '운동에 소모한 칼로리 (kcal 기준)',
})
@Column()
@IsNumber()
calorie: number;

@ApiProperty({example: '60', description: '운동 중 평균 심박수 (평균 기준)'})
@ApiProperty({
example: '60',
description: '운동 중 평균 심박수 (평균 기준)',
})
@Column({ nullable: true })
@IsNumber()
avgHeartRate: number;

@ApiProperty( {example: '120', description: '운동 중 최대 심박수 (최대 기준)'} )
@ApiProperty({
example: '120',
description: '운동 중 최대 심박수 (최대 기준)',
})
@Column({ nullable: true })
@IsNumber()
minHeartRate: number;

@ApiProperty( {example: '180', description: '운동 중 최소 심박수 (최소 기준)'} )
@ApiProperty({
example: '180',
description: '운동 중 최소 심박수 (최소 기준)',
})
@Column({ nullable: true })
@IsNumber()
maxHeartRate: number;

@ApiProperty({
example: 'YYYY-MM-DD hh:mm:ss',
description: '운동 기록 생성 날짜',
})
@CreateDateColumn()
createdAt: Date;

Expand Down
29 changes: 18 additions & 11 deletions BackEnd/src/records/records.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,26 @@ import { CreateExerciseLogDto } from './dto/create-exerciseLog.dto';
import { AccessTokenGuard } from 'src/auth/guard/bearerToken.guard';
import { Profile } from 'src/profiles/decorator/profile.decorator';
import { ProfileModel } from 'src/profiles/entities/profiles.entity';
import {ApiBody, ApiCreatedResponse, ApiOperation, ApiTags} from "@nestjs/swagger";
import {CreateRecordDataDto} from "./dto/create-recordResponse.dto";
import {RecordDataDto} from "./dto/get-recordResponse.dto";

import {
ApiBody,
ApiCreatedResponse,
ApiOperation,
ApiTags,
} from '@nestjs/swagger';
import {
CreateRecordResDto,
GetRecordResDto,
GetUsersRecordsResDto,
} from './dto/record-response.dto';

@ApiTags('사용자 기록 API')
@Controller('api/v1/records')
export class RecordsController {
constructor(private readonly recordsService: RecordsService) {}
@Post()
@ApiOperation({summary: '운동 기록 생성'})
@ApiBody({type: CreateExerciseLogDto})
@ApiCreatedResponse({type: CreateRecordDataDto})
@ApiOperation({ summary: '운동 기록 생성' })
@ApiBody({ type: CreateExerciseLogDto })
@ApiCreatedResponse({ type: CreateRecordResDto })
@UseGuards(AccessTokenGuard)
async createWorkOutLog(
@Profile() profile: ProfileModel,
Expand All @@ -30,16 +37,16 @@ export class RecordsController {
}

@Get('me')
@ApiOperation({summary: '내 모든 운동 기록 조회'})
@ApiCreatedResponse({type: RecordDataDto})
@ApiOperation({ summary: '내 모든 운동 기록 조회' })
@ApiCreatedResponse({ type: GetUsersRecordsResDto })
@UseGuards(AccessTokenGuard)
async getUserRecords(@Profile() profile: ProfileModel) {
return this.recordsService.findByProfileId(profile.id);
}

@Get(':recordId')
@ApiOperation({summary: '하나의 기록 조회'})
@ApiCreatedResponse({type: RecordDataDto})
@ApiOperation({ summary: '하나의 기록 조회' })
@ApiCreatedResponse({ type: GetRecordResDto })
async getRecord(@Param('recordId') recordId: number) {
return this.recordsService.findById(recordId);
}
Expand Down

0 comments on commit 1976bce

Please sign in to comment.