From e95b706907cb060429086e38e7d322ffc2b680e1 Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Wed, 16 Oct 2024 09:49:32 +0900 Subject: [PATCH 1/3] feat:[#54]- add swagger Operation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 간단한 주석을 대체하는 Swagger를 작성한다. --- .../user/controller/ArtistController.java | 18 +++++++----------- .../user/controller/UserController.java | 15 +++++++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/helpmeCookies/user/controller/ArtistController.java b/src/main/java/com/helpmeCookies/user/controller/ArtistController.java index 8640807..607f425 100644 --- a/src/main/java/com/helpmeCookies/user/controller/ArtistController.java +++ b/src/main/java/com/helpmeCookies/user/controller/ArtistController.java @@ -15,16 +15,19 @@ import com.helpmeCookies.user.dto.response.ArtistDetailsRes; import com.helpmeCookies.user.service.ArtistService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; @RestController @RequiredArgsConstructor +@Tag(name = "작가 관련 기능", description = "작가 관련 API") public class ArtistController { private final ArtistService artistService; private final JwtProvider jwtProvider; - // 작가 등록(학생) + @Operation(summary = "학생 작가 등록", description = "학생 작가 등록") @PostMapping("/v1/artists/students") public ResponseEntity registerStudents( @RequestBody StudentArtistReq artistDetailsReq, @@ -34,7 +37,7 @@ public ResponseEntity registerStudents( return ResponseEntity.ok().build(); } - // 작가 등록(사업자) + @Operation(summary = "사업자 작가 등록", description = "사업자 작가 등록") @PostMapping("/v1/artists/bussinesses") public ResponseEntity registerbussinsess( @RequestBody BusinessArtistReq businessArtistReq, @@ -44,14 +47,7 @@ public ResponseEntity registerbussinsess( return ResponseEntity.ok().build(); } - // 작가 목록 조회(페이징) - // TODO: 6주차 회의 이후 추가 - @GetMapping("/v1/artists") - public String getArtists() { - return "ok"; - } - - // 작가 프로필 조회 + @Operation(summary = "작가 프로필 조회", description = "작가 프로필 조회") @GetMapping("/v1/artists/{userId}") public ArtistDetailsRes getArtist( @AuthenticationPrincipal JwtUser jwtUser, @@ -60,7 +56,7 @@ public ArtistDetailsRes getArtist( return artistService.getArtistDetails(userId); } - // 자기 자신 작가 프로필 조회 + @Operation(summary = "작가 프로필 조회", description = "자신의 작가 프로필 조회") @GetMapping("/v1/artist") public ArtistDetailsRes getArtist( @AuthenticationPrincipal JwtUser jwtUser diff --git a/src/main/java/com/helpmeCookies/user/controller/UserController.java b/src/main/java/com/helpmeCookies/user/controller/UserController.java index ead4878..dbbe184 100644 --- a/src/main/java/com/helpmeCookies/user/controller/UserController.java +++ b/src/main/java/com/helpmeCookies/user/controller/UserController.java @@ -23,15 +23,18 @@ import com.helpmeCookies.user.dto.response.UserFollowingRes; import com.helpmeCookies.user.service.UserService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.tags.Tag; import lombok.RequiredArgsConstructor; @RestController @RequiredArgsConstructor +@Tag(name = "유저 및 팔로우 기능", description = "유저 및 팔로우 기능과 관련된 API") public class UserController { private final UserService userService; - // 유저 일반 정보 조회 + @Operation(summary = "유저 일반 정보 조회", description = "로그인한 유저의 username, imageUrl, hashtag를 조회한다.") @GetMapping("/v1/users") public UserCommonInfoRes getUsers( @AuthenticationPrincipal JwtUser jwtUser @@ -40,6 +43,7 @@ public UserCommonInfoRes getUsers( } // 유저 상세 정보 조회 + @Operation(summary = "유저 상세 정보 조회", description = "로그인한 유저의 이름, 주소를 비롯한 개인정보를 함께 조회한다.") @GetMapping("/v1/users/details") public UserDetailsInfoRes getUserDetails( @AuthenticationPrincipal JwtUser jwtUser @@ -48,6 +52,7 @@ public UserDetailsInfoRes getUserDetails( } // 유저 타입 조회 + @Operation(summary = "유저 타입 조회", description = "로그인한 유저의 타입과 권한을 조회한다.") @GetMapping("/v1/users/type") public UserTypeDto getUserType( @AuthenticationPrincipal JwtUser jwtUser @@ -56,6 +61,7 @@ public UserTypeDto getUserType( } // 유저 정보 수정 + @Operation(summary = "유저 정보 수정", description = "로그인한 유저의 개인정보를 수정한다.") @PutMapping("/v1/users") public String updateUserInfo( @AuthenticationPrincipal JwtUser jwtUser, @@ -66,7 +72,7 @@ public String updateUserInfo( return "ok"; } - // 아티스트 팔로우하기 + @Operation(summary = "아티스트 팔로우하기", description = "로그인한 유저가 특정 아티스트를 팔로우한다.") @PostMapping("/v1/users/following/{artistId}") public String followArtist( @AuthenticationPrincipal JwtUser jwtUser, @@ -76,7 +82,7 @@ public String followArtist( return "ok"; } - // 아티스트 팔로우 취소하기 + @Operation(summary = "아티스트 팔로우 취소하기", description = "로그인한 유저가 특정 아티스트를 팔로우 취소한다.") @DeleteMapping("/v1/users/following/{artistId}") public String unfollowArtist( @AuthenticationPrincipal JwtUser jwtUser, @@ -86,7 +92,7 @@ public String unfollowArtist( return "ok"; } - // 유저 팔로우 목록 조회 + @Operation(summary = "팔로잉 목록 조회", description = "로그인한 유저의 팔로우한 아티스트 목록을 조회한다.") @GetMapping("/v1/users/following") public Page getFollowingList( @AuthenticationPrincipal JwtUser jwtUser, @@ -96,6 +102,7 @@ public Page getFollowingList( } // 유저 탈퇴 + @Operation(summary = "유저 탈퇴", description = "로그인한 유저의 정보를 삭제한다.") @DeleteMapping("/v1/users") public String deleteUser( @AuthenticationPrincipal JwtUser jwtUser From 5aded00d5cdc35f5b5e4e8daac37e458300824b2 Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Wed, 16 Oct 2024 09:50:24 +0900 Subject: [PATCH 2/3] refactor:[#54]- refact Exception MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 예외처리를 보다 상세하게 한다. --- .../exception/GlobalExceptionHandler.java | 8 +++++++- .../user/ResourceNotFoundException.java | 4 ++-- .../user/service/ArtistService.java | 12 +++++++----- .../user/service/UserService.java | 18 +++++++++--------- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java b/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java index 0872d56..ad5237a 100644 --- a/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java @@ -4,12 +4,18 @@ import org.springframework.web.bind.annotation.ExceptionHandler; import com.helpmeCookies.global.exception.user.ResourceNotFoundException; +import com.sun.jdi.request.DuplicateRequestException; @ControllerAdvice public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) public String handleResourceNotFoundException() { - return "Resource not found"; + return "해당 리소스를 찾을 수 없습니다."; + } + + @ExceptionHandler(DuplicateRequestException.class) + public String handleDuplicateRequestException() { + return "이미 생성되었거나 중복된 요청입니다."; } } diff --git a/src/main/java/com/helpmeCookies/global/exception/user/ResourceNotFoundException.java b/src/main/java/com/helpmeCookies/global/exception/user/ResourceNotFoundException.java index 38faecf..6feea50 100644 --- a/src/main/java/com/helpmeCookies/global/exception/user/ResourceNotFoundException.java +++ b/src/main/java/com/helpmeCookies/global/exception/user/ResourceNotFoundException.java @@ -1,7 +1,7 @@ package com.helpmeCookies.global.exception.user; public class ResourceNotFoundException extends RuntimeException { - public ResourceNotFoundException() { - super("Resource not found"); + public ResourceNotFoundException(String message) { + super(message); } } diff --git a/src/main/java/com/helpmeCookies/user/service/ArtistService.java b/src/main/java/com/helpmeCookies/user/service/ArtistService.java index b90d2ff..015f319 100644 --- a/src/main/java/com/helpmeCookies/user/service/ArtistService.java +++ b/src/main/java/com/helpmeCookies/user/service/ArtistService.java @@ -3,6 +3,7 @@ import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.helpmeCookies.global.exception.user.ResourceNotFoundException; import com.helpmeCookies.user.dto.ArtistInfoDto; import com.helpmeCookies.user.dto.BusinessArtistDto; import com.helpmeCookies.user.dto.StudentArtistDto; @@ -18,6 +19,7 @@ import com.helpmeCookies.user.repository.BusinessArtistRepository; import com.helpmeCookies.user.repository.StudentArtistRepository; import com.helpmeCookies.user.repository.UserRepository; +import com.sun.jdi.request.DuplicateRequestException; import lombok.RequiredArgsConstructor; @@ -66,7 +68,7 @@ public void registerBusinessArtist(BusinessArtistReq businessArtistReq, Long use User user = userRepository.getReferenceById(userId); if (artistInfoRepository.existsByUserId(userId)) { - throw new IllegalArgumentException("이미 등록된 아티스트입니다."); + throw new DuplicateRequestException("이미 등록된 아티스트입니다."); } // BusinessArtist 생성 @@ -93,22 +95,22 @@ public void registerBusinessArtist(BusinessArtistReq businessArtistReq, Long use @Transactional public ArtistDetailsRes getArtistDetails(Long userId) { ArtistInfo artistInfo = artistInfoRepository.findByUserId(userId) - .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 아티스트입니다.")); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); ArtistInfoDto artistInfoDto = ArtistInfoDto.fromEntity(artistInfo); switch (artistInfo.getArtistType()) { case STUDENT: StudentArtist studentArtist = studentArtistRepository.findByArtistInfo(artistInfo) - .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 학생 아티스트입니다.")); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 학생 아티스트입니다.")); StudentArtistDto studentArtistDto = StudentArtistDto.from(studentArtist); return ArtistDetailsRes.from(artistInfoDto, studentArtistDto); case BUSINESS: BusinessArtist businessArtist = businessArtistRepository.findByArtistInfo(artistInfo) - .orElseThrow(() -> new IllegalArgumentException("존재하지 않는 사업자 아티스트입니다.")); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 사업자 아티스트입니다.")); BusinessArtistDto businessArtistDto = BusinessArtistDto.from(businessArtist); return ArtistDetailsRes.from(artistInfoDto, businessArtistDto); default: - throw new IllegalArgumentException("존재하지 않는 아티스트입니다."); + throw new ResourceNotFoundException("존재하지 않는 아티스트입니다."); } } } diff --git a/src/main/java/com/helpmeCookies/user/service/UserService.java b/src/main/java/com/helpmeCookies/user/service/UserService.java index 63fc4b4..139405b 100644 --- a/src/main/java/com/helpmeCookies/user/service/UserService.java +++ b/src/main/java/com/helpmeCookies/user/service/UserService.java @@ -7,7 +7,6 @@ import com.helpmeCookies.global.exception.user.ResourceNotFoundException; import com.helpmeCookies.user.dto.UserDto; -import com.helpmeCookies.user.dto.UserFollowingDto; import com.helpmeCookies.user.dto.UserInfoDto; import com.helpmeCookies.user.dto.UserTypeDto; import com.helpmeCookies.user.dto.response.UserFollowingRes; @@ -18,6 +17,7 @@ import com.helpmeCookies.user.repository.ArtistInfoRepository; import com.helpmeCookies.user.repository.SocialRepository; import com.helpmeCookies.user.repository.UserRepository; +import com.sun.jdi.request.DuplicateRequestException; import lombok.RequiredArgsConstructor; @@ -32,7 +32,7 @@ public class UserService { @Transactional public UserInfoDto getUserInfo(Long userId) { UserInfo userInfo = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException()) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")) .getUserInfo(); return UserInfoDto.fromEntity(userInfo); @@ -42,7 +42,7 @@ public UserInfoDto getUserInfo(Long userId) { public UserDto updateUserInfo(UserInfoDto userInfoDto, Long userId) { User existingUser = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); existingUser.updateUserInfo(userInfoDto.toEntity()); @@ -71,13 +71,13 @@ public Page getFollowingWithPaging(Long userId, Pageable pagea @Transactional public void followArtist(Long userId, Long artistId) { User user = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); if (socialRepository.existsByFollowerAndFollowing(user, artistInfo)) { - throw new IllegalArgumentException("이미 팔로우한 아티스트입니다."); + throw new DuplicateRequestException("이미 팔로우한 아티스트입니다."); } Social social = Social.builder() @@ -91,13 +91,13 @@ public void followArtist(Long userId, Long artistId) { @Transactional public void unfollowArtist(Long userId, Long artistId) { User user = userRepository.findById(userId) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 유저입니다.")); ArtistInfo artistInfo = artistInfoRepository.findByUserId(artistId) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); Social social = socialRepository.findByFollowerAndFollowing(user, artistInfo) - .orElseThrow(() -> new ResourceNotFoundException()); + .orElseThrow(() -> new ResourceNotFoundException("팔로우하지 않은 아티스트입니다.")); socialRepository.delete(social); } From acdc50b8d985d9831bed83061b031a113c36f247 Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Wed, 16 Oct 2024 09:51:17 +0900 Subject: [PATCH 3/3] refactor:[#54]- refact using final MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 변하지 않아야 하는 값에 final을 붙인다. --- .../java/com/helpmeCookies/global/jwt/JwtToken.java | 4 ++-- .../java/com/helpmeCookies/global/jwt/JwtUser.java | 12 ++++++------ 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/helpmeCookies/global/jwt/JwtToken.java b/src/main/java/com/helpmeCookies/global/jwt/JwtToken.java index ca5c80e..83fb71a 100644 --- a/src/main/java/com/helpmeCookies/global/jwt/JwtToken.java +++ b/src/main/java/com/helpmeCookies/global/jwt/JwtToken.java @@ -6,6 +6,6 @@ @Getter @Builder public class JwtToken { - private String accessToken; - private String refreshToken; + private final String accessToken; + private final String refreshToken; } \ No newline at end of file diff --git a/src/main/java/com/helpmeCookies/global/jwt/JwtUser.java b/src/main/java/com/helpmeCookies/global/jwt/JwtUser.java index e66d7b0..c065a6c 100644 --- a/src/main/java/com/helpmeCookies/global/jwt/JwtUser.java +++ b/src/main/java/com/helpmeCookies/global/jwt/JwtUser.java @@ -13,8 +13,8 @@ @Builder @Getter public class JwtUser implements UserDetails { - private Long id; - private String username; + private final Long id; + private final String username; private Collection authorities; public static JwtUser of(Long id) { @@ -41,22 +41,22 @@ public String getUsername() { @Override public boolean isAccountNonExpired() { - return true; + return false; } @Override public boolean isAccountNonLocked() { - return true; + return false; } @Override public boolean isCredentialsNonExpired() { - return true; + return false; } @Override public boolean isEnabled() { - return true; + return false; } } \ No newline at end of file