Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: 마이페이지 내 정보 변경관련 작업입니다. #66

Merged
merged 6 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/com/catcher/core/domain/entity/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.catcher.core.domain.entity.enums.UserRole;
import jakarta.persistence.*;
import lombok.*;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.annotations.Where;

import java.time.ZonedDateTime;
Expand Down Expand Up @@ -85,4 +86,20 @@ public void changeEmailTerm(boolean shouldOn) {
public void changePhoneTerm(boolean shouldOn) {
phoneMarketingTerm = shouldOn ? ZonedDateTime.now() : null;
}

public void changeMyInfo(String nickname, UserGender gender, Date birthDate) {
if (!StringUtils.equals(nickname, this.nickname)) {
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
this.nickname = nickname;
}
if (!this.userGender.equals(gender)) {
this.userGender = gender;
}
if (!this.birthDate.equals(birthDate)) {
this.birthDate = birthDate;
}
}

public void changeProfileUrl(String filename) {
this.profileImageUrl = filename;
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package com.catcher.core.domain.entity.enums;

import com.fasterxml.jackson.annotation.JsonCreator;

public enum UserGender {
MALE, FEMALE,
MALE, FEMALE;

@JsonCreator
public static UserGender from(String s) {
return UserGender.valueOf(s.toUpperCase());
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
60 changes: 49 additions & 11 deletions src/main/java/com/catcher/core/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,22 @@
import com.catcher.core.dto.user.UserInfoResponse;
import com.catcher.core.dto.user.UserLoginRequest;
import com.catcher.resource.request.PromotionRequest;
import com.catcher.infrastructure.external.service.S3UploadService;
import com.catcher.resource.request.UserInfoEditRequest;
import com.catcher.resource.response.UserDetailsResponse;
import com.catcher.security.CatcherUser;
import com.catcher.utils.KeyGenerator;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.time.ZonedDateTime;
import java.util.Optional;
Expand All @@ -45,6 +50,7 @@ public class UserService {
private final PasswordEncoder passwordEncoder;
private final DBManager dbManager;
private final AuthService authService;
private final S3UploadService s3UploadService;

@Transactional
public TokenDto signUpUser(UserCreateRequest userCreateRequest) {
Expand Down Expand Up @@ -88,6 +94,48 @@ public void togglePhonePromotion(User user, PromotionRequest promotionRequest, P
}
}

public UserInfoResponse getMyInfo(User user) {
return new UserInfoResponse(user.getUsername(),
user.getPhone(),
user.getEmail(),
user.getProfileImageUrl(),
user.getNickname(),
user.getEmailMarketingTerm(),
user.getPhoneMarketingTerm());
}

public UserDetailsResponse getDetailsInfo(User user) {
return new UserDetailsResponse(
user.getNickname(),
user.getPhone(),
user.getEmail(),
user.getBirthDate(),
user.getUserGender()
);
}

@Transactional
public void editUserInfo(User user,
MultipartFile profileFile,
UserInfoEditRequest userInfoEditRequest) {
if (!StringUtils.equals(user.getNickname(), userInfoEditRequest.getNickname())) {
Optional<User> optionalUser = userRepository.findByNickname(userInfoEditRequest.getNickname());
if (optionalUser.isPresent()) {
throw new BaseException(USERS_DUPLICATED_NICKNAME);
}
}

user.changeMyInfo(userInfoEditRequest.getNickname(),
userInfoEditRequest.getGender(),
userInfoEditRequest.getBirth()
);

if (profileFile != null && !profileFile.isEmpty()) {
String fileName = s3UploadService.uploadFile(profileFile);
user.changeProfileUrl(fileName);
}
}

private TokenDto checkAuthenticationAndGetTokenDto(String username, String password) {
try {
CatcherUser authentication = (CatcherUser) authenticationManager.authenticate(
Expand Down Expand Up @@ -136,18 +184,8 @@ private User createUser(UserCreateRequest userCreateRequest) {
}

private void checkOAuthUser(User user) {
if(!user.getUserProvider().equals(CATCHER)) {
if (!user.getUserProvider().equals(CATCHER)) {
throw new BaseException(INVALID_USER_INFO);
}
}

public UserInfoResponse getMyInfo(User user){
return new UserInfoResponse(user.getUsername(),
user.getPhone(),
user.getEmail(),
user.getProfileImageUrl(),
user.getNickname(),
user.getEmailMarketingTerm(),
user.getPhoneMarketingTerm());
}
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 22 additions & 1 deletion src/main/java/com/catcher/resource/UserController.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.catcher.resource.resolver.annotation.AuthCodeVerifyInject;
import com.catcher.resource.response.AuthCodeVerifyResponse;
import com.catcher.resource.response.CaptchaValidateResponse;
import com.catcher.resource.response.UserDetailsResponse;
import com.catcher.security.annotation.AuthorizationRequired;
import com.catcher.security.annotation.CurrentUser;
import io.swagger.v3.oas.annotations.Operation;
Expand All @@ -25,6 +26,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
Expand All @@ -34,13 +36,15 @@
import static com.catcher.common.response.CommonResponse.success;
import static com.catcher.config.JwtTokenProvider.setRefreshCookie;
import static com.catcher.core.domain.entity.enums.UserRole.USER;
import static com.catcher.resource.request.PromotionRequest.PromotionType.*;
import static com.catcher.resource.request.PromotionRequest.PromotionType.EMAIL;
import static com.catcher.resource.request.PromotionRequest.PromotionType.PHONE;
import static com.catcher.utils.HttpServletUtils.deleteCookie;
import static com.catcher.utils.JwtUtils.REFRESH_TOKEN_NAME;
import static com.catcher.utils.KeyGenerator.AuthType;
import static com.catcher.utils.KeyGenerator.AuthType.FIND_ID;
import static com.catcher.utils.KeyGenerator.AuthType.FIND_PASSWORD;
import static org.springframework.http.HttpHeaders.AUTHORIZATION;
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA_VALUE;

@RequiredArgsConstructor
@RestController
Expand Down Expand Up @@ -182,6 +186,23 @@ public CommonResponse<Void> toggleEmail(@CurrentUser User user, @Valid @RequestB
return success();
}

@Operation(summary = "내 세부 정보 가져오기")
@GetMapping("/info/details")
@AuthorizationRequired(USER)
public CommonResponse<UserDetailsResponse> getDetailsInfo(@CurrentUser User user) {
return success(userService.getDetailsInfo(user));
}

@Operation(summary = "내 정보 수정하기")
@PostMapping(value = "/info/edit", consumes = {MULTIPART_FORM_DATA_VALUE})
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
@AuthorizationRequired(USER)
public CommonResponse<Void> editMyInfo(@CurrentUser User user,
@RequestPart(value = "profile_file", required = false) MultipartFile file,
@RequestPart(value = "userInfoEditRequest", required = false) UserInfoEditRequest userInfoEditRequest) {
userService.editUserInfo(user, file, userInfoEditRequest);
return success();
}

private AuthCodeServiceBase getAuthCodeService(AuthType authType) {
return authCodeServices.stream()
.filter(service -> service.support(authType))
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.catcher.resource.request;

import com.catcher.core.domain.entity.enums.UserGender;
import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Date;

@Getter
@NoArgsConstructor
@AllArgsConstructor
public class UserInfoEditRequest {
@NotNull
private String nickname;
@NotNull
@JsonFormat(pattern = "yyyyMMdd")
private Date birth;
private UserGender gender;
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.catcher.resource.response;

import com.catcher.core.domain.entity.enums.UserGender;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

import java.util.Date;

@Getter
@AllArgsConstructor
@NoArgsConstructor
public class UserDetailsResponse {
private String nickname;
private String phone;
private String email;
private Date birth;
private UserGender userGender;
}
dev-khg marked this conversation as resolved.
Show resolved Hide resolved
Loading