From c4696a96ffc806c5ecbde39c5f4bde37192eaeec Mon Sep 17 00:00:00 2001 From: Kim minho Date: Mon, 5 Aug 2024 01:11:14 +0900 Subject: [PATCH] =?UTF-8?q?feat#57:=20=EC=86=8C=EC=85=9C=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../oxox/dto/user/SocialLoginDto.java | 11 ++++ .../oxox/handler/error/ErrorCode.java | 2 + .../kimandhong/oxox/service/PostService.java | 4 +- .../kimandhong/oxox/service/UserService.java | 56 ++++++++++++------- 4 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java b/src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java index 81f6b96..d19dd07 100644 --- a/src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java +++ b/src/main/java/kimandhong/oxox/dto/user/SocialLoginDto.java @@ -1,9 +1,20 @@ package kimandhong.oxox.dto.user; +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; + public record SocialLoginDto( + @Email + @NotBlank(message = "email is required") + @Schema(example = "social@email.com") String email, + @NotBlank(message = "displayName is required") + @Schema(example = "김민호") String displayName, String photoUrl, + @NotBlank(message = "uid is required") + @Schema(example = "test uid") String uid ) { } diff --git a/src/main/java/kimandhong/oxox/handler/error/ErrorCode.java b/src/main/java/kimandhong/oxox/handler/error/ErrorCode.java index 72c62f4..4248c6b 100644 --- a/src/main/java/kimandhong/oxox/handler/error/ErrorCode.java +++ b/src/main/java/kimandhong/oxox/handler/error/ErrorCode.java @@ -11,6 +11,7 @@ public enum ErrorCode { BAD_REQUEST_LOGIN(HttpStatus.BAD_REQUEST, "아이디 또는 비밀번호를 확인해주세요"), WRONG_PASSWORD(HttpStatus.BAD_REQUEST, "비밀번호를 확인해주세요."), WRONG_PARAMETER(HttpStatus.BAD_REQUEST, "정렬 종류를 확인해주세요."), + INVALID_UID(HttpStatus.BAD_REQUEST, "잘못된 UID입니다."), UNAUTHORIZED_REQUEST(HttpStatus.UNAUTHORIZED, "Unauthorized."), FORBIDDEN_ACCESS(HttpStatus.FORBIDDEN, "Forbidden."), @@ -25,6 +26,7 @@ public enum ErrorCode { METHOD_NOT_ALLOWED(HttpStatus.METHOD_NOT_ALLOWED, "Not allowed method."), + NOT_SOCIAL_USER(HttpStatus.CONFLICT, "Email, Password를 사용해 가입한 사용자입니다."), CONFLICT(HttpStatus.CONFLICT, "Conflict"), CONFLICT_EMAIL(HttpStatus.CONFLICT, "중복된 이메일입니다."), CONFLICT_GOOGLE(HttpStatus.CONFLICT, "Google로 가입된 이메일입니다."), diff --git a/src/main/java/kimandhong/oxox/service/PostService.java b/src/main/java/kimandhong/oxox/service/PostService.java index ef65b5b..73686fa 100644 --- a/src/main/java/kimandhong/oxox/service/PostService.java +++ b/src/main/java/kimandhong/oxox/service/PostService.java @@ -84,7 +84,9 @@ public void updatePost(final Long postId, final RequestPostDto postDto, final Mu public void deletePost(final Long id) { final Post post = postRepository.findByIdAndUserId(id, securityUtil.getCustomUserId()).orElseThrow(() -> new NotFoundException(ErrorCode.NOT_FOUND_POST)); postRepository.deleteById(post.getId()); - s3Service.deleteFile(post.getThumbnail()); + if (post.getThumbnail() != null) { + s3Service.deleteFile(post.getThumbnail()); + } } @Transactional diff --git a/src/main/java/kimandhong/oxox/service/UserService.java b/src/main/java/kimandhong/oxox/service/UserService.java index 9f09b5a..abfaff8 100644 --- a/src/main/java/kimandhong/oxox/service/UserService.java +++ b/src/main/java/kimandhong/oxox/service/UserService.java @@ -6,6 +6,7 @@ import kimandhong.oxox.dto.user.LoginDto; import kimandhong.oxox.dto.user.SocialLoginDto; import kimandhong.oxox.handler.error.ErrorCode; +import kimandhong.oxox.handler.error.exception.BadRequestException; import kimandhong.oxox.handler.error.exception.ConflictException; import kimandhong.oxox.handler.error.exception.NotFoundException; import kimandhong.oxox.repository.UserRepository; @@ -26,38 +27,51 @@ public class UserService { @Transactional public User join(final JoinDto joinDto, final MultipartFile file) { - final String profileImage = file != null ? s3Service.uploadFile(file, S3path.PROFILE) : null; + userRepository.findByEmail(joinDto.email()).ifPresent(user -> { + throw new ConflictException(user.getUid() == null + ? ErrorCode.CONFLICT_EMAIL + : ErrorCode.CONFLICT_GOOGLE); + }); - try { - userRepository.findByEmail(joinDto.email()).ifPresent(user -> { - throw new ConflictException(user.getUid() == null - ? ErrorCode.CONFLICT_EMAIL - : ErrorCode.CONFLICT_GOOGLE); - }); + final String password = passwordEncoder.encode(joinDto.password()); + final Long sequence = profileCustomRepository.findMaxSequenceByNickname(joinDto.nickname()) + 1; - final String password = passwordEncoder.encode(joinDto.password()); - final Long sequence = profileCustomRepository.findMaxSequenceByNickname(joinDto.nickname()) + 1; + final String profileImage = file != null ? s3Service.uploadFile(file, S3path.PROFILE) : null; - final User user = User.from(joinDto, password, sequence, profileImage); + final User user = User.from(joinDto, password, sequence, profileImage); - return userRepository.save(user); - } catch (Exception e) { - s3Service.deleteFile(profileImage); - throw new RuntimeException(e.getMessage()); - } + return userRepository.save(user); } public User login(final LoginDto loginDto) { return userRepository.findByEmail(loginDto.email()) - .filter(foundUser -> passwordEncoder.matches(loginDto.password(), foundUser.getPassword())) + .map(user -> { + if (user.getPassword() == null) { + throw new BadRequestException(ErrorCode.CONFLICT_GOOGLE); + } + if (!passwordEncoder.matches(loginDto.password(), user.getPassword())) { + throw new NotFoundException(ErrorCode.BAD_REQUEST_LOGIN); + } + return user; + }) .orElseThrow(() -> new NotFoundException(ErrorCode.BAD_REQUEST_LOGIN)); } public User socialLogin(final SocialLoginDto loginDto) { - return userRepository.findByUid(loginDto.uid()).orElseGet(() -> { - final Long sequence = profileCustomRepository.findMaxSequenceByNickname(loginDto.displayName()) + 1; - final User user = User.from(loginDto, sequence); - return userRepository.save(user); - }); + return userRepository.findByEmail(loginDto.email()) + .map(user -> { + if (!user.getPassword().isEmpty()) { + throw new BadRequestException(ErrorCode.NOT_SOCIAL_USER); + } + if (!user.getUid().equals(loginDto.uid())) { + throw new BadRequestException(ErrorCode.INVALID_UID); + } + return user; + }) + .orElseGet(() -> { + final Long sequence = profileCustomRepository.findMaxSequenceByNickname(loginDto.displayName()) + 1; + final User newUser = User.from(loginDto, sequence); + return userRepository.save(newUser); + }); } }