Skip to content

Commit

Permalink
Merge pull request #18 from KusitmsHDmedi/feature/17-user
Browse files Browse the repository at this point in the history
[feat] 회원가입 api 구현
  • Loading branch information
RyuKwanKon authored Sep 13, 2023
2 parents 7d79aba + 9d546b0 commit a00e55a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kusithm.hdmedi_server.domain.user.controller;

import com.kusithm.hdmedi_server.domain.user.dto.request.UserAuthRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserSignUpRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.UserAuthResponseDto;
import com.kusithm.hdmedi_server.domain.user.service.AuthService;
import com.kusithm.hdmedi_server.global.common.BaseResponse;
Expand All @@ -27,4 +28,12 @@ public ResponseEntity<BaseResponse<?>> signIn(@RequestHeader("Authorization") fi
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, responseDto));
}

@PostMapping("/signUp")
public ResponseEntity<BaseResponse<?>> signUp(@RequestHeader("Authorization") final String token,
@RequestBody final UserSignUpRequestDto userSignUpRequestDto){
final UserAuthResponseDto responseDto = authService.signUp(token, userSignUpRequestDto);
return ResponseEntity.status(HttpStatus.OK)
.body(BaseResponse.of(SuccessCode.OK, responseDto));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.kusithm.hdmedi_server.domain.user.domain;

public enum Gender {
NAN,
WOMAN;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public class User {
private Platform platform;
private String platformId;
private String userName;
@Enumerated(value = EnumType.STRING)
private Gender gender;

public static User createUser(Platform platform, String platformId, String userName){
return User.builder()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.kusithm.hdmedi_server.domain.user.dto.request;

import com.kusithm.hdmedi_server.domain.user.domain.Gender;
import com.kusithm.hdmedi_server.domain.user.domain.Platform;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
public class UserSignUpRequestDto {
private String userName;
private LocalDateTime birthday;
private String gender;
private String platform;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@

public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findUserByPlatformAndPlatformId(Platform platform, String platformId);
boolean existsUserByPlatformAndPlatformId(Platform platform, String platformId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,20 @@
import com.kusithm.hdmedi_server.domain.user.domain.Platform;
import com.kusithm.hdmedi_server.domain.user.domain.User;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserAuthRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.request.UserSignUpRequestDto;
import com.kusithm.hdmedi_server.domain.user.dto.response.UserAuthResponseDto;
import com.kusithm.hdmedi_server.domain.user.repository.RefreshTokenRepository;
import com.kusithm.hdmedi_server.domain.user.repository.UserRepository;
import com.kusithm.hdmedi_server.global.config.jwt.JwtProvider;
import com.kusithm.hdmedi_server.global.config.jwt.Token;
import com.kusithm.hdmedi_server.global.error.exception.ConflictException;
import com.kusithm.hdmedi_server.global.error.exception.EntityNotFoundException;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

import static com.kusithm.hdmedi_server.domain.user.domain.RefreshToken.createRefreshToken;
import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.DUPLICATE_USER;
import static com.kusithm.hdmedi_server.global.error.exception.ErrorCode.USER_NOT_FOUND;

@RequiredArgsConstructor
Expand All @@ -37,6 +38,16 @@ public UserAuthResponseDto signIn(String token, UserAuthRequestDto requestDto) {
return UserAuthResponseDto.of(issuedToken, findUser);
}

public UserAuthResponseDto signUp(String token, UserSignUpRequestDto requestDto){
Platform platform = Platform.getEnumPlatformFrom(requestDto.getPlatform());
String platformId = getPlatformId(token);
validateDuplicateUser(platform, platformId);
User saveUser = saveUser(platform, platformId, requestDto.getUserName());
Token issuedToken = issueAccessTokenAndRefreshToken(saveUser);
updateRefreshToken(issuedToken.getRefreshToken(), saveUser);
return UserAuthResponseDto.of(issuedToken, saveUser);
}

private User getUser(Platform platform, String platformId) {
return userRepository.findUserByPlatformAndPlatformId(platform, platformId)
.orElseThrow(() -> new EntityNotFoundException(USER_NOT_FOUND));
Expand All @@ -59,4 +70,9 @@ private Token issueAccessTokenAndRefreshToken(User user) {
private void updateRefreshToken(String refreshToken, User user) {
refreshTokenRepository.save(createRefreshToken(user.getId(), refreshToken));
}

private void validateDuplicateUser(Platform platform, String platformId) {
if (userRepository.existsUserByPlatformAndPlatformId(platform, platformId))
throw new ConflictException(DUPLICATE_USER);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class SecurityConfig {
private final JwtAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private final JwtProvider jwtProvider;
// TODO api 추가될 때 white list url 확인해서 추가하기.
private static final String[] whiteList = {"/api/user/signin", "/api/user/signup", "/api/user/reissue", "/"};
private static final String[] whiteList = {"/api/user/signIn", "/api/user/signUp", "/api/user/reissue", "/"};

@Bean
public WebSecurityCustomizer webSecurityCustomizer() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public enum ErrorCode {
* 409 Conflict
*/
CONFLICT(HttpStatus.CONFLICT, "이미 존재하는 리소스입니다."),
DUPLICATE_USER(HttpStatus.CONFLICT, "이미 존재하는 회원입니다."),

/**
* 500 Internal Server Error
Expand Down

0 comments on commit a00e55a

Please sign in to comment.