Skip to content

Commit

Permalink
Merge pull request #38 from najakgil/feature/#35
Browse files Browse the repository at this point in the history
[Refactor]: 좋아요 코드 수정
  • Loading branch information
hw130 authored Apr 2, 2024
2 parents 6479950 + 0ff63ee commit da1eebf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ private GetChartResponse(List<PhotoDto.PhotoChartDto> contents, long totalElemen
}

public static GetChartResponse of(ScrollPaginationCollection<Photo> photoScroll, long totalElements) {
GetChartResponse GetChartResponse;
GetChartResponse response;
if (photoScroll.isLastScroll()) {
GetChartResponse = newLastScroll(photoScroll.getCurrentScrollItems(), totalElements);
response = newLastScroll(photoScroll.getCurrentScrollItems(), totalElements);
} else {
GetChartResponse = newScrollHasNext(photoScroll.getCurrentScrollItems(), totalElements, photoScroll.getNextCursor().getId());
response = newScrollHasNext(photoScroll.getCurrentScrollItems(), totalElements, photoScroll.getNextCursor().getId());
}

return GetChartResponse;
return response;
}

private static GetChartResponse newLastScroll(List<Photo> photoScroll, long totalElements) {
Expand All @@ -51,9 +51,12 @@ private static GetChartResponse newScrollHasNext(List<Photo> photoScroll, long t
return new GetChartResponse(getContents(photoScroll), totalElements, nextCursor);
}

/**
* 조회한 데이터를 클라이언트에게 전달할 데이터로 가공
*/
private static List<PhotoDto.PhotoChartDto> getContents(List<Photo> photoScroll) {
return photoScroll.stream()
.map(feed -> new PhotoDto.PhotoChartDto(feed.getId(), feed.getImgUrl(), feed.getLikes()))
.map(photo -> new PhotoDto.PhotoChartDto(photo.getId(), photo.getImgUrl(), photo.getLikes()))
.collect(Collectors.toList());
}

Expand Down
24 changes: 4 additions & 20 deletions src/main/java/com/example/developjeans/service/PhotoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -176,15 +176,12 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){
throw new BusinessLogicException(ExceptionCode.INVALID_USER_JWT);
}

User _user = userRepository.findById(userId)
User user = userRepository.findById(userId)
.orElseThrow(() -> new EntityNotFoundException("존재하지 않은 유저 ID: " + userId));

Photo photo = photoRepository.findById(photoId)
.orElseThrow(() -> new EntityNotFoundException("존재하지 않은 사진 ID: " + photoId));

User user = User.builder()
.id(userId)
.build();

Optional<PhotoLike> optionalLike = photoLikeRepository.findByPhotoAndUser(photo, user);

Expand All @@ -193,19 +190,10 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){
photoLikeRepository.delete(photoLike);
photo.setLikes(photo.getLikes() - 1);
String message = "좋아요 취소";
// return new PhotoLikeRes(photoId, photo.getLikes(), message);
PhotoDto.PhotoResponseDto photoResponseDto = photoMapper.toResponseDto(photo);
return new PhotoDto.PhotoLikeDto(photoResponseDto.getId(), photoResponseDto.getLikes(), message);
}

/*
if(photoLikeRepository.existsByPhotoAndUserAndStatus(photo, user, Status.A)) {
photoLikeRepository.deleteById(photo);
photo.setLikes(photo.getLikes() - 1);
System.out.println("좋아요가 취소되었습니다.");
return new PhotoLikeRes(photoId, photo.getLikes());
} */

PhotoLike photoLike = PhotoLike
.builder()
.user(user)
Expand All @@ -219,27 +207,23 @@ public PhotoDto.PhotoLikeDto likePhoto(Long photoId, Long userId){
String message = "좋아요";
PhotoDto.PhotoResponseDto photoResponseDto = photoMapper.toResponseDto(photo);
return new PhotoDto.PhotoLikeDto(photoResponseDto.getId(), photoResponseDto.getLikes(), message);
// return new PhotoLikeRes(photoId, photo.getLikes(), message);
} catch (Exception e){
throw new BusinessLogicException(ExceptionCode.DATABASE_ERROR);
}




}

@Transactional(readOnly = true)
public GetChartResponse getChart(String sort, int size, Long lastPageId) {
public GetChartResponse getChart(String sort, int size, Long lastPhotoId) {
// 페이지 요청 객체 생성
PageRequest pageRequest = PageRequest.of(0, size + 1);

Page<Photo> page;
if ("likes".equals(sort) || "latest".equals(sort)) {
// 정렬 기준에 따라 쿼리 실행
page = "likes".equals(sort) ?
photoRepository.findAllByOrderByLikesDesc(lastPageId, pageRequest) :
photoRepository.findAllByOrderByCreatedAtDesc(lastPageId,pageRequest);
photoRepository.findAllByOrderByLikesDesc(lastPhotoId, pageRequest) :
photoRepository.findAllByOrderByCreatedAtDesc(lastPhotoId,pageRequest);
} else {
// 유효하지 않은 sort값이 온 경우 예외 처리
throw new BusinessLogicException(ExceptionCode.INVALID_SORT);
Expand Down

0 comments on commit da1eebf

Please sign in to comment.