Skip to content

Commit

Permalink
Merge pull request #16 from HongikGraduationProject/video
Browse files Browse the repository at this point in the history
Video
  • Loading branch information
qjvk2880 authored Apr 13, 2024
2 parents 1b167dd + 6772766 commit 4a82280
Show file tree
Hide file tree
Showing 19 changed files with 206 additions and 24 deletions.
14 changes: 14 additions & 0 deletions src/main/java/com/hongik/graduationproject/config/WebConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.hongik.graduationproject.config;

import com.hongik.graduationproject.config.converter.MainCategoryRequestConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.format.FormatterRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addFormatters(FormatterRegistry registry) {
registry.addConverter(new MainCategoryRequestConverter());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.hongik.graduationproject.config.converter;

import com.hongik.graduationproject.eum.MainCategory;
import com.hongik.graduationproject.exception.AppException;
import com.hongik.graduationproject.exception.ErrorCode;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.core.convert.converter.Converter;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

public class MainCategoryRequestConverter implements Converter<String, MainCategory> {
@Override
public MainCategory convert(String source) {
return MainCategory.valueOf(source.toUpperCase());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.hongik.graduationproject.controller;

import com.hongik.graduationproject.domain.dto.Response;
import com.hongik.graduationproject.domain.dto.category.SubCategoryListResponse;
import com.hongik.graduationproject.domain.dto.video.VideoSummaryListResponse;
import com.hongik.graduationproject.eum.MainCategory;
import com.hongik.graduationproject.service.CategoryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api")
public class CategoryController {
private final CategoryService categoryService;

@Operation(summary = "서브 카테고리 목록 조회", description = "사용자의 특정 메인 카테고리에 해당하는 서브 카테고리 목록 조회를 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = SubCategoryListResponse.class)))
@ResponseStatus(HttpStatus.OK)
@GetMapping("/categories")
public Response<SubCategoryListResponse> getSubCategoryList(@RequestParam MainCategory mainCategory) {
return Response.createSuccess(categoryService.getSubCategoryList(mainCategory));
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.hongik.graduationproject.controller;

import com.hongik.graduationproject.domain.dto.Response;
import com.hongik.graduationproject.domain.dto.video.VideoSummaryDto;
import com.hongik.graduationproject.domain.dto.video.VideoSummaryInitiateRequest;
import com.hongik.graduationproject.domain.dto.video.VideoSummaryInitiateResponse;
import com.hongik.graduationproject.domain.dto.video.VideoSummaryStatusResponse;
import com.hongik.graduationproject.domain.dto.video.*;
import com.hongik.graduationproject.service.VideoSummaryService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
Expand All @@ -25,7 +22,7 @@
public class VideoSummaryController {
private final VideoSummaryService videoSummaryService;

@Operation(summary = "카테고리 미지정 영상 요약 요청", description = "영상 요약 요청을 위한 메소드")
@Operation(summary = "영상 요약 요청", description = "영상 요약 요청을 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = VideoSummaryInitiateResponse.class)))
@PostMapping("/summaries/initiate")
@ResponseStatus(HttpStatus.ACCEPTED)
Expand Down Expand Up @@ -56,6 +53,14 @@ public Response<VideoSummaryDto> getSummaryByVideoSummaryId(@PathVariable(name =
return Response.createSuccess(videoSummaryService.getVideoSummaryById(videoSummaryId));
}

@Operation(summary = "영상 요약 목록 조회", description = "categoryId로 영상 요약 목록 조회를 위한 메소드")
@ApiResponse(content = @Content(schema = @Schema(implementation = VideoSummaryListResponse.class)))
@GetMapping("/summaries")
@ResponseStatus(HttpStatus.OK)
public Response<VideoSummaryListResponse> getAllSummariesByCategoryId(@Parameter(required = true) @RequestParam Long categoryId) {
return Response.createSuccess(videoSummaryService.getAllSummariesByCategoryId(categoryId));
}

@GetMapping("/hi")
public Response<String> hi() {
return Response.createSuccess("hi");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.hongik.graduationproject.domain.dto.category;

import com.hongik.graduationproject.eum.MainCategory;
import lombok.Data;

public record SubCategoryListRequest(
MainCategory mainCategory
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.hongik.graduationproject.domain.dto.category;

import java.util.List;

public record SubCategoryListResponse(
List<SubCategoryResponse> subCategoryList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.hongik.graduationproject.domain.dto.category;

public record SubCategoryResponse (
String subCategory,
Long categoryId
){
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hongik.graduationproject.domain.entity.VideoSummary;
import com.hongik.graduationproject.eum.MainCategory;
import com.hongik.graduationproject.eum.Platform;
import lombok.AllArgsConstructor;
import lombok.Builder;
Expand All @@ -27,6 +28,7 @@ public class VideoSummaryDto {
String address;
LocalDateTime createdAt;
Platform platform;
String mainCategory;


public static VideoSummaryDto from(VideoSummary videoSummary) {
Expand All @@ -40,6 +42,7 @@ public static VideoSummaryDto from(VideoSummary videoSummary) {
.address(videoSummary.getAddress())
.createdAt(videoSummary.getCreatedAt().minusHours(9))
.platform(videoSummary.getPlatform())
.mainCategory(videoSummary.getGeneratedMainCategory().getName())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ public class VideoSummaryInitiateRequest {
String url;
@Schema(description = "카테고리 아이디", nullable = true, example = "1")
Long categoryId;
@Schema(description = "(임시) 사용자가 카테고리를 지정했는지 여부", example = "true")
@Schema(description = "사용자가 카테고리를 지정했는지 여부", example = "true")
Boolean isCategoryIncluded;

@Schema(description = "userId 첨부하지 않아도 작동합니다!!! 이 필드는 무시하세요!!")
Long userId = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.hongik.graduationproject.domain.dto.video;


import com.hongik.graduationproject.domain.entity.VideoSummary;

import java.util.List;

public record VideoSummaryListResponse(
List<VideoSummaryResponse> videoSummaryList
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.hongik.graduationproject.domain.dto.video;

import com.hongik.graduationproject.domain.entity.VideoSummary;
import com.hongik.graduationproject.eum.Platform;

import java.util.Arrays;
import java.util.List;

public record VideoSummaryResponse(
String title,
List<String> keywords,
String url,
String address,
Platform platform
) {
public VideoSummaryResponse(VideoSummary videoSummary) {
this(
videoSummary.getTitle(),
Arrays.stream(videoSummary.getKeywords().split(",")).toList(),
videoSummary.getUrl(),
videoSummary.getAddress(),
videoSummary.getPlatform()
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private static String listToString(List<String> keywords) {
for (String keyword : keywords) {
sb.append(keyword).append(',');
}
return sb.substring(0, sb.length() - 2);
return sb.substring(0, sb.length() - 1);
} else {
return "";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
public enum ErrorCode {
INVALID_VIDEO_URL(HttpStatus.BAD_REQUEST, "유효하지 않은 영상 URL입니다."),
ALREADY_REQUESTED_SUMMARIZING(HttpStatus.BAD_REQUEST, "사용자가 이미 해당 영상을 요약 요청했습니다. 잠시 후 다시 시도하세요"),
FAILED_TO_EXTRACT_EXTRACT_ID(HttpStatus.BAD_REQUEST, "영상의 고유 ID를 추출하는데 실패했습니다.");
FAILED_TO_EXTRACT_EXTRACT_ID(HttpStatus.BAD_REQUEST, "영상의 고유 ID를 추출하는데 실패했습니다."),
VIDEO_SUMMARY_NOT_FOUND(HttpStatus.NOT_FOUND,"영상 요약 정보를 찾을 수 없습니다"),
MAIN_CATEGORY_NOT_EXISTS(HttpStatus.BAD_REQUEST,"해당 메인 카테고리가 존재하지 않습니다.");

private final HttpStatus httpStatus;
private final String message;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.convert.ConversionFailedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.method.annotation.MethodArgumentConversionNotSupportedException;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

@RestControllerAdvice
Expand All @@ -19,4 +23,12 @@ ResponseEntity<ErrorResponse> AppExceptionHandler(AppException ex, HttpServletRe
.body(ErrorResponse.of(ex.getErrorCode()));
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
public ResponseEntity<ErrorResponse> handleConflict(MethodArgumentTypeMismatchException ex) {
log.error("ConversionFailedException: {}", ex.getMessage());
return ResponseEntity
.status(ErrorCode.MAIN_CATEGORY_NOT_EXISTS.getHttpStatus())
.body(ErrorResponse.of(ErrorCode.MAIN_CATEGORY_NOT_EXISTS));
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.hongik.graduationproject.repository;

import com.hongik.graduationproject.domain.entity.Category;
import com.hongik.graduationproject.domain.entity.User;
import com.hongik.graduationproject.eum.MainCategory;
import jakarta.validation.constraints.Min;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

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

public interface CategoryRepository extends JpaRepository<Category, Long> {
@Query("select c from Category c where c.user.id = :userId and c.mainCategory = :mainCategory and c.subCategory = '기타'")
Optional<Category> findDefaultCategoryByUserIdAndMainCategory(Long userId, MainCategory mainCategory);

List<Category> findAllByMainCategoryAndUser(MainCategory mainCategory, User user);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends JpaRepository<User, String> {

public interface UserRepository extends JpaRepository<User, Long> {

public User findByEmail(String email);
public User findByKakaoId(Long kakaoId);

Optional<User> findById(Long id);
Optional<User> findByEmail(String email);
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package com.hongik.graduationproject.repository;

import com.hongik.graduationproject.domain.entity.Category;
import com.hongik.graduationproject.domain.entity.VideoSummaryCategory;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface VideoSummaryCategoryRepository extends JpaRepository<VideoSummaryCategory, Long> {
List<VideoSummaryCategory> findAllByCategory(Category category);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.hongik.graduationproject.service;

import com.hongik.graduationproject.domain.dto.category.SubCategoryListRequest;
import com.hongik.graduationproject.domain.dto.category.SubCategoryListResponse;
import com.hongik.graduationproject.domain.dto.category.SubCategoryResponse;
import com.hongik.graduationproject.domain.entity.User;
import com.hongik.graduationproject.eum.MainCategory;
import com.hongik.graduationproject.repository.CategoryRepository;
import com.hongik.graduationproject.repository.UserRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

@Service
@RequiredArgsConstructor
public class CategoryService {
private final CategoryRepository categoryRepository;
private final UserRepository userRepository;

public SubCategoryListResponse getSubCategoryList(MainCategory mainCategory) {
User user = userRepository.getReferenceById(1L);
// MainCategory mainCategory = subCategoryListRequest.mainCategory();/

List<SubCategoryResponse> subCategoryList = categoryRepository.findAllByMainCategoryAndUser(mainCategory, user).stream()
.map(category -> new SubCategoryResponse(category.getSubCategory(), category.getId()))
.toList();
return new SubCategoryListResponse(subCategoryList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

@Service
Expand Down Expand Up @@ -60,20 +61,9 @@ public VideoSummaryInitiateResponse initiateSummarizing(VideoSummaryInitiateRequ
return new VideoSummaryInitiateResponse(videoCode);
}

private static VideoSummaryStatusCache of(VideoSummaryInitiateRequest summaryInitiateRequest, String videoCode, Long userId) {
return VideoSummaryStatusCache.builder()
.videoCode(videoCode)
.videoSummaryId(-1L)
.status("PROCESSING")
.userId(userId)
.isCategoryIncluded(summaryInitiateRequest.getIsCategoryIncluded())
.categoryId(summaryInitiateRequest.getCategoryId())
.build();
}

public VideoSummaryDto getVideoSummaryById(Long videoSummaryId) {
Optional<VideoSummary> videoSummary = videoSummaryRepository.findById(videoSummaryId);
return VideoSummaryDto.from(videoSummary.get());
VideoSummary videoSummary = videoSummaryRepository.findById(videoSummaryId).orElseThrow(() -> new AppException(ErrorCode.VIDEO_SUMMARY_NOT_FOUND));
return VideoSummaryDto.from(videoSummary);
}

@Transactional
Expand All @@ -90,4 +80,12 @@ public VideoSummaryStatusResponse getStatus(String videoCode) {
}
return VideoSummaryStatusResponse.from(statusCache);
}

public VideoSummaryListResponse getAllSummariesByCategoryId(Long categoryId) {
Category category = categoryRepository.getReferenceById(categoryId);
List<VideoSummaryResponse> videoSummaryResponseList = videoSummaryCategoryRepository.findAllByCategory(category).stream()
.map(videoSummaryCategory -> new VideoSummaryResponse(videoSummaryCategory.getVideoSummary()))
.toList();
return new VideoSummaryListResponse(videoSummaryResponseList);
}
}

0 comments on commit 4a82280

Please sign in to comment.