Skip to content

Commit

Permalink
fix: 응답 계층 컨트롤러로 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
raymondanythings committed Jul 11, 2024
1 parent 0873f15 commit 10f2e6f
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 47 deletions.
35 changes: 24 additions & 11 deletions src/auth/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ export class AuthController {
exampleTitle: '회원 생성 완료',
},
])
@Put('signup')
@Put('')
async createUser(
@Body() createUserDTO: CreateUserDTO,
): Promise<Api<boolean>> {
if (!createUserDTO.email && !createUserDTO.phone)
throw ApiException.PLAIN_BAD_REQUEST('이메일 또는 전화번호는 필수에요.')
return this.authService.signup(createUserDTO)
await this.authService.signup(createUserDTO)
return Api.OK(true)
}

@ApiOperation({
Expand All @@ -71,7 +72,8 @@ export class AuthController {
async requestVerificationCode(
@Query() getUserByVidDTO: GetUserByVidDTO,
): Promise<Api<boolean>> {
return this.authService.requestVerificationCode(getUserByVidDTO)
await this.authService.requestVerificationCode(getUserByVidDTO)
return Api.OK(true)
}

/**
Expand All @@ -95,7 +97,8 @@ export class AuthController {
async verifyingCode(
@Body() verifyCodeDTO: VerifyCodeDTO,
): Promise<Api<boolean>> {
return this.authService.verifyingCode(verifyCodeDTO)
await this.authService.verifyingCode(verifyCodeDTO)
return Api.OK(true)
}

@ApiOperation({
Expand All @@ -115,7 +118,8 @@ export class AuthController {
async login(
@Body() loginRequestDTO: LoginRequestDTO,
): Promise<Api<VerifyingCodeResponseDTO>> {
return this.authService.login(loginRequestDTO)
const newVerifyToken = await this.authService.login(loginRequestDTO)
return Api.OK(newVerifyToken)
}

@UseGuards(AccessGuard)
Expand All @@ -133,8 +137,11 @@ export class AuthController {
])
@HttpCode(200)
@Post('refresh')
refresh(@PlainToken() token: string): Promise<Api<VerifyingCodeResponseDTO>> {
return this.authService.refresh(token)
async refresh(
@PlainToken() token: string,
): Promise<Api<VerifyingCodeResponseDTO>> {
const newToken = await this.authService.refresh(token)
return Api.OK(newToken)
}

@ApiOperation({
Expand All @@ -150,8 +157,12 @@ export class AuthController {
},
])
@Get('password-code')
getChangePasswordCode(@Query() getUserByVidDTO: GetUserByVidDTO) {
return this.authService.getChangePasswordCode(getUserByVidDTO)
async getChangePasswordCode(
@Query() getUserByVidDTO: GetUserByVidDTO,
): Promise<Api<boolean>> {
const response =
await this.authService.getChangePasswordCode(getUserByVidDTO)
return Api.OK(response)
}

@ApiOperation({
Expand All @@ -168,9 +179,11 @@ export class AuthController {
])
@HttpCode(200)
@Post('password-code')
verifyChangePasswordCode(
async verifyChangePasswordCode(
@Body() verifyCodeDTO: VerifyCodeDTO,
): Promise<Api<ChangePasswordCodeResponseDTO>> {
return this.authService.verifyChangePasswordCode(verifyCodeDTO)
const changePasswordCodeResponseDTO =
await this.authService.verifyChangePasswordCode(verifyCodeDTO)
return Api.OK(changePasswordCodeResponseDTO)
}
}
33 changes: 18 additions & 15 deletions src/auth/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import { EmailTemplateName } from '@/shared/constants/common.constant'

import { TokenPayload } from '@/shared/interfaces/token.interface'
import { AuthRepository } from '@/auth/auth.repository'
import { Api } from '@/shared/dtos/api.dto'

import { UserException } from '@/users/users.exception'
import { ChangePasswordCodeResponseDTO } from './dtos/change-password-code-response.dto'
Expand All @@ -46,7 +45,7 @@ export class AuthService {
) {}

@Transactional()
async signup(createUserDTO: CreateUserDTO): Promise<Api<boolean>> {
async signup(createUserDTO: CreateUserDTO): Promise<boolean> {
const existUser = await this.authRepository.findOne(createUserDTO)
if (existUser) {
throw UserException.CONFLICT
Expand All @@ -69,11 +68,13 @@ export class AuthService {
updatedAt: new Date(),
})

return Api.OK(true)
return true
}

@Transactional()
async requestVerificationCode(getUserByVidDTO: GetUserByVidDTO) {
async requestVerificationCode(
getUserByVidDTO: GetUserByVidDTO,
): Promise<boolean> {
const foundVerification =
await this.verificationsService.getVerificationByVid(getUserByVidDTO)

Expand All @@ -100,11 +101,11 @@ export class AuthService {
)
}

return Api.OK(true)
return true
}

@Transactional()
async verifyingCode(verifyCodeDTO: VerifyCodeDTO): Promise<Api<boolean>> {
async verifyingCode(verifyCodeDTO: VerifyCodeDTO): Promise<boolean> {
await this.verificationsService.verifyingCode(verifyCodeDTO)
const foundVerification =
await this.verificationsService.getVerificationByVid(verifyCodeDTO)
Expand All @@ -114,12 +115,12 @@ export class AuthService {
})

await this.verificationsService.removeVerification(foundVerification.id)
return Api.OK(true)
return true
}

async login(
loginRequestDTO: LoginRequestDTO,
): Promise<Api<VerifyingCodeResponseDTO>> {
): Promise<VerifyingCodeResponseDTO> {
const userWhereCond = {
email: loginRequestDTO.id,
phone: loginRequestDTO.id,
Expand All @@ -140,7 +141,7 @@ export class AuthService {
// TODO: 커스텀에러를 통해 인증되지 않은 계정의 로그인 요청 분기 필요합니다.
const newVerifyToken = await this.publishToken(foundUser.id)

return Api.OK(newVerifyToken)
return newVerifyToken
}

/**
Expand All @@ -149,7 +150,9 @@ export class AuthService {
* @param getUserByVidDTO
* @returns
*/
async getChangePasswordCode(getUserByVidDTO: GetUserByVidDTO) {
async getChangePasswordCode(
getUserByVidDTO: GetUserByVidDTO,
): Promise<boolean> {
const foundUser = await this.usersService.getUserByVid(getUserByVidDTO)
const code = this.verificationsService.generateRandomString()
if (getUserByVidDTO.type === VerificationType.PHONE) {
Expand All @@ -171,12 +174,12 @@ export class AuthService {
)
}
await this.redisService.setPasswordCode(foundUser.id, code)
return Api.OK(true)
return true
}

async verifyChangePasswordCode(
verifyCodeDTO: VerifyCodeDTO,
): Promise<Api<ChangePasswordCodeResponseDTO>> {
): Promise<ChangePasswordCodeResponseDTO> {
const foundUser = await this.usersService.getUserByVid(verifyCodeDTO)

const code = await this.redisService.findById(foundUser.id)
Expand All @@ -192,10 +195,10 @@ export class AuthService {
changePasswordKey,
)

return Api.OK(new ChangePasswordCodeResponseDTO(changePasswordKey))
return new ChangePasswordCodeResponseDTO(changePasswordKey)
}

async refresh(token: string): Promise<Api<VerifyingCodeResponseDTO>> {
async refresh(token: string): Promise<VerifyingCodeResponseDTO> {
const verifiedToken = this.verify(token, {
ignoreExpiration: true,
})
Expand All @@ -210,7 +213,7 @@ export class AuthService {

const newToken = await this.publishToken(foundUser.id)

return Api.OK(newToken)
return newToken
}

verify(
Expand Down
4 changes: 2 additions & 2 deletions src/breeds/breeds.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { BreedsRepository } from '@/breeds/breeds.repository'
export class BreedsService {
constructor(private readonly breedsRepository: BreedsRepository) {}

async findById(id: string) {
async findById(id: string): Promise<GetBreedResponseDTO> {
const foundBreeds = await this.breedsRepository.findOne(id)

if (!foundBreeds) throw new NotFoundException()

return foundBreeds
return new GetBreedResponseDTO(foundBreeds)
}

async getAllBreeds() {
Expand Down
4 changes: 2 additions & 2 deletions src/breeds/dtos/get-breed.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class GetBreedResponseDTO {
id!: string

@ApiProperty({ description: '이름', example: '나폴리탄 마스티프' })
name!: string
nameKR!: string

@ApiProperty({
description: '영어 이름',
Expand All @@ -46,7 +46,7 @@ export class GetBreedResponseDTO {

constructor(breeds: Selectable<Breed>) {
this.id = breeds.id
this.name = breeds.nameKR
this.nameKR = breeds.nameKR
this.nameEN = breeds.nameEN!
this.avatar = breeds.avatar || ''
}
Expand Down
25 changes: 18 additions & 7 deletions src/common/common.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,11 @@ export class CommonController {
async getAllGraphics(
@Query() getGraphicsRequestDTO: GetGraphicsRequestDTO,
): Promise<Api<GetGraphicsResponseDTO[]>> {
return this.graphicsService.getAllGraphicsByCategorOrType(
getGraphicsRequestDTO,
)
const getGraphicsResponseDTO =
await this.graphicsService.getAllGraphicsByCategorOrType(
getGraphicsRequestDTO,
)
return Api.OK(getGraphicsResponseDTO)
}

@Get('graphics/:id')
Expand Down Expand Up @@ -119,7 +121,10 @@ export class CommonController {
async getGraphicById(
@Param() getGraphicByIdRequestDTO: GetGraphicByIdRequestDTO,
): Promise<Api<GetGraphicsResponseDTO>> {
return this.graphicsService.getGraphicById(getGraphicByIdRequestDTO)
const response = await this.graphicsService.getGraphicById(
getGraphicByIdRequestDTO,
)
return Api.OK(response)
}

@Put('graphics')
Expand All @@ -139,7 +144,9 @@ export class CommonController {
async createGraphic(
@Body() createGraphicsDTO: CreateGraphicsDTO,
): Promise<Api<GetGraphicsResponseDTO>> {
return this.graphicsService.createGraphic(createGraphicsDTO)
const getGraphicsResponseDTO =
await this.graphicsService.createGraphic(createGraphicsDTO)
return Api.OK(getGraphicsResponseDTO)
}

@Post('graphics')
Expand All @@ -159,7 +166,10 @@ export class CommonController {
async updateGraphic(
@Body() updateGraphicsDTO: UpdateGraphicsDTO,
): Promise<Api<GetGraphicsResponseDTO>> {
return this.graphicsService.updateGraphic(updateGraphicsDTO)
const getGraphicsResponseDTO =
await this.graphicsService.updateGraphic(updateGraphicsDTO)

return Api.OK(getGraphicsResponseDTO)
}

@Delete('graphic')
Expand All @@ -175,6 +185,7 @@ export class CommonController {
},
])
async removeGraphic(@Body() removeGraphicsDTO: RemoveGraphicsDTO) {
return this.graphicsService.removeGraphic(removeGraphicsDTO)
const response = await this.graphicsService.removeGraphic(removeGraphicsDTO)
return Api.OK(response)
}
}
25 changes: 15 additions & 10 deletions src/graphics/graphics.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { GraphicsRepository } from '@/graphics/grahics.repository'

import { ExternalsService } from '@/externals/externals.service'

import { Api } from '@/shared/dtos/api.dto'
import { GetGraphicsResponseDTO } from '@/graphics/dtos/get-graphics-response.dto'
import {
GetGraphicByIdRequestDTO,
Expand All @@ -25,23 +24,27 @@ export class GraphicsService {

async getAllGraphicsByCategorOrType(
getGraphicsRequestDTO: GetGraphicsRequestDTO,
) {
): Promise<GetGraphicsResponseDTO[]> {
const allGraphics = await this.graphicsRepository.findAllByCategoryOrType(
getGraphicsRequestDTO,
)

return Api.OK(allGraphics.map((item) => new GetGraphicsResponseDTO(item)))
return allGraphics.map((item) => new GetGraphicsResponseDTO(item))
}

async getGraphicById(getGraphicByIdRequestDTO: GetGraphicByIdRequestDTO) {
async getGraphicById(
getGraphicByIdRequestDTO: GetGraphicByIdRequestDTO,
): Promise<GetGraphicsResponseDTO> {
const foundGraphic = await this.graphicsRepository.findOne(
getGraphicByIdRequestDTO.id,
)
return Api.OK(new GetGraphicsResponseDTO(foundGraphic))
return new GetGraphicsResponseDTO(foundGraphic)
}

@Transactional()
async createGraphic(createGraphicsDTO: CreateGraphicsDTO) {
async createGraphic(
createGraphicsDTO: CreateGraphicsDTO,
): Promise<GetGraphicsResponseDTO> {
const isLottieFile = createGraphicsDTO.file.mimeType.includes('json')

const isExistName = await this.graphicsRepository.findOneByName(
Expand All @@ -63,11 +66,13 @@ export class GraphicsService {
type: isLottieFile ? GraphicType.Lottie : GraphicType.GIF,
url: graphicUrl[0],
})
return Api.OK(new GetGraphicsResponseDTO(res[0]))
return new GetGraphicsResponseDTO(res[0])
}

@Transactional()
async updateGraphic(updateGraphicsDTO: UpdateGraphicsDTO) {
async updateGraphic(
updateGraphicsDTO: UpdateGraphicsDTO,
): Promise<GetGraphicsResponseDTO> {
const foundGraphic = await this.graphicsRepository.findOne(
updateGraphicsDTO.id,
)
Expand Down Expand Up @@ -100,12 +105,12 @@ export class GraphicsService {
...updateValues,
})

return Api.OK(new GetGraphicsResponseDTO(updateResponse))
return new GetGraphicsResponseDTO(updateResponse)
}

@Transactional()
async removeGraphic(removeGraphicsDTO: RemoveGraphicsDTO) {
await this.graphicsRepository.removeGraphic(removeGraphicsDTO.id)
return Api.OK(true)
return true
}
}

0 comments on commit 10f2e6f

Please sign in to comment.