Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions src/main/java/Gotcha/domain/user/controller/UserController.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package Gotcha.domain.user.controller;

import Gotcha.common.api.SuccessRes;
import Gotcha.common.jwt.auth.SecurityUserDetails;
import Gotcha.domain.user.api.UserApi;
import Gotcha.domain.user.dto.NicknameReq;
import Gotcha.domain.user.dto.UserInfoRes;
import Gotcha.domain.user.service.UserService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
Expand All @@ -25,4 +30,11 @@ public ResponseEntity<?> checkNickname(@Valid @RequestBody NicknameReq nicknameR

return ResponseEntity.ok(SuccessRes.from("사용 가능한 닉네임입니다."));
}

@GetMapping("/me/main-info")
public ResponseEntity<?> getUserInfo(@AuthenticationPrincipal SecurityUserDetails userDetails){
UserInfoRes userInfoRes = userService.getUserInfo(userDetails.getId(), userDetails.getRole());

return ResponseEntity.status(HttpStatus.OK).body(userInfoRes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
@ToString
public enum UserExceptionCode implements ExceptionCode {
NICKNAME_EXIST(HttpStatus.CONFLICT, "이미 존재하는 닉네임입니다."),
EMAIL_EXIST(HttpStatus.CONFLICT, "이미 가입된 이메일입니다.");
EMAIL_EXIST(HttpStatus.CONFLICT, "이미 가입된 이메일입니다."),
INVALID_USERID(HttpStatus.NOT_FOUND, "존재하지 않는 사용자입니다.");

private final HttpStatus status;
private final String message;
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/Gotcha/domain/user/service/UserService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@

import Gotcha.common.exception.CustomException;
import Gotcha.common.util.RedisUtil;
import Gotcha.domain.user.dto.UserInfoRes;
import Gotcha.domain.user.entity.Role;
import Gotcha.domain.user.entity.User;
import Gotcha.domain.user.exceptionCode.UserExceptionCode;
import Gotcha.domain.user.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

import static Gotcha.common.redis.RedisProperties.GUEST_KEY_PREFIX;
import static Gotcha.common.redis.RedisProperties.NICKNAME_VERIFY_KEY_PREFIX;

@Service
Expand Down Expand Up @@ -35,4 +41,24 @@ public void checkEmail(String email) {
throw new CustomException(UserExceptionCode.EMAIL_EXIST);
}
}

@Transactional(readOnly = true)
public UserInfoRes getUserInfo(Long userId, Role role){
User user = switch (role){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

switch문 깔끔하네요!! 맘에 들어요

case GUEST -> findGuestByGuestId(userId);
case USER,ADMIN -> findUserByUserId(userId);
default -> throw new CustomException(UserExceptionCode.INVALID_USERID);
};
return UserInfoRes.fromEntity(user);
}

private User findUserByUserId(Long userId){
return userRepository.findById(userId)
.orElseThrow(()->new CustomException(UserExceptionCode.INVALID_USERID));
}

private User findGuestByGuestId(Long guestId){
return Optional.ofNullable((User) redisUtil.getData(GUEST_KEY_PREFIX + guestId))
.orElseThrow(()-> new CustomException(UserExceptionCode.INVALID_USERID));
}
}