From d2b9d080934d2b653f6c3335f5a40e85b34676aa Mon Sep 17 00:00:00 2001 From: yooonwodyd Date: Fri, 8 Nov 2024 15:14:45 +0900 Subject: [PATCH] refactor:[#84]- refact Artist API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 아티스트 API 스펙 변경에 따른 세부사항 수정 --- .../exception/GlobalExceptionHandler.java | 4 +- .../global/security/WebSecurityConfig.java | 3 +- .../user/controller/ArtistController.java | 15 ++++++-- .../controller/apiDocs/ArtistApiDocs.java | 7 ++-- .../user/dto/response/ArtistDetailsRes.java | 16 +++++--- .../user/repository/SocialRepository.java | 1 + .../user/service/ArtistService.java | 38 +++++++++++++++++-- 7 files changed, 64 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java b/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java index 9705e08..2194705 100644 --- a/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java +++ b/src/main/java/com/helpmeCookies/global/exception/GlobalExceptionHandler.java @@ -13,8 +13,8 @@ public class GlobalExceptionHandler { @ExceptionHandler(ResourceNotFoundException.class) - public ResponseEntity> handleResourceNotFoundException() { - return ResponseEntity.badRequest().body(ApiResponse.error(HttpStatus.BAD_REQUEST,"해당 리소스를 찾을 수 없습니다.")); + public ResponseEntity> handleResourceNotFoundException(ResourceNotFoundException e) { + return ResponseEntity.badRequest().body(ApiResponse.error(HttpStatus.BAD_REQUEST, e.getMessage())); } @ExceptionHandler(DuplicateRequestException.class) diff --git a/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java b/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java index 57cf551..7c1ffa2 100644 --- a/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java +++ b/src/main/java/com/helpmeCookies/global/security/WebSecurityConfig.java @@ -75,8 +75,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { "/v1/**", "swagger-ui/**", "/test/signup", - "/v1/artist", - "/v1/artists" + "/v1/artists/**" ).permitAll() .anyRequest().authenticated() ).exceptionHandling((exception) -> exception diff --git a/src/main/java/com/helpmeCookies/user/controller/ArtistController.java b/src/main/java/com/helpmeCookies/user/controller/ArtistController.java index f9530ff..3ea8ba6 100644 --- a/src/main/java/com/helpmeCookies/user/controller/ArtistController.java +++ b/src/main/java/com/helpmeCookies/user/controller/ArtistController.java @@ -43,11 +43,18 @@ public ResponseEntity> registerbussinsess( return ResponseEntity.ok((ApiResponse.success(SuccessCode.OK))); } - @GetMapping("/v1/artists/{userId}") - public ResponseEntity> getArtist( - @PathVariable Long userId + @GetMapping("/v1/artists/{artistInfoId}") + public ResponseEntity> getArtistPublicDetails( + @PathVariable Long artistInfoId, + @AuthenticationPrincipal JwtUser jwtUser ) { - ArtistDetailsRes artistDetailsRes = artistService.getArtistDetails(userId); + ArtistDetailsRes artistDetailsRes; + + if (jwtUser == null) { + artistDetailsRes = artistService.getArtistDetails(artistInfoId); + } else { + artistDetailsRes = artistService.getArtistPublicDetails(artistInfoId, jwtUser.getId()); + } return ResponseEntity.ok((ApiResponse.success(SuccessCode.OK, artistDetailsRes))); } diff --git a/src/main/java/com/helpmeCookies/user/controller/apiDocs/ArtistApiDocs.java b/src/main/java/com/helpmeCookies/user/controller/apiDocs/ArtistApiDocs.java index 95c6ea8..6e26be3 100644 --- a/src/main/java/com/helpmeCookies/user/controller/apiDocs/ArtistApiDocs.java +++ b/src/main/java/com/helpmeCookies/user/controller/apiDocs/ArtistApiDocs.java @@ -35,9 +35,10 @@ ResponseEntity> registerbussinsess( ); @Operation(summary = "작가 프로필 조회", description = "작가 프로필 조회") - @GetMapping("/v1/artists/{userId}") - ResponseEntity> getArtist( - @PathVariable Long userId + @GetMapping("/v1/artists/{artistInfoId}") + public ResponseEntity> getArtistPublicDetails( + @PathVariable Long artistInfoId, + @AuthenticationPrincipal JwtUser jwtUser ); @Operation(summary = "작가 자신의 프로필 조회", description = "작가 자신의 프로필 조회") diff --git a/src/main/java/com/helpmeCookies/user/dto/response/ArtistDetailsRes.java b/src/main/java/com/helpmeCookies/user/dto/response/ArtistDetailsRes.java index 71dbaa5..55cfac4 100644 --- a/src/main/java/com/helpmeCookies/user/dto/response/ArtistDetailsRes.java +++ b/src/main/java/com/helpmeCookies/user/dto/response/ArtistDetailsRes.java @@ -5,32 +5,38 @@ import com.helpmeCookies.user.dto.StudentArtistDto; public record ArtistDetailsRes( + Long id, String nickname, String description, Long totalFollowers, Long totalLikes, String about, - String ImageUrl + String ImageUrl, + boolean isFollowed ) { - public static ArtistDetailsRes from(ArtistInfoDto artistInfoDto, BusinessArtistDto businessArtistDto) { + public static ArtistDetailsRes from(ArtistInfoDto artistInfoDto, BusinessArtistDto businessArtistDto, boolean isFollowed) { return new ArtistDetailsRes( + artistInfoDto.id(), artistInfoDto.nickname(), businessArtistDto.headName(), artistInfoDto.totalFollowers(), artistInfoDto.totalLikes(), artistInfoDto.about(), - artistInfoDto.artistImageUrl() + artistInfoDto.artistImageUrl(), + isFollowed ); } - public static ArtistDetailsRes from(ArtistInfoDto artistInfoDto, StudentArtistDto studentArtistDto) { + public static ArtistDetailsRes from(ArtistInfoDto artistInfoDto, StudentArtistDto studentArtistDto, boolean isFollowed) { return new ArtistDetailsRes( + artistInfoDto.id(), artistInfoDto.nickname(), studentArtistDto.schoolName(), artistInfoDto.totalFollowers(), artistInfoDto.totalLikes(), artistInfoDto.about(), - artistInfoDto.artistImageUrl() + artistInfoDto.artistImageUrl(), + isFollowed ); } } diff --git a/src/main/java/com/helpmeCookies/user/repository/SocialRepository.java b/src/main/java/com/helpmeCookies/user/repository/SocialRepository.java index 4d10d75..2467665 100644 --- a/src/main/java/com/helpmeCookies/user/repository/SocialRepository.java +++ b/src/main/java/com/helpmeCookies/user/repository/SocialRepository.java @@ -14,5 +14,6 @@ @Repository public interface SocialRepository extends JpaRepository { Boolean existsByFollowerAndFollowing(User follower, ArtistInfo following); + Boolean existsByFollowerIdAndFollowingId(Long followerId, Long followingId); Optional findByFollowerAndFollowing(User follower, ArtistInfo following); } diff --git a/src/main/java/com/helpmeCookies/user/service/ArtistService.java b/src/main/java/com/helpmeCookies/user/service/ArtistService.java index c36475e..a7c82b9 100644 --- a/src/main/java/com/helpmeCookies/user/service/ArtistService.java +++ b/src/main/java/com/helpmeCookies/user/service/ArtistService.java @@ -15,6 +15,7 @@ import com.helpmeCookies.user.entity.User; import com.helpmeCookies.user.repository.ArtistInfoRepository; import com.helpmeCookies.user.repository.BusinessArtistRepository; +import com.helpmeCookies.user.repository.SocialRepository; import com.helpmeCookies.user.repository.StudentArtistRepository; import com.helpmeCookies.user.repository.UserRepository; import com.sun.jdi.request.DuplicateRequestException; @@ -30,6 +31,8 @@ public class ArtistService { private final BusinessArtistRepository businessArtistRepository; private final StudentArtistRepository studentArtistRepository; private final ArtistInfoRepository artistInfoRepository; + private final UserService userService; + private final SocialRepository socialRepository; @Transactional public void registerStudentsArtist(StudentArtistReq studentArtistReq, Long userId) { @@ -95,8 +98,8 @@ public void registerBusinessArtist(BusinessArtistReq businessArtistReq, Long use } @Transactional - public ArtistDetailsRes getArtistDetails(Long userId) { - ArtistInfo artistInfo = artistInfoRepository.findByUserId(userId) + public ArtistDetailsRes getArtistDetails(Long artistInfoId) { + ArtistInfo artistInfo = artistInfoRepository.findById(artistInfoId) .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); ArtistInfoDto artistInfoDto = ArtistInfoDto.fromEntity(artistInfo); @@ -105,12 +108,39 @@ public ArtistDetailsRes getArtistDetails(Long userId) { StudentArtist studentArtist = studentArtistRepository.findByArtistInfo(artistInfo) .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 학생 아티스트입니다.")); StudentArtistDto studentArtistDto = StudentArtistDto.from(studentArtist); - return ArtistDetailsRes.from(artistInfoDto, studentArtistDto); + return ArtistDetailsRes.from(artistInfoDto, studentArtistDto,false); case BUSINESS: BusinessArtist businessArtist = businessArtistRepository.findByArtistInfo(artistInfo) .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 사업자 아티스트입니다.")); BusinessArtistDto businessArtistDto = BusinessArtistDto.from(businessArtist); - return ArtistDetailsRes.from(artistInfoDto, businessArtistDto); + return ArtistDetailsRes.from(artistInfoDto, businessArtistDto,false); + default: + throw new ResourceNotFoundException("존재하지 않는 아티스트입니다."); + } + } + + + // TODO: 중복되는 메서드 분리 필요. + @Transactional + public ArtistDetailsRes getArtistPublicDetails(Long artistInfoId, Long followerId) { + + ArtistInfo artistInfo = artistInfoRepository.findById(artistInfoId) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 아티스트입니다.")); + ArtistInfoDto artistInfoDto = ArtistInfoDto.fromEntity(artistInfo); + + boolean isFollowed = socialRepository.existsByFollowerIdAndFollowingId(followerId, artistInfoId); + + switch (artistInfo.getArtistType()) { + case STUDENT: + StudentArtist studentArtist = studentArtistRepository.findByArtistInfo(artistInfo) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 학생 아티스트입니다.")); + StudentArtistDto studentArtistDto = StudentArtistDto.from(studentArtist); + return ArtistDetailsRes.from(artistInfoDto, studentArtistDto, isFollowed); + case BUSINESS: + BusinessArtist businessArtist = businessArtistRepository.findByArtistInfo(artistInfo) + .orElseThrow(() -> new ResourceNotFoundException("존재하지 않는 사업자 아티스트입니다.")); + BusinessArtistDto businessArtistDto = BusinessArtistDto.from(businessArtist); + return ArtistDetailsRes.from(artistInfoDto, businessArtistDto, isFollowed); default: throw new ResourceNotFoundException("존재하지 않는 아티스트입니다."); }