Skip to content

Commit

Permalink
feat: AuthController, AuthService 생성 및 회원가입 API 작성 (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kyeong-hyeok committed Aug 2, 2023
1 parent 63dca32 commit e62e849
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.project.mapdagu.domain.auth.controller;

import com.project.mapdagu.common.dto.ResponseDto;
import com.project.mapdagu.domain.auth.service.AuthService;
import com.project.mapdagu.domain.member.dto.request.SignUpRequestDto;
import com.project.mapdagu.domain.member.dto.response.SignUpResponseDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class AuthController {

private final AuthService authService;

@PostMapping("/api/sign-up")
public ResponseEntity<SignUpResponseDto> signUp(@RequestBody SignUpRequestDto signUpRequestDto) {
SignUpResponseDto signUpResponseDto = authService.signUp(signUpRequestDto);
return ResponseDto.created(signUpResponseDto);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.project.mapdagu.domain.auth.service;

import com.project.mapdagu.domain.member.dto.request.SignUpRequestDto;
import com.project.mapdagu.domain.member.dto.response.SignUpResponseDto;
import com.project.mapdagu.domain.member.entity.Member;
import com.project.mapdagu.domain.member.repository.MemberRepository;
import com.project.mapdagu.error.exception.custom.BusinessException;
import lombok.RequiredArgsConstructor;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.project.mapdagu.error.ErrorCode.ALREADY_EXIST_EMAIL;
import static com.project.mapdagu.error.ErrorCode.ALREADY_EXIST_USERNAME;

@Service
@Transactional
@RequiredArgsConstructor
public class AuthService {

private final MemberRepository memberRepository;
private final PasswordEncoder passwordEncoder;

public SignUpResponseDto signUp(SignUpRequestDto signUpRequestDto) {

if (memberRepository.existsByEmail(signUpRequestDto.email())) {
throw new BusinessException(ALREADY_EXIST_EMAIL);
}
if (memberRepository.existsByUserName(signUpRequestDto.userName())) {
throw new BusinessException(ALREADY_EXIST_USERNAME);
}

Member member = signUpRequestDto.toEntity();
member.passwordEncode(passwordEncoder);
memberRepository.save(member);
return SignUpResponseDto.of(member.getId(), member.getUserName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.project.mapdagu.domain.member.dto.response;

public record SignUpResponseDto(Long id, String userName) {
public static SignUpResponseDto of(Long id, String userName){
return new SignUpResponseDto(id, userName);
}
}

0 comments on commit e62e849

Please sign in to comment.