Skip to content

Commit

Permalink
refactor:[kakao-tech-campus-2nd-step3#84]- refact Artist API
Browse files Browse the repository at this point in the history
아티스트 API 스펙 변경에 따른 세부사항 수정
  • Loading branch information
yooonwodyd committed Nov 8, 2024
1 parent e0c5699 commit d2b9d08
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
public class GlobalExceptionHandler {

@ExceptionHandler(ResourceNotFoundException.class)
public ResponseEntity<ApiResponse<Void>> handleResourceNotFoundException() {
return ResponseEntity.badRequest().body(ApiResponse.error(HttpStatus.BAD_REQUEST,"해당 리소스를 찾을 수 없습니다."));
public ResponseEntity<ApiResponse<Void>> handleResourceNotFoundException(ResourceNotFoundException e) {
return ResponseEntity.badRequest().body(ApiResponse.error(HttpStatus.BAD_REQUEST, e.getMessage()));
}

@ExceptionHandler(DuplicateRequestException.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ public ResponseEntity<ApiResponse<Void>> registerbussinsess(
return ResponseEntity.ok((ApiResponse.success(SuccessCode.OK)));
}

@GetMapping("/v1/artists/{userId}")
public ResponseEntity<ApiResponse<ArtistDetailsRes>> getArtist(
@PathVariable Long userId
@GetMapping("/v1/artists/{artistInfoId}")
public ResponseEntity<ApiResponse<ArtistDetailsRes>> 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)));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ ResponseEntity<ApiResponse<Void>> registerbussinsess(
);

@Operation(summary = "작가 프로필 조회", description = "작가 프로필 조회")
@GetMapping("/v1/artists/{userId}")
ResponseEntity<ApiResponse<ArtistDetailsRes>> getArtist(
@PathVariable Long userId
@GetMapping("/v1/artists/{artistInfoId}")
public ResponseEntity<ApiResponse<ArtistDetailsRes>> getArtistPublicDetails(
@PathVariable Long artistInfoId,
@AuthenticationPrincipal JwtUser jwtUser
);

@Operation(summary = "작가 자신의 프로필 조회", description = "작가 자신의 프로필 조회")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@
@Repository
public interface SocialRepository extends JpaRepository<Social, Long> {
Boolean existsByFollowerAndFollowing(User follower, ArtistInfo following);
Boolean existsByFollowerIdAndFollowingId(Long followerId, Long followingId);
Optional<Social> findByFollowerAndFollowing(User follower, ArtistInfo following);
}
38 changes: 34 additions & 4 deletions src/main/java/com/helpmeCookies/user/service/ArtistService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);

Expand All @@ -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("존재하지 않는 아티스트입니다.");
}
Expand Down

0 comments on commit d2b9d08

Please sign in to comment.