Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.threestar.trainus.domain.profile.dto.ImageUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.ImageUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.IntroUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.IntroUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileDetailResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileUpdateRequestDto;
import com.threestar.trainus.domain.profile.service.ProfileFacadeService;
import com.threestar.trainus.global.annotation.LoginUser;
import com.threestar.trainus.global.unit.BaseResponse;
Expand All @@ -38,13 +40,23 @@ public ResponseEntity<BaseResponse<ProfileDetailResponseDto>> getProfileDetail(
return BaseResponse.ok("프로필 상세 조회가 완료되었습니다.", response, HttpStatus.OK);
}

@PatchMapping
@Operation(summary = "유저 프로필 수정 api")
public ResponseEntity<BaseResponse<ProfileResponseDto>> updateProfile(
@Valid @RequestBody ProfileUpdateRequestDto requestDto,
@LoginUser Long userId
@PatchMapping("/image")
@Operation(summary = "유저 프로필 이미지 수정 api")
public ResponseEntity<BaseResponse<ImageUpdateResponseDto>> updateProfileImage(
@Valid @RequestBody ImageUpdateRequestDto requestDto,
@LoginUser Long loginUserId
) {
ProfileResponseDto response = facadeService.updateProfile(userId, requestDto);
return BaseResponse.ok("프로필 수정이 완료되었습니다.", response, HttpStatus.OK);
ImageUpdateResponseDto response = facadeService.updateProfileImage(loginUserId, requestDto);
return BaseResponse.ok("프로필 이미지 수정이 완료되었습니다.", response, HttpStatus.OK);
}

@PatchMapping("/intro")
@Operation(summary = "유저 프로필 자기소개 수정 api")
public ResponseEntity<BaseResponse<IntroUpdateResponseDto>> updateProfileIntro(
@Valid @RequestBody IntroUpdateRequestDto requestDto,
@LoginUser Long loginUserId
) {
IntroUpdateResponseDto response = facadeService.updateProfileIntro(loginUserId, requestDto);
return BaseResponse.ok("프로필 자기소개 수정이 완료되었습니다.", response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.threestar.trainus.domain.profile.dto;

import org.hibernate.validator.constraints.URL;

import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;

public record ProfileUpdateRequestDto(
public record ImageUpdateRequestDto(

@Size(max = 2048, message = "프로필 이미지 URL은 2048자 이하여야 합니다.")
@Pattern(
regexp = "^$|^https?://[\\w\\-._~:/?#\\[\\]@!$&'()*+,;=%]+$",
message = "올바른 URL 형식이어야 합니다."
)
String profileImage,

@Size(max = 255, message = "자기소개는 255자 이하여야 합니다.")
String intro
String profileImage
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.threestar.trainus.domain.profile.dto;

public record ImageUpdateResponseDto(
Long userId,
String nickname,
String profileImage
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.threestar.trainus.domain.profile.dto;

import jakarta.validation.constraints.Size;

public record IntroUpdateRequestDto(

@Size(max = 255, message = "자기소개는 255자 이하여야 합니다.")
String intro
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.threestar.trainus.domain.profile.dto;

public record IntroUpdateResponseDto(
Long userId,
String nickname,
String intro
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public class Profile {
@Column(length = 255)
private String intro;

public void updateProfile(String profileImage, String intro) {
public void updateProfileImage(String profileImage) {
this.profileImage = profileImage;
}

public void updateProfileIntro(String intro) {
this.intro = intro;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.threestar.trainus.domain.profile.mapper;

import com.threestar.trainus.domain.profile.dto.ImageUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.IntroUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileResponseDto;
import com.threestar.trainus.domain.profile.entity.Profile;
import com.threestar.trainus.domain.user.entity.User;
Expand All @@ -19,6 +21,22 @@ public static ProfileResponseDto toResponseDto(Profile profile, User user) {
);
}

public static ImageUpdateResponseDto toImageResponseDto(Profile profile, User user) {
return new ImageUpdateResponseDto(
user.getId(),
user.getNickname(),
profile.getProfileImage()
);
}

public static IntroUpdateResponseDto toIntroResponseDto(Profile profile, User user) {
return new IntroUpdateResponseDto(
user.getId(),
user.getNickname(),
profile.getIntro()
);
}

public static Profile toDefaultEntity(User user) {
return Profile.builder()
.user(user)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

import com.threestar.trainus.domain.metadata.dto.ProfileMetadataResponseDto;
import com.threestar.trainus.domain.metadata.service.ProfileMetadataService;
import com.threestar.trainus.domain.profile.dto.IntroUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.ProfileDetailResponseDto;
import com.threestar.trainus.domain.profile.dto.ImageUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ImageUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.IntroUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileUpdateRequestDto;
import com.threestar.trainus.domain.profile.mapper.ProfileDetailMapper;
import com.threestar.trainus.domain.user.entity.User;

Expand All @@ -31,8 +34,13 @@ public ProfileDetailResponseDto getProfileDetail(Long userId) {

//프로필 수정
@Transactional
public ProfileResponseDto updateProfile(Long userId, ProfileUpdateRequestDto requestDto) {
return profileService.updateProfile(userId, requestDto);
public ImageUpdateResponseDto updateProfileImage(Long userId, ImageUpdateRequestDto requestDto) {
return profileService.updateProfileImage(userId, requestDto);
}

@Transactional
public IntroUpdateResponseDto updateProfileIntro(Long userId, IntroUpdateRequestDto requestDto) {
return profileService.updateProfileIntro(userId, requestDto);
}

//회원가입 시 프로필,메타데이터 디폴트로 생성.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.threestar.trainus.domain.profile.dto.IntroUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.ImageUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ImageUpdateRequestDto;
import com.threestar.trainus.domain.profile.dto.IntroUpdateResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileResponseDto;
import com.threestar.trainus.domain.profile.dto.ProfileUpdateRequestDto;
import com.threestar.trainus.domain.profile.entity.Profile;
import com.threestar.trainus.domain.profile.mapper.ProfileMapper;
import com.threestar.trainus.domain.profile.repository.ProfileRepository;
Expand Down Expand Up @@ -39,15 +42,28 @@ public ProfileResponseDto getProfile(Long userId) {
}

@Transactional
public ProfileResponseDto updateProfile(Long userId, ProfileUpdateRequestDto requestDto) {
public ImageUpdateResponseDto updateProfileImage(Long userId, ImageUpdateRequestDto requestDto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

Profile profile = findByUserId(userId);

profile.updateProfile(requestDto.profileImage(), requestDto.intro());
profile.updateProfileImage(requestDto.profileImage());

return ProfileMapper.toResponseDto(profile, user);
return ProfileMapper.toImageResponseDto(profile, user);
}

@Transactional
public IntroUpdateResponseDto updateProfileIntro(Long userId, IntroUpdateRequestDto requestDto) {
User user = userRepository.findById(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.USER_NOT_FOUND));

Profile profile = profileRepository.findByUserId(userId)
.orElseThrow(() -> new BusinessException(ErrorCode.PROFILE_NOT_FOUND));

profile.updateProfileIntro(requestDto.intro());

return ProfileMapper.toIntroResponseDto(profile, user);
}

public Profile findByUserId(Long userId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -13,10 +14,12 @@
import com.threestar.trainus.domain.user.dto.LoginRequestDto;
import com.threestar.trainus.domain.user.dto.LoginResponseDto;
import com.threestar.trainus.domain.user.dto.NicknameCheckRequestDto;
import com.threestar.trainus.domain.user.dto.PasswordUpdateDto;
import com.threestar.trainus.domain.user.dto.SignupRequestDto;
import com.threestar.trainus.domain.user.dto.SignupResponseDto;
import com.threestar.trainus.domain.user.service.EmailVerificationService;
import com.threestar.trainus.domain.user.service.UserService;
import com.threestar.trainus.global.annotation.LoginUser;
import com.threestar.trainus.global.unit.BaseResponse;

import io.swagger.v3.oas.annotations.Operation;
Expand Down Expand Up @@ -87,4 +90,14 @@ public ResponseEntity<BaseResponse<Void>> confirmVerificationCode(
emailVerificationService.verifyCode(request.email(), request.verificationCode());
return BaseResponse.ok("이메일 인증이 완료되었습니다.", null, HttpStatus.OK);
}

@PatchMapping("/password")
@Operation(summary = "비밀번호 변경 api")
public ResponseEntity<BaseResponse<Void>> updatePassword(
@Valid @RequestBody PasswordUpdateDto request,
@LoginUser Long loginUserId
) {
userService.updatePassword(request, loginUserId);
return BaseResponse.ok("비밀번호 변경이 완료되었습니다.", null, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.threestar.trainus.domain.user.dto;

import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Size;

public record PasswordUpdateDto(

@NotBlank(message = "현재 비밀번호는 필수입니다.")
String currentPassword,

@NotBlank(message = "새 비밀번호는 필수입니다.")
@Size(min = 8, max = 20, message = "비밀번호는 8자 이상 20자 이하입니다.")
String newPassword,

@NotBlank(message = "새 비밀번호 확인은 필수입니다.")
String confirmPassword
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ public class User extends BaseDateEntity {
@OneToMany(mappedBy = "user")
private List<UserCoupon> userCoupons = new ArrayList<>();

public void updatePassword(String newPassword) {
this.password = newPassword;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.threestar.trainus.domain.profile.service.ProfileFacadeService;
import com.threestar.trainus.domain.user.dto.LoginRequestDto;
import com.threestar.trainus.domain.user.dto.LoginResponseDto;
import com.threestar.trainus.domain.user.dto.PasswordUpdateDto;
import com.threestar.trainus.domain.user.dto.SignupRequestDto;
import com.threestar.trainus.domain.user.dto.SignupResponseDto;
import com.threestar.trainus.domain.user.entity.User;
Expand Down Expand Up @@ -119,4 +120,22 @@ public User getAdminUser(Long userId) {
return user;
}

public void updatePassword(PasswordUpdateDto request, Long userId) {
//새 비밀번호와 새 비밀번호 확인끼리의 검증
if (!request.newPassword().equals(request.confirmPassword())) {
throw new BusinessException(ErrorCode.INVALID_REQUEST_DATA);
}

User user = getUserById(userId);
//유저의 현재 비밀번호 검증
if (!passwordEncoder.matches(request.currentPassword(), user.getPassword())) {
throw new BusinessException(ErrorCode.INVALID_REQUEST_DATA);
}

String encordedNewPassword = passwordEncoder.encode(request.newPassword());

user.updatePassword(encordedNewPassword);

userRepository.save(user);
}
}
Loading