Skip to content

Commit

Permalink
Merge pull request #22 from f-lab-edu/issue19
Browse files Browse the repository at this point in the history
issue19
  • Loading branch information
misim3 authored Jul 29, 2024
2 parents c95c765 + a68ef3a commit e2f5280
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class StartWatchingVideoResponse {
@Schema(name = "watchingTime", description = "동영상 시청 시각. 동영상 시청한 기록이 없다면, 0. 있다면, 시청 시각 반환", example = "1234", requiredMode = Schema.RequiredMode.REQUIRED)
private Long watchingTime;

@Schema(name = "isWatchedToEnd", description = "동영상 시청 완료 여부", example = "true or false", requiredMode = Schema.RequiredMode.REQUIRED)
private Boolean isWatchedToEnd;

@Schema(name = "commentListResponse", description = "댓글 목록", example = "...", requiredMode = Schema.RequiredMode.REQUIRED)
private CommentListResponse commentListResponse;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public static List<VideoResponse> convertVideos(List<Video> videos) {
.userId(video.getUser().getId())
.category(VideoCategory.getNameByCode(video.getCategoryId()))
.videoUrl(video.getVideoFile().getPath())
.views(video.getViews())
.views(video.getViewCount())
.thumbnailUrl(video.getThumbnailUrl())
.build())
.toList();
Expand Down
21 changes: 11 additions & 10 deletions mitube-app/src/main/java/com/misim/entity/Video.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
Expand All @@ -26,29 +25,31 @@ public class Video extends BaseTimeEntity {
private String description;

@ManyToOne
@Setter
private User user;

@ManyToOne
@Setter
private VideoFile videoFile;

private Integer categoryId;
private Long viewCount;

@Setter
private Long views;
private Integer categoryId;

// 연관관계 설정 가능
private String thumbnailUrl;

@Builder
public Video(String title, String description, Integer categoryId, Long views,
String thumbnailUrl, User user) {
public Video(String title, String description, User user, VideoFile videoFile, Long viewCount,
Integer categoryId, String thumbnailUrl) {
this.title = title;
this.description = description;
this.categoryId = categoryId;
this.views = views;
this.user = user;
this.videoFile = videoFile;
this.viewCount = viewCount;
this.categoryId = categoryId;
this.thumbnailUrl = thumbnailUrl;
}

public void incrementViewCount() {
this.viewCount++;
}
}
23 changes: 0 additions & 23 deletions mitube-app/src/main/java/com/misim/entity/View.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,13 @@

import com.misim.entity.Video;
import java.util.List;
import java.util.Optional;
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 VideoRepository extends JpaRepository<Video, Long> {

@Query("SELECT v.views FROM Video v WHERE v.id = :videoId")
Optional<Long> findViewsByVideoId(@Param("videoId") Long videoId);

Video findTopByUserId(Long userId);

@Query("SELECT v FROM Video v ORDER BY v.createdDate DESC LIMIT 10")
Expand Down
44 changes: 0 additions & 44 deletions mitube-app/src/main/java/com/misim/repository/ViewRepository.java

This file was deleted.

25 changes: 25 additions & 0 deletions mitube-app/src/main/java/com/misim/service/AsyncService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.misim.service;

import com.misim.entity.Video;
import com.misim.entity.WatchingInfo;
import com.misim.repository.VideoRepository;
import com.misim.repository.WatchingInfoRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class AsyncService {

private final VideoRepository videoRepository;
private final WatchingInfoRepository watchingInfoRepository;

@Async
public void startWatchingVideo(Video video, WatchingInfo watchingInfo) {
videoRepository.save(video);
if (watchingInfo.getId() == null) {
watchingInfoRepository.save(watchingInfo);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,7 @@ public ReactionResponse getReaction(Long userId, Long videoId) {
throw new MitubeException(MitubeErrorCode.NOT_FOUND_USER);
}

if (!videoRepository.existsById(videoId)) {
throw new MitubeException(MitubeErrorCode.NOT_FOUND_VIDEO);
}

Optional<Reaction> reaction = reactionRepository.findById(userId);
Optional<Reaction> reaction = reactionRepository.findByUserIdAndVideoId(userId, videoId);

return reaction.map(r -> ReactionResponse.builder()
.type(r.getType())
Expand Down
59 changes: 20 additions & 39 deletions mitube-app/src/main/java/com/misim/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.misim.entity.Video;
import com.misim.entity.VideoCategory;
import com.misim.entity.VideoFile;
import com.misim.entity.View;
import com.misim.entity.WatchingInfo;
import com.misim.exception.MitubeErrorCode;
import com.misim.exception.MitubeException;
Expand Down Expand Up @@ -46,8 +45,8 @@ public class VideoService {
private final UserRepository userRepository;
private final WatchingInfoRepository watchingInfoRepository;
private final SubscriptionRepository subscriptionRepository;
private final AsyncService asyncService;
private final ReactionService reactionService;
private final ViewService viewService;

public String uploadVideos(MultipartFile file) {

Expand Down Expand Up @@ -112,58 +111,48 @@ public void createVideos(CreateVideoRequest createVideoRequest) {
throw new MitubeException(MitubeErrorCode.INVALID_CATEGORY);
}

VideoFile videoFile = videoFileRepository.findById(videoFileId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_VIDEO_FILE));

User user = userRepository.findByNickname(createVideoRequest.getNickname());

Video video = Video.builder()
.title(createVideoRequest.getTitle())
.description(createVideoRequest.getDescription())
.user(user)
.videoFile(videoFile)
.categoryId(createVideoRequest.getCategoryId())
.views(0L)
.thumbnailUrl("")
.build();

// 비디오 파일 연결
VideoFile videoFile = videoFileRepository.findById(videoFileId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_VIDEO_FILE));

video.setVideoFile(videoFile);

// 유저 연결
User user = userRepository.findByNickname(createVideoRequest.getNickname());

video.setUser(user);

// 비디오 저장
videoRepository.save(video);
}

public StartWatchingVideoResponse startWatchingVideo(Long videoId, Long userId) {

View view = viewService.getIncrementView(videoId);

WatchingInfo watchingInfo;
Video video = videoRepository.findById(videoId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_VIDEO));

if (watchingInfoRepository.existsByUserIdAndVideoId(userId, videoId)) {

watchingInfo = watchingInfoRepository.findByUserIdAndVideoId(userId, videoId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_WATCHING_INFO));

} else {
// 로그인 상태라면, 해당 유저가 해당 동영상에 대한 반응 정보를 불러온다.
ReactionResponse reactionResponse = reactionService.getReaction(userId, videoId);

watchingInfo = WatchingInfo.builder()
WatchingInfo watchingInfo = watchingInfoRepository.findByUserIdAndVideoId(userId, videoId)
.orElse(WatchingInfo.builder()
.userId(userId)
.videoId(videoId)
.watchingTime(0L)
.build();
.build()
);

watchingInfoRepository.save(watchingInfo);
video.incrementViewCount();

}

// 로그인 상태라면, 해당 유저가 해당 동영상에 대한 반응 정보를 불러온다.
ReactionResponse reactionResponse = reactionService.getReaction(userId, videoId);
asyncService.startWatchingVideo(video, watchingInfo);

return StartWatchingVideoResponse.builder()
.watchingTime(watchingInfo.getWatchingTime())
.views(view.getCount())
.isWatchedToEnd(watchingInfo.getIsWatchedToEnd())
.views(video.getViewCount())
.reactionResponse(reactionResponse)
.build();
}
Expand All @@ -174,10 +163,6 @@ public void updateWatchingVideo(Long videoId, Long userId, Long watchingTime) {
WatchingInfo watchingInfo = watchingInfoRepository.findByUserIdAndVideoId(userId, videoId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_WATCHING_INFO));

if (watchingInfo == null) {
throw new MitubeException(MitubeErrorCode.NOT_FOUND_WATCHING_INFO);
}

watchingInfo.addWatchingTime(watchingTime);

watchingInfoRepository.save(watchingInfo);
Expand All @@ -189,10 +174,6 @@ public void completeWatchingVideo(Long videoId, Long userId, Long watchingTime)
WatchingInfo watchingInfo = watchingInfoRepository.findByUserIdAndVideoId(userId, videoId)
.orElseThrow(() -> new MitubeException(MitubeErrorCode.NOT_FOUND_WATCHING_INFO));

if (watchingInfo == null) {
throw new MitubeException(MitubeErrorCode.NOT_FOUND_WATCHING_INFO);
}

watchingInfo.addWatchingTime(watchingTime);
watchingInfo.completeWatchingVideo();

Expand Down
37 changes: 0 additions & 37 deletions mitube-app/src/main/java/com/misim/service/ViewService.java

This file was deleted.

0 comments on commit e2f5280

Please sign in to comment.