Skip to content

Commit

Permalink
Merge pull request #58 from sjmjys954646/feature/2-branch-watching
Browse files Browse the repository at this point in the history
Feature/2 branch watching
  • Loading branch information
sjmjys954646 authored Nov 5, 2023
2 parents 4bf9ffb + 4ba5445 commit 86f2291
Show file tree
Hide file tree
Showing 6 changed files with 187 additions and 46 deletions.
4 changes: 2 additions & 2 deletions src/main/java/com/example/demo/video/VideoHistory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Where(clause = "deleted_at IS NULL")
@SQLDelete(sql = "UPDATE videohistories SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?")
@Table(name = "videohistories")
@SQLDelete(sql = "UPDATE video_histories SET deleted_at = CURRENT_TIMESTAMP, is_deleted = TRUE where id = ?")
@Table(name = "video_histories")
public class VideoHistory extends BaseTime {
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Id
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/example/demo/video/VideoJPARepository.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
package com.example.demo.video;

import com.example.demo.mentoring.MentorPost;
import com.example.demo.user.userInterest.UserInterest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;

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

public interface VideoJPARepository extends JpaRepository<Video, Integer> {
Optional<Video> findById(int id);

@Query(value = "SELECT * FROM videos v " +
"INNER JOIN video_interests vi ON v.id = vi.video_id " +
"INNER JOIN interest_tb i ON vi.interest_id = i.id " +
"WHERE i.category IN :userInterests " +
"ORDER BY RAND()", // MySQL에서 랜덤 정렬
countQuery = "SELECT COUNT(*) FROM videos v " +
"INNER JOIN video_interests vi ON v.id = vi.video_id " +
"INNER JOIN interest_tb i ON vi.interest_id = i.id " +
"WHERE i.category IN :userInterests", // 페이징을 위한 count 쿼리
nativeQuery = true)
Page<Video> findByVideoCategory(@Param("userInterests") List<String> userInterests, Pageable pageable);


@Query(value = "select v from Video v inner join VideoInterest vi on v.id = vi.video.id where vi.interest.id = :categoryId")
Page<Video> findByCategoryId(@Param("categoryId") int id, Pageable pageable);
}
33 changes: 21 additions & 12 deletions src/main/java/com/example/demo/video/VideoResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@
import java.util.stream.Collectors;

public class VideoResponse {
@Getter
@Setter
public static class VideoPageResponseDTO{
private int page;
private List<VideoAllResponseDTO> videoAllResponseDTO;
private boolean last;

public VideoPageResponseDTO(int page, List<VideoAllResponseDTO> videoAllResponseDTO, boolean last){
this.page = page;
this.videoAllResponseDTO = videoAllResponseDTO;
this.last = last;
}
}


@Getter
@Setter
public static class VideoAllResponseDTO {
Expand Down Expand Up @@ -38,27 +53,21 @@ public VideoAllResponseDTO(Video video, VideoInterest videoInterest)
@Setter
public static class VideoResponseDTO {
private int videoID;
private String videoUrl;
private String videoTitleKorean;
private String videoTitleEng;
private String url;
private String interests;
private long views;
private String videoThumbnailUrl;
private String videoStartTime;
private String videoEndTime;
private String startTime;
private String endTime;
private List<SubtitleDTO> subtitles;

public VideoResponseDTO(Video video, VideoInterest videoInterest, List<Subtitle> subtitles)
{
this.videoID = video.getId();
this.videoUrl = video.getVideoUrl();
this.videoTitleKorean = video.getVideoTitleKorean();
this.videoTitleEng = video.getVideoTitleEng();
this.url = video.getVideoUrl();
this.interests = videoInterest.getInterest().getCategory();
this.views = video.getViews();
this.videoThumbnailUrl = video.getVideoThumbnailUrl();
this.videoStartTime = video.getVideoStartTime();
this.videoEndTime = video.getVideoEndTime();
this.startTime = video.getVideoStartTime();
this.endTime = video.getVideoEndTime();
this.subtitles = subtitles.stream()
.map(SubtitleDTO::new)
.collect(Collectors.toList());
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/com/example/demo/video/VideoRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public class VideoRestController {

private final VideoService videoService;

/* @GetMapping("/videos/interest")
@GetMapping("/videos/interest")
public ResponseEntity<?> getUserCategoryVideo(@AuthenticationPrincipal CustomUserDetails userDetails){
List<VideoResponse.VideoAllResponseDTO> responseDTOs = videoService.findUserCategory(userDetails.getUser().getId());
return ResponseEntity.ok(ApiUtils.success(responseDTOs));
}*/
return ResponseEntity.ok(ApiResponseBuilder.success(responseDTOs));
}

@GetMapping("/videos/main")
public ResponseEntity<?> getCategoryFilterVideo(@RequestParam(value = "page", defaultValue = "0") Integer page) {
List<VideoResponse.VideoAllResponseDTO> responseDTOs = videoService.findAllVideo(page);
public ResponseEntity<?> getCategoryFilterVideo(@RequestParam(value = "category", defaultValue = "0") int id) {
List<VideoResponse.VideoPageResponseDTO> responseDTOs = videoService.findAllVideo(id);
return ResponseEntity.ok(ApiResponseBuilder.success(responseDTOs));
}

Expand Down
74 changes: 60 additions & 14 deletions src/main/java/com/example/demo/video/VideoService.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.example.demo.video;

import com.example.demo.config.errors.exception.Exception404;
import com.example.demo.interest.Interest;
import com.example.demo.user.userInterest.UserInterest;
import com.example.demo.user.userInterest.UserInterestJPARepository;
import lombok.RequiredArgsConstructor;
Expand All @@ -10,6 +11,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

Expand All @@ -23,13 +25,19 @@ public class VideoService {
private final UserInterestJPARepository userInterestJPARepository;
private final VideoHistoryJPARepository videoHistoryJPARepository;

private final int MAINVIDEOTOTAL = 100;
private final int MAINVIDEONUM = 4;
private final int HISTORYVIDEONUM = 5;

public List<VideoResponse.VideoAllResponseDTO> findAllVideo(Integer page) {
Pageable pageable = PageRequest.of(page,MAINVIDEONUM);
public List<VideoResponse.VideoPageResponseDTO> findAllVideo(int categoryId) {
Pageable pageable = PageRequest.of(0 ,MAINVIDEOTOTAL);

Page<Video> pageContent;
if(categoryId == 0)
pageContent = videoJPARepository.findAll(pageable);
else
pageContent = videoJPARepository.findByCategoryId(categoryId, pageable);

Page<Video> pageContent = videoJPARepository.findAll(pageable);

if(pageContent.getTotalPages() == 0){
throw new Exception404("해당 영상들이 존재하지 않습니다");
Expand All @@ -39,11 +47,30 @@ public List<VideoResponse.VideoAllResponseDTO> findAllVideo(Integer page) {
List<VideoResponse.VideoAllResponseDTO> videoDTOList = pageContent.getContent().stream().map(
video -> {
VideoInterest videoInterests = videoInterestJPARepository.findVideoInterestByVideoId(video.getId());

return new VideoResponse.VideoAllResponseDTO(video, videoInterests);
}
).collect(Collectors.toList());
return videoDTOList;

List<VideoResponse.VideoPageResponseDTO> videoPageResponseDTOS = new ArrayList<>();
List<VideoResponse.VideoAllResponseDTO> tempGroup = new ArrayList<>();
int page = 0;
boolean finish = false;
for(int i = 0;i < videoDTOList.size();i++)
{
tempGroup.add(videoDTOList.get(i));
if ((i + 1) % 4 == 0 || i == videoDTOList.size() - 1) {
if(i == videoDTOList.size() - 1)
finish = true;
VideoResponse.VideoPageResponseDTO videoPageResponseDTO = new VideoResponse.VideoPageResponseDTO(
page, tempGroup, finish
);
tempGroup = new ArrayList<>();
videoPageResponseDTOS.add(videoPageResponseDTO);
page++;
}
}

return videoPageResponseDTOS;
}

public VideoResponse.VideoResponseDTO findVideo(int id) {
Expand Down Expand Up @@ -78,13 +105,32 @@ public List<VideoResponse.VideoAllResponseDTO> findHistoryVideo(Integer page, in
return videoDTOList;
}

// public List<VideoResponse.VideoAllResponseDTO> findUserCategory(int id) {
// List<UserInterest> userInterests = userInterestJPARepository.findAllById(id);
// Pageable pageable = PageRequest.of(0,4);
// Page<Video> pageContent;
// if(userInterests.size() == 0)
// {
// pageContent = videoJPARepository.findAll(pageable);
// }
// }
public List<VideoResponse.VideoAllResponseDTO> findUserCategory(int id) {
List<UserInterest> userInterests = userInterestJPARepository.findAllById(id);
List<String> Interests = userInterests.stream()
.map(userInterest -> userInterest.getInterest().getCategory()).collect(Collectors.toList());
Pageable pageable = PageRequest.of(0,4);
Page<Video> pageContent;
if(userInterests.size() == 0)
{
pageContent = videoJPARepository.findAll(pageable);
}
else
{
pageContent = videoJPARepository.findByVideoCategory(Interests ,pageable);
}

if(pageContent.getTotalPages() == 0){
throw new Exception404("해당 영상들이 존재하지 않습니다");
}

// 각 Video에대해서 Interest 끌어오기
List<VideoResponse.VideoAllResponseDTO> videoDTOList = pageContent.getContent().stream().map(
video -> {
VideoInterest videoInterests = videoInterestJPARepository.findVideoInterestByVideoId(video.getId());
return new VideoResponse.VideoAllResponseDTO(video, videoInterests);
}
).collect(Collectors.toList());
return videoDTOList;
}
}
Loading

0 comments on commit 86f2291

Please sign in to comment.