-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
#63 user info api
- Loading branch information
Showing
7 changed files
with
170 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
src/main/java/com/book/backend/domain/user/controller/UserController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package com.book.backend.domain.user.controller; | ||
|
||
import com.book.backend.domain.user.dto.UserDto; | ||
import com.book.backend.domain.user.dto.UserInfoDto; | ||
import com.book.backend.domain.user.entity.User; | ||
import com.book.backend.domain.user.mapper.UserMapper; | ||
import com.book.backend.domain.user.service.UserService; | ||
import com.book.backend.global.ResponseTemplate; | ||
import io.swagger.v3.oas.annotations.Operation; | ||
import io.swagger.v3.oas.annotations.media.Content; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import io.swagger.v3.oas.annotations.responses.ApiResponse; | ||
import jakarta.validation.Valid; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.web.bind.annotation.*; | ||
|
||
@RestController | ||
@RequestMapping("/api/user") | ||
@RequiredArgsConstructor | ||
@Slf4j | ||
public class UserController { | ||
private final UserService userService; | ||
private final ResponseTemplate responseTemplate; | ||
private final UserMapper userMapper; | ||
|
||
@Operation(summary = "유저 정보 불러오기", description = "유저 정보를 불러옵니다.", | ||
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserDto.class)), | ||
description = UserDto.description)}) | ||
@GetMapping("/info") | ||
public ResponseEntity<?> getUserInfo() { | ||
log.trace("UserController > getUserInfo()"); | ||
|
||
User user = userService.loadLoggedinUser(); | ||
UserDto userDto = userMapper.convertToUserDto(user); | ||
|
||
return responseTemplate.success(userDto, HttpStatus.OK); | ||
} | ||
|
||
@Operation(summary = "유저 정보 수정", description = "유저의 변경 가능한 정보를 수정합니다.", | ||
responses = {@ApiResponse(responseCode = "200", content = @Content(schema = @Schema(implementation = UserInfoDto.class)), | ||
description = UserInfoDto.description)}) | ||
@PutMapping("/info/edit") | ||
public ResponseEntity<?> editUserInfo(@Valid @RequestBody UserInfoDto requestDto) { | ||
log.trace("UserController > editUserInfo()"); | ||
|
||
User user = userService.loadLoggedinUser(); | ||
User updatedUser = userService.updateUserInfo(user, requestDto); | ||
|
||
UserInfoDto userInfoDto = userMapper.convertToUserInfoDto(updatedUser); | ||
|
||
return responseTemplate.success(userInfoDto, HttpStatus.OK); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/java/com/book/backend/domain/user/dto/UserInfoDto.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.book.backend.domain.user.dto; | ||
|
||
import jakarta.validation.constraints.NotBlank; | ||
import jakarta.validation.constraints.Past; | ||
import jakarta.validation.constraints.Pattern; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.Setter; | ||
|
||
import java.time.LocalDate; | ||
|
||
@Getter | ||
@Setter | ||
@NoArgsConstructor | ||
public class UserInfoDto { | ||
@NotBlank(message = "닉네임은 필수 입력값입니다.") | ||
private String nickname; | ||
|
||
@NotBlank(message = "성별은 필수 입력값입니다.") | ||
@Pattern(regexp = "NOT_SELECTED|MAN|WOMAN", message = "성별은 NOT_SELECTED, MAN, WOMAN 중 하나여야 합니다.") | ||
private String gender; | ||
|
||
@Past(message = "현재 날짜보다 이전 날짜여야 합니다.") | ||
private LocalDate birthDate; | ||
|
||
public static final String description = | ||
"nickname : 닉네임 | " + | ||
"gender : 성별 | " + | ||
"birthDate : 생일"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 47 additions & 2 deletions
49
src/main/java/com/book/backend/domain/user/service/UserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,60 @@ | ||
package com.book.backend.domain.user.service; | ||
|
||
import com.book.backend.domain.user.dto.UserInfoDto; | ||
import com.book.backend.domain.user.entity.User; | ||
import com.book.backend.domain.user.mapper.UserMapper; | ||
import com.book.backend.domain.user.repository.UserRepository; | ||
import com.book.backend.exception.CustomException; | ||
import com.book.backend.exception.ErrorCode; | ||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import org.springframework.transaction.annotation.Transactional; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
@Transactional | ||
@Transactional(readOnly = true) | ||
public class UserService { | ||
private final UserRepository userRepository; | ||
private final UserMapper userMapper; | ||
|
||
// TODO: 기능에 따른 회원 서비스 메서드 추가 예정 | ||
public User loadLoggedinUser() { | ||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); | ||
String username = authentication.getName(); | ||
|
||
return findByUsername(username); | ||
} | ||
|
||
public User findByUsername(String username) { | ||
try { | ||
return findByLoginId(username); | ||
} catch (IllegalArgumentException e) { | ||
return findByKakaoId(username); | ||
} | ||
} | ||
|
||
public User findByLoginId(String loginId) { | ||
return userRepository.findByLoginId(loginId) | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
|
||
public User findByKakaoId(String kakaoId) { | ||
return userRepository.findByKakaoId(kakaoId) | ||
.orElseThrow(IllegalArgumentException::new); | ||
} | ||
|
||
@Transactional | ||
public User updateUserInfo(User user, UserInfoDto requestDto) { | ||
if (user == null) { | ||
throw new CustomException(ErrorCode.USER_NOT_FOUND); | ||
} | ||
|
||
user.setNickname(requestDto.getNickname()); | ||
user.setGender(userMapper.convertStringToGender(requestDto.getGender())); | ||
user.setBirthDate(requestDto.getBirthDate()); | ||
userRepository.save(user); | ||
|
||
return user; | ||
} | ||
} |