Skip to content

Commit

Permalink
feat: 사용자들이 좋아요한 컨텐츠 TOP 10 조회 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
GitJIHO committed Nov 27, 2024
1 parent db500ae commit 680e99f
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.software.ott.common.dto.StringTypeMessageResponse;
import com.software.ott.history.dto.ContentLikeResponse;
import com.software.ott.history.dto.TopContentLikeResponse;
import com.software.ott.history.service.ContentLikeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
Expand Down Expand Up @@ -32,4 +33,11 @@ public ResponseEntity<List<ContentLikeResponse>> readAllContentsLikeByMember(@Re
List<ContentLikeResponse> contentLikeResponses = contentLikeService.readAllLikeContentsByMember(memberId, like);
return ResponseEntity.ok().body(contentLikeResponses);
}

@Operation(summary = "사용자들 좋아요한 컨텐츠 TOP10", description = "사용자들의 좋아요한 컨텐츠들 중 TOP 10을 조회합니다.")
@GetMapping("/top")
public ResponseEntity<List<TopContentLikeResponse>> getTop10MostLikedContents() {
List<TopContentLikeResponse> topContentLikeResponses = contentLikeService.getTop10MostLikedContents();
return ResponseEntity.ok().body(topContentLikeResponses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.software.ott.history.dto;

import com.software.ott.content.entity.Content;

public record TopContentLikeResponse(
Content content,
Long likeCount
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.software.ott.history.entity.ContentLike;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
Expand All @@ -12,4 +13,10 @@ public interface ContentLikeRepository extends JpaRepository<ContentLike, Long>
Optional<ContentLike> findByMemberIdAndContentId(Long memberId, Long contentId);

List<ContentLike> findAllByMemberIdAndLiked(Long memberId, boolean liked);

@Query("SELECT cl.content, COUNT(cl) AS likeCount FROM ContentLike cl " +
"WHERE cl.liked = true " +
"GROUP BY cl.content " +
"ORDER BY likeCount DESC")
List<Object[]> findTop10MostLikedContents();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

@Repository
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.software.ott.content.entity.Content;
import com.software.ott.content.repository.ContentRepository;
import com.software.ott.history.dto.ContentLikeResponse;
import com.software.ott.history.dto.TopContentLikeResponse;
import com.software.ott.history.entity.ContentLike;
import com.software.ott.history.repository.ContentLikeRepository;
import com.software.ott.member.entity.Member;
Expand Down Expand Up @@ -54,4 +55,18 @@ public List<ContentLikeResponse> readAllLikeContentsByMember(Long memberId, bool
ContentLike.getContent()))
.collect(Collectors.toList());
}

@Transactional
public List<TopContentLikeResponse> getTop10MostLikedContents() {
List<Object[]> topContents = contentLikeRepository.findTop10MostLikedContents();

return topContents.stream()
.map(result -> {
Content content = (Content) result[0];
long likeCount = (long) result[1];

return new TopContentLikeResponse(content, likeCount);
})
.collect(Collectors.toList());
}
}

0 comments on commit 680e99f

Please sign in to comment.