Skip to content

Commit

Permalink
Merge pull request #18 from techeer-sv/feat/#11
Browse files Browse the repository at this point in the history
feat/#11
  • Loading branch information
suhyeon0921 authored Sep 26, 2023
2 parents 7db7d72 + 98b1681 commit e03f9e8
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 8 deletions.
17 changes: 17 additions & 0 deletions Mindspace_back/src/global/common/customException.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { HttpException, HttpStatus } from '@nestjs/common';

export class CustomException extends HttpException {
constructor(
private readonly _errorCode: string,
message: string,
status: HttpStatus,
) {
super(
{
errorCode: _errorCode,
message: message,
},
status,
);
}
}
3 changes: 3 additions & 0 deletions Mindspace_back/src/user/dto/user-nickname-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export class UserNicknameResponseDto {
constructor(public nickname: string) {}
}
7 changes: 7 additions & 0 deletions Mindspace_back/src/user/dto/user.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@nestjs/common';
import { User } from '../entities/user.entity';
import { UserSignupRequestDto } from './user-signup-request.dto';
import { UserResponseDto } from './user-response.dto';
import { UserNicknameResponseDto } from './user-nickname-response.dto';

@Injectable()
export class UserMapper {
Expand All @@ -21,4 +22,10 @@ export class UserMapper {
nickname: user.nickname,
};
}

nicknameDtoFromEntity(user: User): UserNicknameResponseDto {
return {
nickname: user.nickname,
};
}
}
32 changes: 32 additions & 0 deletions Mindspace_back/src/user/exception/errorResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { HttpStatus } from '@nestjs/common';
import { CustomException } from '../../global/common/customException';

export class UserNotFoundException extends CustomException {
constructor() {
super('U001', '해당 유저를 찾을 수 없습니다.', HttpStatus.NOT_FOUND);
}
}

export class UserInvalidPasswordException extends CustomException {
constructor() {
super('U002', '비밀번호를 다시 확인해주세요.', HttpStatus.BAD_REQUEST);
}
}

export class UserNicknameDuplicatedException extends CustomException {
constructor() {
super('U003', '해당 nickname는 사용 중입니다.', HttpStatus.CONFLICT);
}
}

export class UserEmailDuplicatedException extends CustomException {
constructor() {
super('U004', '이미 해당 Email로 회원가입했습니다.', HttpStatus.CONFLICT);
}
}

export class UserLoginRequiredException extends CustomException {
constructor() {
super('U005', '로그인이 필요합니다.', HttpStatus.UNAUTHORIZED);
}
}
25 changes: 21 additions & 4 deletions Mindspace_back/src/user/user.controller.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
import { Controller, Post, Get, Body, HttpStatus, Res } from '@nestjs/common';
import { UserService } from './user.service';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import {
Controller,
Post,
Get,
Body,
HttpStatus,
Res,
Headers,
} from '@nestjs/common';
import { Response } from 'express';
import { UserMapper } from './dto/user.mapper';
import { UserSignupRequestDto } from './dto/user-signup-request.dto';
import { UserLoginRequestDto } from './dto/user-login-request.dto';
import { Response } from 'express';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { UserNicknameResponseDto } from './dto/user-nickname-response.dto';
import { UserService } from './user.service';

@ApiTags('User')
@Controller('api/v1/user')
Expand Down Expand Up @@ -44,4 +53,12 @@ export class UserController {
const users = await this.userService.getAllUser();
res.status(HttpStatus.OK).json(users);
}

@ApiOperation({ summary: '닉네임 반환' })
@Get('/nickname')
async getUserNickname(
@Headers('authorization') userId: number,
): Promise<UserNicknameResponseDto> {
return this.userService.getUserNickname(userId);
}
}
43 changes: 39 additions & 4 deletions Mindspace_back/src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
import { Injectable } from '@nestjs/common';
import { Injectable, NotFoundException } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from './entities/user.entity';
import { UserSignupRequestDto } from './dto/user-signup-request.dto';
import { UserLoginRequestDto } from './dto/user-login-request.dto';
import { UserMapper } from './dto/user.mapper';
import { UserNicknameResponseDto } from './dto/user-nickname-response.dto';
import {
UserEmailDuplicatedException,
UserInvalidPasswordException,
UserNicknameDuplicatedException,
UserNotFoundException,
} from './exception/errorResponse';

@Injectable()
export class UserService {
constructor(
@InjectRepository(User) private readonly userRepository: Repository<User>,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
private readonly userMapper: UserMapper,
) {}

async signupUser(userSignupRequestDto: UserSignupRequestDto): Promise<User> {
const emailExists = await this.userRepository.findOne({
where: { email: userSignupRequestDto.email },
});
if (emailExists) {
throw new UserEmailDuplicatedException();
}

const nicknameExists = await this.userRepository.findOne({
where: { nickname: userSignupRequestDto.nickname },
});
if (nicknameExists) {
throw new UserNicknameDuplicatedException();
}

const userEntity = this.userMapper.DtoToEntity(userSignupRequestDto);
return await this.userRepository.save(userEntity);
}
Expand All @@ -24,11 +46,11 @@ export class UserService {
});

if (!user) {
throw new Error('해당 이메일로 가입한 사용자 없음');
throw new UserNotFoundException();
}

if (user.password !== userLoginRequestDto.password) {
throw new Error('비밀번호 오류');
throw new UserInvalidPasswordException();
}

// TODO: jwt token
Expand All @@ -38,4 +60,17 @@ export class UserService {
async getAllUser(): Promise<User[]> {
return await this.userRepository.find();
}

async getUserNickname(userId: number): Promise<UserNicknameResponseDto> {
const user = await this.isUserExisted(userId);
return this.userMapper.nicknameDtoFromEntity(user);
}

async isUserExisted(id: number): Promise<User> {
const user = await this.userRepository.findOne({ where: { id: id } });
if (!user) {
throw new NotFoundException(`User with ID ${id} not found`);
}
return user;
}
}

0 comments on commit e03f9e8

Please sign in to comment.