Skip to content

Commit

Permalink
#106 Feat : 회원정보 수정(이름 변경, 이메일 변경) API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ls-rain committed Feb 11, 2024
1 parent 48770fd commit 8b36731
Show file tree
Hide file tree
Showing 7 changed files with 146 additions and 4 deletions.
24 changes: 24 additions & 0 deletions src/main/java/friend/spring/converter/MyPageConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import friend.spring.domain.Post;
import friend.spring.domain.User;
import friend.spring.domain.mapping.Post_scrap;
import friend.spring.web.dto.MyPageRequestDTO;
import friend.spring.web.dto.MyPageResponseDTO;
import org.springframework.data.domain.Page;

Expand Down Expand Up @@ -63,4 +64,27 @@ public static MyPageResponseDTO.MyProfileResDTO toMyProfileResDTO(User user){
.phone(user.getPhone())
.build();
}

public static User toUserName(MyPageRequestDTO.ProfileEditNameReq profileEditNameReq){
return User.builder()
.nickname(profileEditNameReq.getNickName())
.build();
}
public static MyPageResponseDTO.ProfileEditNameRes toProfileEditNameResDTO(User user){
return MyPageResponseDTO.ProfileEditNameRes.builder()
.nickName(user.getNickname())
.build();
}

public static MyPageResponseDTO.ProfileEditEmailRes toProfileEditEmailResDTO(User user){
return MyPageResponseDTO.ProfileEditEmailRes.builder()
.changeEmail(user.getEmail())
.build();
}

public static User toUserEmail(MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq){
return User.builder()
.email(profileEditEmailReq.getChangeEmail())
.build();
}
}
4 changes: 4 additions & 0 deletions src/main/java/friend/spring/domain/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,5 +165,9 @@ public void setPoint(Integer point) {
public void setFile(File file) {
this.file = file;
}

public void setNickname(String nickname){ this.nickname = nickname; }

public void setEmail(String email){this.email = email;}
}

5 changes: 5 additions & 0 deletions src/main/java/friend/spring/service/MyPageService.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import friend.spring.domain.Post;
import friend.spring.domain.User;
import friend.spring.domain.mapping.Post_scrap;
import friend.spring.web.dto.MyPageRequestDTO;
import org.springframework.data.domain.Page;
import org.springframework.web.multipart.MultipartFile;

Expand All @@ -21,4 +22,8 @@ public interface MyPageService {
void editUserImage(MultipartFile file, HttpServletRequest request);

User getEditUserPage(Long userId);

User editUserName(Long userId, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq);

User editUserEmail(Long userId, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq);
}
15 changes: 15 additions & 0 deletions src/main/java/friend/spring/service/MyPageServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import friend.spring.repository.PostScrapRepository;
import friend.spring.repository.UserRepository;
import friend.spring.security.JwtTokenProvider;
import friend.spring.web.dto.MyPageRequestDTO;
import friend.spring.web.dto.MyPageResponseDTO;
import lombok.AllArgsConstructor;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -101,5 +102,19 @@ public User getEditUserPage(Long userId) {
return user;
}

@Override
public User editUserName(Long userId, MyPageRequestDTO.ProfileEditNameReq profileEditNameReq) {
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
user.setNickname(profileEditNameReq.getNickName());
return user;
}

@Override
public User editUserEmail(Long userId, MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq) {
User user = userRepository.findById(userId).orElseThrow(() -> new GeneralException(ErrorStatus.USER_NOT_FOUND));
user.setEmail(profileEditEmailReq.getChangeEmail());
return user;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import friend.spring.domain.Post;
import friend.spring.domain.User;
import friend.spring.domain.mapping.Comment_choice;
import friend.spring.service.JwtTokenService;
import friend.spring.service.MyPageService;
import friend.spring.service.PostService;
import friend.spring.service.UserService;
import friend.spring.service.*;
import friend.spring.web.dto.CommentResponseDTO;
import friend.spring.web.dto.MyPageRequestDTO;
import friend.spring.web.dto.MyPageResponseDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -24,6 +22,7 @@
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

@RestController
Expand All @@ -35,6 +34,7 @@ public class MyPageRestController {
private final MyPageService myPageService;

private final JwtTokenService jwtTokenService;
private final EmailService emailService;

//저장한 게시물(내 카테고리) 조회
@GetMapping("/post")
Expand Down Expand Up @@ -119,4 +119,42 @@ public ApiResponse<MyPageResponseDTO.MyProfileResDTO> getEditUserPage(
User editUserPage = myPageService.getEditUserPage(userId);
return ApiResponse.onSuccess(MyPageConverter.toMyProfileResDTO(editUserPage));
}

@PatchMapping(value = "/profile/modify/name")
@Operation(summary = "회원정보 이름 수정 API", description = "회원정보 이름을 수정하는 API입니다. ex) /user/my-page/profile/modify/name")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 요청에 성공했습니다. "),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4005",description = "UNAUTHORIZED, 인증 코드가 일치하지 않습니다."),
})
@Parameters({
@Parameter(name = "atk", description = "RequestHeader - 로그인한 사용자의 accessToken")
})
public ApiResponse<MyPageResponseDTO.ProfileEditNameRes> editUserName(
@RequestHeader(name = "atk") String atk,
HttpServletRequest request,
@RequestBody @Valid MyPageRequestDTO.ProfileEditNameReq profileEditNameReq){
Long userId = jwtTokenService.JwtToId(request);
emailService.CheckAuthNum(profileEditNameReq.getEmail(), profileEditNameReq.getCertification());
User editUserName = myPageService.editUserName(userId, profileEditNameReq);
return ApiResponse.onSuccess(MyPageConverter.toProfileEditNameResDTO(editUserName));
}

@PatchMapping(value = "/profile/modify/email")
@Operation(summary = "회원정보 이메일 수정 API", description = "회원정보 이메일을 수정하는 API입니다. ex) /user/my-page/profile/modify/email")
@ApiResponses({
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "COMMON200", description = "OK, 요청에 성공했습니다. "),
@io.swagger.v3.oas.annotations.responses.ApiResponse(responseCode = "USER4005",description = "UNAUTHORIZED, 인증 코드가 일치하지 않습니다."),
})
@Parameters({
@Parameter(name = "atk", description = "RequestHeader - 로그인한 사용자의 accessToken")
})
public ApiResponse<MyPageResponseDTO.ProfileEditEmailRes> editUserEmail(
@RequestHeader(name = "atk") String atk,
HttpServletRequest request,
@RequestBody @Valid MyPageRequestDTO.ProfileEditEmailReq profileEditEmailReq){
Long userId = jwtTokenService.JwtToId(request);
emailService.CheckAuthNum(profileEditEmailReq.getCurEmail(), profileEditEmailReq.getCertification());
User editUserEmail = myPageService.editUserEmail(userId, profileEditEmailReq);
return ApiResponse.onSuccess(MyPageConverter.toProfileEditEmailResDTO(editUserEmail));
}
}
38 changes: 38 additions & 0 deletions src/main/java/friend/spring/web/dto/MyPageRequestDTO.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package friend.spring.web.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;

public class MyPageRequestDTO {

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProfileEditNameReq{
@Email//1)@기호를 포함해야 한다.2_@기호를 기준으로 이메일 주소를 이루는 로컬호스트와 도메인 파트가 존재해야 한다.3)도메인 파트는 최소하나의 점과
//그 뒤에 최소한 2개의 알파벳을 가진다를 검증
@NotEmpty(message = "이메일을 입력해 주세요")
private String email;
private String certification;
String nickName;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProfileEditEmailReq{
@Email//1)@기호를 포함해야 한다.2_@기호를 기준으로 이메일 주소를 이루는 로컬호스트와 도메인 파트가 존재해야 한다.3)도메인 파트는 최소하나의 점과
//그 뒤에 최소한 2개의 알파벳을 가진다를 검증
@NotEmpty(message = "이메일을 입력해 주세요")
private String curEmail;
private String certification;
String changeEmail;
}
}
18 changes: 18 additions & 0 deletions src/main/java/friend/spring/web/dto/MyPageResponseDTO.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Getter;
import lombok.NoArgsConstructor;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Set;
Expand Down Expand Up @@ -57,4 +59,20 @@ public static class MyProfileResDTO{
String email;
String phone;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProfileEditNameRes{
String nickName;
}

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public static class ProfileEditEmailRes{
String changeEmail;
}
}

0 comments on commit 8b36731

Please sign in to comment.