Skip to content

Commit

Permalink
Merge pull request #61 from sjmjys954646/feature/2-branch-watching
Browse files Browse the repository at this point in the history
Feature/2 branch watching
  • Loading branch information
choboss00 authored Nov 5, 2023
2 parents 86f2291 + 72e3755 commit 903fce6
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 21 deletions.
9 changes: 8 additions & 1 deletion src/main/java/com/example/demo/video/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.SQLDelete;
import org.hibernate.annotations.Where;

import javax.persistence.*;
import javax.validation.constraints.NotNull;

@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
Expand Down Expand Up @@ -36,7 +38,8 @@ public class Video extends BaseTime {
@Column
private String videoEndTime;

@Column
@Column(nullable = false)
@ColumnDefault("0")
private long views;

@Column
Expand All @@ -52,4 +55,8 @@ public Video(String videoUrl, String videoTitleKorean, String videoTitleEng, Str
this.views = views;
this.videoThumbnailUrl = videoThumbnailUrl;
}

public void addView(){
this.views += 1;
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/example/demo/video/VideoJPARepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ public interface VideoJPARepository extends JpaRepository<Video, Integer> {

@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 " +
"INNER JOIN interests 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 " +
"INNER JOIN interests 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);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/example/demo/video/VideoRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.example.demo.video;

public class VideoRequest {
}
11 changes: 5 additions & 6 deletions src/main/java/com/example/demo/video/VideoRestController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@
import com.example.demo.config.utils.ApiResponseBuilder;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "Video API")
Expand All @@ -33,8 +32,8 @@ public ResponseEntity<?> getCategoryFilterVideo(@RequestParam(value = "category"
}

@GetMapping("/videos/{id}")
public ResponseEntity<?> getVideoId(@PathVariable int id) {
VideoResponse.VideoResponseDTO responseDTO = videoService.findVideo(id);
public ResponseEntity<?> getVideoId(@PathVariable int id, @AuthenticationPrincipal CustomUserDetails userDetails) {
VideoResponse.VideoResponseDTO responseDTO = videoService.findVideo(id, userDetails.getUser());
return ResponseEntity.ok(ApiResponseBuilder.success(responseDTO));
}

Expand Down
14 changes: 11 additions & 3 deletions src/main/java/com/example/demo/video/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.example.demo.config.errors.exception.Exception404;
import com.example.demo.interest.Interest;
import com.example.demo.user.User;
import com.example.demo.user.userInterest.UserInterest;
import com.example.demo.user.userInterest.UserInterestJPARepository;
import lombok.RequiredArgsConstructor;
Expand Down Expand Up @@ -58,7 +59,7 @@ public List<VideoResponse.VideoPageResponseDTO> findAllVideo(int categoryId) {
for(int i = 0;i < videoDTOList.size();i++)
{
tempGroup.add(videoDTOList.get(i));
if ((i + 1) % 4 == 0 || i == videoDTOList.size() - 1) {
if ((i + 1) % MAINVIDEONUM == 0 || i == videoDTOList.size() - 1) {
if(i == videoDTOList.size() - 1)
finish = true;
VideoResponse.VideoPageResponseDTO videoPageResponseDTO = new VideoResponse.VideoPageResponseDTO(
Expand All @@ -73,9 +74,16 @@ public List<VideoResponse.VideoPageResponseDTO> findAllVideo(int categoryId) {
return videoPageResponseDTOS;
}

public VideoResponse.VideoResponseDTO findVideo(int id) {
public VideoResponse.VideoResponseDTO findVideo(int id, User user) {
Video video = videoJPARepository.findById(id)
.orElseThrow(() -> new Exception404("해당 영상이 존재하지 않습니다.\n" + "id : " + id));;
.orElseThrow(() -> new Exception404("해당 영상이 존재하지 않습니다.\n" + "id : " + id));

video.addView();
if(user != null)
{
VideoHistory videoHistory = new VideoHistory(user, video);
videoHistoryJPARepository.save(videoHistory);
}

VideoInterest videoInterest = videoInterestJPARepository.findVideoInterestByVideoId(video.getId());
List<Subtitle> videoSubtitles = subtitleJPARepository.findSubtitleByVideoId(video.getId());
Expand Down
25 changes: 16 additions & 9 deletions src/test/java/com/example/demo/VideoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -292,12 +292,20 @@ void findAllTest() throws Exception{

@Test
@Order(2)
void findTest() throws Exception{
VideoResponse.VideoResponseDTO videoFind = videoService.findVideo(1);
org.assertj.core.api.Assertions.assertThat(1)
.isEqualTo(videoFind.getVideoID());
Assertions.assertThat("asdfasdf")
.isEqualTo(videoFind.getSubtitles().get(0).getKorSubtitleContent());
void HistoryTest() throws Exception{
User user2 = User.builder()
.email("[email protected]")
.password("asdf1234!")
.firstName("Jin")
.lastName("Seung")
.country("Korea")
.age(21)
.role(Role.MENTOR)
.phone("010-0000-0000")
.build();

userJPARepository.save(user2);
VideoResponse.VideoResponseDTO videoFind = videoService.findVideo(3,user2);
}

@Test
Expand All @@ -312,13 +320,12 @@ void findHistoryTest() throws Exception{
@Order(2)
void findUserCategoryTest() throws Exception{
List<VideoResponse.VideoAllResponseDTO> findUserCategory = videoService.findUserCategory(1);

}

@Test
@Order(2)
@Order(3)
void findOmTest() throws Exception {
List<VideoResponse.VideoPageResponseDTO> videoFind = videoService.findAllVideo(1);
List<VideoResponse.VideoAllResponseDTO> videoFind = videoService.findHistoryVideo(0,2);

String responseBody = om.writeValueAsString(videoFind);

Expand Down

0 comments on commit 903fce6

Please sign in to comment.