Skip to content

Commit

Permalink
Merge pull request #20 from HongikGraduationProject/summarizing
Browse files Browse the repository at this point in the history
Summarizing
  • Loading branch information
qjvk2880 authored May 25, 2024
2 parents 060c05e + eacfee3 commit 6b7e3e1
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
@Getter
public enum ErrorCode {
INVALID_VIDEO_URL(HttpStatus.BAD_REQUEST, "유효하지 않은 영상 URL입니다."),
ALREADY_REQUESTED_SUMMARIZING(HttpStatus.BAD_REQUEST, "사용자가 이미 해당 영상을 요약 요청했습니다. 잠시 후 다시 시도하세요"),
ALREADY_REQUESTED_SUMMARIZING(HttpStatus.BAD_REQUEST, "사용자가 이미 해당 영상을 요약 요청했습니다."),
FAILED_TO_EXTRACT_EXTRACT_ID(HttpStatus.BAD_REQUEST, "영상의 고유 ID를 추출하는데 실패했습니다."),
VIDEO_SUMMARY_NOT_FOUND(HttpStatus.NOT_FOUND,"영상 요약 정보를 찾을 수 없습니다"),
MAIN_CATEGORY_NOT_EXISTS(HttpStatus.BAD_REQUEST,"해당 메인 카테고리가 존재하지 않습니다.");

MAIN_CATEGORY_NOT_EXISTS(HttpStatus.BAD_REQUEST,"해당 메인 카테고리가 존재하지 않습니다."),
SUMMARIZING_STATUS_NOT_EXIST(HttpStatus.NOT_FOUND, "요약 정보를 찾을 수 없습니다."),
CATEGORY_NOT_EXIST(HttpStatus.NOT_FOUND, "카테고리를 찾을 수 없습니다.");
private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,21 @@
import com.hongik.graduationproject.domain.entity.Category;
import com.hongik.graduationproject.domain.entity.VideoSummary;
import com.hongik.graduationproject.domain.entity.VideoSummaryCategory;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

public interface VideoSummaryCategoryRepository extends JpaRepository<VideoSummaryCategory, Long> {
List<VideoSummaryCategory> findAllByCategory(Category category);
VideoSummaryCategory findByVideoSummary(VideoSummary videoSummary);
boolean existsByCategoryAndVideoSummary(Category category, VideoSummary videoSummary);

@EntityGraph(attributePaths = {"category", "category.user", "videoSummary"})
@Query("SELECT COUNT(vsc) > 0 " +
"FROM VideoSummaryCategory vsc " +
"WHERE vsc.category.user.id = :userId " +
"AND vsc.videoSummary.videoCode = :videoCode")
boolean existsByVideoCodeAndUserId(String videoCode, Long userId);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
package com.hongik.graduationproject.repository;


import com.hongik.graduationproject.domain.entity.cache.VideoSummaryStatusCache;
import org.springframework.data.repository.CrudRepository;

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

public interface VideoSummaryStatusCacheRepository extends CrudRepository<VideoSummaryStatusCache, String> {
Optional<VideoSummaryStatusCache> findByVideoCode(String videoCode);
Optional<VideoSummaryStatusCache> findFirstByVideoCode(String videoCode);

boolean existsByVideoCode(String videoCode);

boolean existsByVideoCodeAndUserId(String videoCode, Long userId);

Optional<VideoSummaryStatusCache> findByVideoCodeAndUserId(String videoCode, Long userId);
List<VideoSummaryStatusCache> findAllByVideoCode(String videoCode);
}
package com.hongik.graduationproject.repository;


import com.hongik.graduationproject.domain.entity.cache.VideoSummaryStatusCache;
import org.springframework.data.repository.CrudRepository;

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

public interface VideoSummaryStatusCacheRepository extends CrudRepository<VideoSummaryStatusCache, String> {
Optional<VideoSummaryStatusCache> findFirstByVideoCode(String videoCode);
List<VideoSummaryStatusCache> findAllByVideoCode(String videoCode);
Optional<VideoSummaryStatusCache> findByVideoCodeAndUserId(String videoCode, Long userId);
boolean existsByVideoCodeAndUserId(String videoCode, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.hongik.graduationproject.domain.entity.VideoSummaryCategory;
import com.hongik.graduationproject.domain.entity.cache.VideoSummaryStatusCache;
import com.hongik.graduationproject.eum.Platform;
import com.hongik.graduationproject.eum.SummaryStatus;
import com.hongik.graduationproject.exception.AppException;
import com.hongik.graduationproject.exception.ErrorCode;
import com.hongik.graduationproject.repository.CategoryRepository;
Expand All @@ -20,6 +21,8 @@
import java.util.List;
import java.util.Optional;

import static com.hongik.graduationproject.eum.SummaryStatus.*;

@Service
@RequiredArgsConstructor
public class VideoSummaryService {
Expand All @@ -37,13 +40,13 @@ public VideoSummaryInitiateResponse initiateSummarizing(VideoSummaryInitiateRequ

userId = summaryInitiateRequest.getUserId();

if (summaryStatusCacheRepository.existsByVideoCodeAndUserId(videoCode, userId)) {
if (checkDuplicateSummarizing(videoCode, userId)) {
throw new AppException(ErrorCode.ALREADY_REQUESTED_SUMMARIZING);
}

Optional<VideoSummaryStatusCache> statusCache = summaryStatusCacheRepository.findFirstByVideoCode(videoCode);
if (statusCache.isPresent()) {
summaryStatusCacheRepository.save(VideoSummaryStatusCache.of(summaryInitiateRequest, userId, statusCache.get()));
Optional<VideoSummaryStatusCache> mayBeStatusCache = summaryStatusCacheRepository.findFirstByVideoCode(videoCode);
if (mayBeStatusCache.isPresent()) {
summaryStatusCacheRepository.save(VideoSummaryStatusCache.of(summaryInitiateRequest, userId, mayBeStatusCache.get()));
return new VideoSummaryInitiateResponse(videoCode);
}

Expand All @@ -61,6 +64,11 @@ public VideoSummaryInitiateResponse initiateSummarizing(VideoSummaryInitiateRequ
return new VideoSummaryInitiateResponse(videoCode);
}

private boolean checkDuplicateSummarizing(String videoCode, Long userId) {
return videoSummaryCategoryRepository.existsByVideoCodeAndUserId(videoCode, userId) ||
summaryStatusCacheRepository.existsByVideoCodeAndUserId(videoCode, userId);
}

// 무조건 중복허용이 안되는 로직
public VideoSummaryDto getVideoSummaryById(Long videoSummaryId) {
VideoSummary videoSummary = videoSummaryRepository.getReferenceById(videoSummaryId);
Expand All @@ -73,20 +81,21 @@ public VideoSummaryDto getVideoSummaryById(Long videoSummaryId) {

@Transactional
public VideoSummaryStatusResponse getStatus(String videoCode, Long userId) {
VideoSummaryStatusCache statusCache = summaryStatusCacheRepository.findByVideoCode(videoCode).get();
if (statusCache.getStatus().equals("COMPLETE")) {
VideoSummaryStatusCache statusCache = summaryStatusCacheRepository.findByVideoCodeAndUserId(videoCode, userId)
.orElseThrow(() -> new AppException(ErrorCode.SUMMARIZING_STATUS_NOT_EXIST));

if (statusCache.getStatus().equals(COMPLETE.name())) {
// Category category = categoryRepository.findDefaultCategoryByUserIdAndMainCategory(userId, statusCache.getGeneratedMainCategory()).get();
Category category = categoryRepository.findDefaultCategoryByUserIdAndMainCategory(1L, statusCache.getGeneratedMainCategory()).get();
Category category = categoryRepository.findDefaultCategoryByUserIdAndMainCategory(1L, statusCache.getGeneratedMainCategory())
.orElseThrow(() -> new AppException(ErrorCode.CATEGORY_NOT_EXIST));
VideoSummary videoSummary = videoSummaryRepository.getReferenceById(statusCache.getVideoSummaryId());

if (!videoSummaryCategoryRepository.existsByCategoryAndVideoSummary(category,videoSummary)) {
videoSummaryCategoryRepository.save(VideoSummaryCategory.builder()
.category(category)
.videoSummary(videoSummary)
.build());
}
summaryStatusCacheRepository.delete(statusCache);
videoSummaryCategoryRepository.save(VideoSummaryCategory.builder()
.category(category)
.videoSummary(videoSummary)
.build());

summaryStatusCacheRepository.delete(statusCache);
}
return VideoSummaryStatusResponse.from(statusCache);
}
Expand Down

0 comments on commit 6b7e3e1

Please sign in to comment.