-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: AuthController, AuthService 생성 및 회원가입 API 작성 (#4)
- Loading branch information
1 parent
63dca32
commit e62e849
Showing
3 changed files
with
69 additions
and
0 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
src/main/java/com/project/mapdagu/domain/auth/controller/AuthController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/project/mapdagu/domain/auth/service/AuthService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/project/mapdagu/domain/member/dto/response/SignUpResponseDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |