Skip to content

Commit

Permalink
[api-server-v1] feat: UserProfile 조회 API 추가 - /v1/user/profile (#112)
Browse files Browse the repository at this point in the history
  • Loading branch information
gdtknight committed Oct 12, 2023
1 parent 6ab10de commit f964bb6
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.antMatchers(HttpMethod.PUT)
.authenticated()
.antMatchers(HttpMethod.DELETE)
.authenticated()
.anyRequest()
.authenticated());

return http.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import kr.joberchip.server.v1._utils.ApiResponse;
import kr.joberchip.server.v1.user.controller.dto.UpdateUserRequestDTO;
import kr.joberchip.server.v1.user.controller.dto.UpdateUserResponseDTO;
import kr.joberchip.server.v1.user.controller.dto.UserProfileDTO;
import kr.joberchip.server.v1.user.controller.dto.UserRequest;
import kr.joberchip.server.v1.user.service.UserService;
import lombok.*;
Expand Down Expand Up @@ -35,10 +36,23 @@ public ResponseEntity<?> login(@RequestBody @Valid UserRequest loginUser, Errors

@PutMapping("/update")
public ResponseEntity<ApiResponse.Result<UpdateUserResponseDTO>> updateUserInfo(
@AuthenticationPrincipal CustomUserDetails loginUser,
UpdateUserRequestDTO updateUserRequestDTO) {
@AuthenticationPrincipal CustomUserDetails loginUser,
UpdateUserRequestDTO updateUserRequestDTO) {
UpdateUserResponseDTO responseDTO = userService.updateUserInfo(loginUser, updateUserRequestDTO);

return ResponseEntity.ok(ApiResponse.success(responseDTO));
}

@GetMapping("/profile")
public ApiResponse.Result<UserProfileDTO> userProfile(
@AuthenticationPrincipal CustomUserDetails loginUser) {

log.info("[UserController][GET] Login User - {}", loginUser.getUsername());

UserProfileDTO response = userService.getUserProfile(loginUser.user().getUserId());

log.info("[UserController][GET] Response - {}", response);

return ApiResponse.success(response);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.joberchip.server.v1.user.controller.dto;

import javax.persistence.Lob;
import kr.joberchip.core.user.User;

public record UserProfileDTO(
Long userId, String username, String nickname, @Lob String profileImageLink) {
public static UserProfileDTO fromEntity(User user) {
return new UserProfileDTO(
user.getUserId(), user.getUsername(), user.getNickname(), user.getProfileImageLink());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
import kr.joberchip.server.v1._config.security.CustomUserDetails;
import kr.joberchip.server.v1._config.security.JwtProvider;
import kr.joberchip.server.v1._errors.ErrorMessage;
import kr.joberchip.server.v1._errors.exceptions.ApiClientException;
import kr.joberchip.server.v1._errors.exceptions.DuplicatedUsernameException;
import kr.joberchip.server.v1._errors.exceptions.UserNotFoundException;
import kr.joberchip.server.v1.storage.service.S3StorageService;
import kr.joberchip.server.v1.user.controller.dto.UpdateUserResponseDTO;
import kr.joberchip.server.v1.user.controller.dto.UpdateUserRequestDTO;
import kr.joberchip.server.v1.user.controller.dto.UpdateUserResponseDTO;
import kr.joberchip.server.v1.user.controller.dto.UserProfileDTO;
import kr.joberchip.server.v1.user.controller.dto.UserRequest;
import kr.joberchip.server.v1.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -68,17 +70,30 @@ private Optional<User> checkPassword(UserRequest loginUser, Optional<User> userO
}

@Transactional
public UpdateUserResponseDTO updateUserInfo(CustomUserDetails loginUser, UpdateUserRequestDTO updateUserRequestDTO) {
User user = userRepository.findByUsername(loginUser.getUsername())
public UpdateUserResponseDTO updateUserInfo(
CustomUserDetails loginUser, UpdateUserRequestDTO updateUserRequestDTO) {
User user =
userRepository
.findByUsername(loginUser.getUsername())
.orElseThrow(() -> new UserNotFoundException(ErrorMessage.USER_ENTITY_NOT_FOUND));

if(updateUserRequestDTO.nickname() != null) user.setNickname(updateUserRequestDTO.nickname());
if(updateUserRequestDTO.profileImageLink() != null) {
if (updateUserRequestDTO.nickname() != null) user.setNickname(updateUserRequestDTO.nickname());
if (updateUserRequestDTO.profileImageLink() != null) {
s3StorageService.delete(user.getProfileImageLink());
user.setProfileImageLink(s3StorageService.store(updateUserRequestDTO.profileImageLink()));
}

userRepository.save(user);
return UpdateUserResponseDTO.fromEntity(user);
}

public UserProfileDTO getUserProfile(Long userId) {

User currentUser =
userRepository
.findById(userId)
.orElseThrow(() -> new ApiClientException(ErrorMessage.USER_ENTITY_NOT_FOUND));

return UserProfileDTO.fromEntity(currentUser);
}
}

0 comments on commit f964bb6

Please sign in to comment.