Skip to content

Commit

Permalink
user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
T-ferret committed Sep 28, 2024
1 parent 2dff696 commit aab7ac4
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.goormdari.domain.user.domain.dto.request;
package com.goormdari.domain.user.dto.request;

import lombok.Builder;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package com.goormdari.domain.user.presentation;

import com.goormdari.domain.calendar.exception.InvalidTokenException;
import com.goormdari.domain.user.dto.response.FindCurrentStepResponse;
import com.goormdari.domain.user.dto.response.UserInfoResponse;
import com.goormdari.domain.user.dto.request.UpdateUserRequest;
import com.goormdari.domain.user.service.UserService;
import com.goormdari.domain.user.dto.response.FindCurrentStepResponse;
import com.goormdari.global.config.security.jwt.JWTUtil;
import com.goormdari.global.payload.ErrorResponse;
import com.goormdari.global.payload.ResponseCustom;
Expand All @@ -14,6 +15,7 @@
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

Expand Down Expand Up @@ -67,7 +69,7 @@ public UserInfoResponse getCurrentUserInfo(@Parameter(description = "Accesstoken
@Operation(summary = "ํ˜„์žฌ ์œ ์ € ํ”„๋กœํ•„ ์—…๋ฐ์ดํŠธ", description = "์œ ์ €์˜ nickname, username, password(์ง€๋‚œ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒ€์ฆ ๊ณผ์ • ์กด์žฌ) email, profileImageUrl ์—…๋ฐ์ดํŠธ ๊ธฐ๋Šฅ(null ๊ฐ’์œผ๋กœ ์ „์†ก ์‹œ, ์—…๋ฐ์ดํŠธ X)")
@PostMapping("/profile")
public UserInfoResponse updateCurrentUserInfo(@Parameter(description = "Accesstoken์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š”.", required = true) @RequestHeader("Authorization") String token,
@Valid @RequestBody UpdateUserRequest updateUserRequest) {
@Valid @RequestBody UpdateUserRequest updateUserRequest) {
if (token == null) {
throw new InvalidTokenException();
}
Expand Down
47 changes: 47 additions & 0 deletions src/main/java/com/goormdari/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.goormdari.domain.user.service;

import com.amazonaws.services.kms.model.NotFoundException;
import com.goormdari.domain.user.domain.exception.InvalidPasswordException;
import com.goormdari.domain.user.dto.response.UserInfoResponse;
import com.goormdari.domain.user.dto.response.FindCurrentStepResponse;
import com.goormdari.domain.user.domain.User;
Expand Down Expand Up @@ -101,4 +102,50 @@ public UserInfoResponse getUserInfo(Long userId) {
.build();

}

public boolean verifyCurrentPassword(User user, String rawPassword) {
return passwordEncoder.matches(rawPassword, user.getPassword());
}

@Transactional
public UserInfoResponse updateUserProfile(Long userId, com.goormdari.domain.user.dto.request.UpdateUserRequest updateUserRequest) {
User user = userRepository.findById(userId).orElseThrow(()->new NotFoundException("User Not Found: " + userId));

// ๋‹‰๋„ค์ž„ ์—…๋ฐ์ดํŠธ
if (updateUserRequest.getNickname() != null && !updateUserRequest.getNickname().isEmpty()) {
user.updateNickname(updateUserRequest.getNickname());
}

// ์œ ์ €๋„ค์ž„ ์—…๋ฐ์ดํŠธ (์ค‘๋ณต ์ฒดํฌ ํ•„์š”)
if (updateUserRequest.getUsername() != null && !updateUserRequest.getUsername().isEmpty()) {
if (userRepository.findByUsername(updateUserRequest.getUsername()).isPresent() &&
!user.getUsername().equals(updateUserRequest.getUsername())) {
throw new IllegalArgumentException("Username is already exists.");
}
user.updateUsername(updateUserRequest.getUsername());
}

// ๋น„๋ฐ€๋ฒˆํ˜ธ ์—…๋ฐ์ดํŠธ (์ธ์ฝ”๋”ฉ ํ•„์š”)
if (updateUserRequest.getPassword() != null && !updateUserRequest.getPassword().isEmpty()) {
// ํ˜„์žฌ ๋น„๋ฐ€๋ฒˆํ˜ธ ๊ฒ€์ฆ
if (updateUserRequest.getCurrentPassword() == null || !verifyCurrentPassword(user, updateUserRequest.getCurrentPassword())) {
throw new InvalidPasswordException("Current password is incorrect.");
}
user.updatePassword(passwordEncoder.encode(updateUserRequest.getPassword()));
}

// ํ”„๋กœํ•„ URL ์—…๋ฐ์ดํŠธ
if (updateUserRequest.getProfileUrl() != null && !updateUserRequest.getProfileUrl().isEmpty()) {
user.updateProfileUrl(updateUserRequest.getProfileUrl());
}

// ํ”„๋กœํ•„ ์ด๋ฉ”์ผ ์—…๋ฐ์ดํŠธ
if (updateUserRequest.getEmail() != null && !updateUserRequest.getEmail().isEmpty()) {
user.updateEmail(updateUserRequest.getEmail());
}

userRepository.save(user);

return getUserInfo(userId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.goormdari.global.config.security.jwt.JWTFilter;
import com.goormdari.global.config.security.jwt.JWTUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
Expand All @@ -21,6 +22,7 @@
@RequiredArgsConstructor
@Configuration
@EnableWebSecurity
@EnableAutoConfiguration
public class SecurityConfig {

private final JWTUtil jwtUtil;
Expand Down

0 comments on commit aab7ac4

Please sign in to comment.