diff --git a/mitube-app/build.gradle b/mitube-app/build.gradle index 2cedc6b..9c72703 100644 --- a/mitube-app/build.gradle +++ b/mitube-app/build.gradle @@ -7,6 +7,7 @@ dependencies { implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.3.0' implementation 'net.nurigo:sdk:4.3.0' // sms 서비스 implementation 'org.springframework.boot:spring-boot-starter-data-redis' + implementation 'org.springframework.boot:spring-boot-starter-actuator' runtimeOnly 'com.h2database:h2' runtimeOnly 'com.mysql:mysql-connector-j' compileOnly 'org.projectlombok:lombok' diff --git a/mitube-app/src/main/java/com/misim/MitubeAppApplication.java b/mitube-app/src/main/java/com/misim/MitubeAppApplication.java index ceca88b..4c32b16 100644 --- a/mitube-app/src/main/java/com/misim/MitubeAppApplication.java +++ b/mitube-app/src/main/java/com/misim/MitubeAppApplication.java @@ -2,8 +2,10 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication +@EnableScheduling public class MitubeAppApplication { public static void main(String[] args) { diff --git a/mitube-app/src/main/java/com/misim/entity/ViewIncreaseRequest.java b/mitube-app/src/main/java/com/misim/entity/ViewIncreaseRequest.java new file mode 100644 index 0000000..0b4148d --- /dev/null +++ b/mitube-app/src/main/java/com/misim/entity/ViewIncreaseRequest.java @@ -0,0 +1,33 @@ +package com.misim.entity; + +import com.misim.util.TimeUtil; +import jakarta.persistence.Entity; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.Table; +import java.time.LocalDateTime; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@Table(name = "view_increase_requests") +@NoArgsConstructor +public class ViewIncreaseRequest { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private Long videoId; + + private LocalDateTime requestDateTime; + + @Builder + public ViewIncreaseRequest(Long videoId) { + this.videoId = videoId; + this.requestDateTime = TimeUtil.getNow(); + } +} diff --git a/mitube-app/src/main/java/com/misim/repository/ViewIncreaseRequestRepository.java b/mitube-app/src/main/java/com/misim/repository/ViewIncreaseRequestRepository.java new file mode 100644 index 0000000..9664c92 --- /dev/null +++ b/mitube-app/src/main/java/com/misim/repository/ViewIncreaseRequestRepository.java @@ -0,0 +1,18 @@ +package com.misim.repository; + +import com.misim.entity.ViewIncreaseRequest; +import java.time.LocalDateTime; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; + +@Repository +public interface ViewIncreaseRequestRepository extends JpaRepository { + + @Query("SELECT v.videoId, COUNT(*) as requestCount FROM ViewIncreaseRequest v where v.requestDateTime > :oneWeekAgo GROUP BY v.videoId ORDER BY requestCount DESC LIMIT 10") + List findTopIncreaseAmountVideoIds(@Param("oneWeekAgo") LocalDateTime oneWeekAgo); + + void deleteByRequestDateTimeBefore(LocalDateTime oneWeekAgo); +} diff --git a/mitube-app/src/main/java/com/misim/service/AsyncService.java b/mitube-app/src/main/java/com/misim/service/AsyncService.java index c6d294e..ffa258c 100644 --- a/mitube-app/src/main/java/com/misim/service/AsyncService.java +++ b/mitube-app/src/main/java/com/misim/service/AsyncService.java @@ -1,8 +1,10 @@ package com.misim.service; import com.misim.entity.Video; +import com.misim.entity.ViewIncreaseRequest; import com.misim.entity.WatchingInfo; import com.misim.repository.VideoRepository; +import com.misim.repository.ViewIncreaseRequestRepository; import com.misim.repository.WatchingInfoRepository; import lombok.RequiredArgsConstructor; import org.springframework.scheduling.annotation.Async; @@ -14,6 +16,7 @@ public class AsyncService { private final VideoRepository videoRepository; private final WatchingInfoRepository watchingInfoRepository; + private final ViewIncreaseRequestRepository viewIncreaseRequestRepository; @Async public void startWatchingVideo(Video video, WatchingInfo watchingInfo) { @@ -21,5 +24,9 @@ public void startWatchingVideo(Video video, WatchingInfo watchingInfo) { if (watchingInfo.getId() == null) { watchingInfoRepository.save(watchingInfo); } + viewIncreaseRequestRepository.save(ViewIncreaseRequest.builder() + .videoId(video.getId()) + .build() + ); } } diff --git a/mitube-app/src/main/java/com/misim/service/HomeService.java b/mitube-app/src/main/java/com/misim/service/HomeService.java index 06c5571..83629de 100644 --- a/mitube-app/src/main/java/com/misim/service/HomeService.java +++ b/mitube-app/src/main/java/com/misim/service/HomeService.java @@ -15,7 +15,7 @@ public HomeResponse getHome(Long userId) { return HomeResponse.builder() .categoryList(VideoCategory.getCategoryList()) - .hotVideoList(null)//videoService.getHotVideos()) + .hotVideoList(videoService.getHotVideos()) .newVideoList(videoService.getNewVideos()) .watchingVideoList(videoService.getWatchingVideos(userId)) .subscribingChannelNewVideoList(videoService.getSubscribingChannelNewVideos(userId)) diff --git a/mitube-app/src/main/java/com/misim/service/VideoService.java b/mitube-app/src/main/java/com/misim/service/VideoService.java index 1817d0a..0164753 100644 --- a/mitube-app/src/main/java/com/misim/service/VideoService.java +++ b/mitube-app/src/main/java/com/misim/service/VideoService.java @@ -47,6 +47,7 @@ public class VideoService { private final SubscriptionRepository subscriptionRepository; private final AsyncService asyncService; private final ReactionService reactionService; + private final ViewIncreaseRequestService viewIncreaseRequestService; public String uploadVideos(MultipartFile file) { @@ -189,16 +190,14 @@ public List getNewVideos() { return VideoResponse.convertVideos(videos); } -// public List getHotVideos() { -// -// Set viewSet = viewRepository.findHotTen(); -// -// List videoIds = viewSet.stream().map(View::getVideoId).toList(); -// -// List