Skip to content

Commit

Permalink
Feat: 원하는 기간 동안 저장된 숏폼의 메인카테고리 순위 가져오는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
yunhacandy authored Aug 11, 2024
2 parents b9b200e + d1c6d7d commit a0171ce
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.hongik.graduationproject.controller;

import java.util.Date;

import com.hongik.graduationproject.domain.dto.Response;
import com.hongik.graduationproject.domain.dto.category.MainCategoryRankingListResponse;
import com.hongik.graduationproject.domain.dto.category.SubCategoryCreateRequest;
Expand All @@ -14,6 +16,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.*;
Expand Down Expand Up @@ -54,4 +57,16 @@ public Response<MainCategoryRankingListResponse> getMainCategoryRanking(@Authent
MainCategoryRankingListResponse rankingList = categoryService.getMainCategoryRanking(userId);
return Response.createSuccess(rankingList);
}

@Operation(summary = "주차별 메인 카테고리 랭킹 가져오기", description = "특정 주차에 대한 메인 카테고리 1, 2위를 가져오는 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = MainCategoryRankingListResponse.class)))
@ResponseStatus(HttpStatus.OK)
@GetMapping("/categories/rankings/weekly")
public Response<MainCategoryRankingListResponse> getMainCategoryRankingByDate(
@AuthenticationPrincipal Long userId,
@RequestParam(name = "start-date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date startDate,
@RequestParam(name = "end-date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date endDate) {
MainCategoryRankingListResponse rankingList = categoryService.getMainCategoryRankingByDate(userId, startDate, endDate);
return Response.createSuccess(rankingList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.hongik.graduationproject.domain.entity.Category;
import com.hongik.graduationproject.domain.entity.User;
import com.hongik.graduationproject.enums.MainCategory;
import java.util.Date;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

Expand Down Expand Up @@ -39,4 +40,15 @@ public interface CategoryRepository extends JpaRepository<Category, Long> {
"ORDER BY COUNT(vsc.video_summary_category_id) DESC " +
"LIMIT 2", nativeQuery = true)
List<MainCategoryCount> getSummaryCountOfMainCategoryByUser(Long userId);

@Query(value = "SELECT c.main_category AS mainCategory, COUNT(vsc.video_summary_category_id) AS count " +
"FROM category c " +
"LEFT JOIN video_summary_category vsc ON c.category_id = vsc.category_id " +
"LEFT JOIN video_summary vs ON vsc.video_summary_id = vs.video_summary_id " +
"WHERE c.user_id = :userId " +
"AND vs.created_at BETWEEN :startDate AND :endDate "+
"GROUP BY c.main_category " +
"ORDER BY COUNT(vsc.video_summary_category_id) DESC " +
"LIMIT 2", nativeQuery = true)
List<MainCategoryCount> getSummaryCountOfMainCategoryByUserAndDate(Long userId, Date startDate, Date endDate);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.hongik.graduationproject.exception.ErrorCode;
import com.hongik.graduationproject.repository.CategoryRepository;
import com.hongik.graduationproject.repository.UserRepository;
import java.util.Date;
import java.util.stream.Collectors;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -76,6 +77,23 @@ public MainCategoryRankingListResponse getMainCategoryRanking(Long userId) {
return new MainCategoryRankingListResponse(rankingList);
}

@Transactional
public MainCategoryRankingListResponse getMainCategoryRankingByDate(Long userId, Date startDate, Date endDate) {
List<MainCategoryCount> categoryCounts = categoryRepository.getSummaryCountOfMainCategoryByUserAndDate(userId, startDate, endDate);

int totalSummaries = categoryCounts.stream()
.mapToInt(count -> count.getCount().intValue())
.sum();

List<MainCategoryRankingResponse> rankingList = categoryCounts.stream()
.map(count -> createRankingResponse(count, totalSummaries))
.filter(ranking -> ranking.summaryCount() > 0)
.collect(Collectors.toList());

log.info("사용자 ID {}의 {}부터 {}까지의 메인 카테고리 순위 조회", userId, startDate, endDate);
return new MainCategoryRankingListResponse(rankingList);
}

private MainCategoryRankingResponse createRankingResponse(MainCategoryCount count, int totalSummaries) {
MainCategory mainCategory = MainCategory.valueOf(count.getMainCategory());
int summaryCount = count.getCount().intValue();
Expand Down

0 comments on commit a0171ce

Please sign in to comment.