Skip to content

Commit

Permalink
Merge pull request #19 from Team-J9/feature/video-tag
Browse files Browse the repository at this point in the history
Video Tags 구현
  • Loading branch information
SJ70 authored Jul 18, 2024
2 parents f0a7b7c + ee9f66c commit bbd7c75
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
18 changes: 18 additions & 0 deletions src/main/java/com/j9/bestmoments/controller/MyVideoController.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -100,4 +101,21 @@ public ResponseEntity<Page<VideoPreviewDto>> findDeletedVideos(Pageable pageable
return ResponseEntity.ok(videos.map(VideoPreviewDto::of));
}

@Operation(summary = "태그 수정")
@PostMapping("/{videoId}/tags")
public ResponseEntity<?> findAllByTag(@PathVariable UUID videoId, @RequestBody List<String> tags) {
UUID memberId = UUID.fromString(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
Video video = videoService.findByIdAndUploaderId(videoId, memberId);
videoService.setVideoTags(video, tags);
return ResponseEntity.ok(null);
}

@Operation(summary = "태그로 동영상 목록 조회")
@GetMapping("/tags/{tag}")
public ResponseEntity<Page<VideoPreviewDto>> findAllByTag(@PathVariable String tag, Pageable pageable) {
UUID memberId = UUID.fromString(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString());
Page<Video> videos = videoService.findAllByUploaderAndTag(memberId, tag, pageable);
return ResponseEntity.ok(videos.map(VideoPreviewDto::of));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,18 @@ public ResponseEntity<VideoFindDto> view(@PathVariable UUID videoId) {
return ResponseEntity.ok(VideoFindDto.of(video));
}

@Operation(summary = "사용자의 동영상 목록 조회")
@Operation(summary = "게시자로 동영상 목록 조회")
@GetMapping("/by/{memberId}")
public ResponseEntity<Page<VideoPreviewDto>> findAllByUploaderId(@PathVariable UUID memberId, Pageable pageable) {
Page<Video> videos = videoService.findAllPublicByUploaderId(memberId, pageable);
return ResponseEntity.ok(videos.map(VideoPreviewDto::of));
}

@Operation(summary = "태그로 동영상 목록 조회")
@GetMapping("/tags/{tag}")
public ResponseEntity<Page<VideoPreviewDto>> findAllByTag(@PathVariable String tag, Pageable pageable) {
Page<Video> videos = videoService.findAllPublicByTag(tag, pageable);
return ResponseEntity.ok(videos.map(VideoPreviewDto::of));
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/j9/bestmoments/domain/Video.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.j9.bestmoments.domain;

import jakarta.persistence.Column;
import jakarta.persistence.ElementCollection;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.EnumType;
Expand All @@ -11,6 +12,8 @@
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import lombok.Builder;
import lombok.EqualsAndHashCode;
Expand Down Expand Up @@ -49,6 +52,9 @@ public class Video {
private LocalDateTime createdAt;
private LocalDateTime deletedAt;

@ElementCollection
private List<String> tags = new ArrayList<>();

@Builder
public Video(Member uploader, String fileUrl, String title, String description, VideoStatus videoStatus) {
this.uploader = uploader;
Expand Down Expand Up @@ -78,4 +84,8 @@ public void setVideoStatus(VideoStatus videoStatus) {
this.videoStatus = videoStatus;
}

public void setVideoTags(List<String> tags) {
this.tags = tags;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,8 @@ public interface VideoRepository extends JpaRepository<Video, UUID> {

Optional<Video> findByIdAndUploaderId(UUID id, UUID uploaderId);

Page<Video> findAllByTagsContainsAndVideoStatusAndDeletedAtIsNull(String tag, VideoStatus videoStatus, PageRequest pageRequest);

Page<Video> findAllByUploaderIdAndTagsContainsAndDeletedAtIsNull(UUID uploaderId, String tag, PageRequest pageRequest);

}
23 changes: 23 additions & 0 deletions src/main/java/com/j9/bestmoments/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.j9.bestmoments.repository.VideoRepository;
import jakarta.persistence.EntityNotFoundException;
import jakarta.security.auth.message.AuthException;
import java.util.List;
import java.util.UUID;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
Expand Down Expand Up @@ -91,6 +92,12 @@ public Video restore(Video video) {
return video;
}

@Transactional
public void setVideoTags(Video video, List<String> tags) {
video.setVideoTags(tags);
videoRepository.save(video);
}

public Page<Video> findAllActivatedByUploaderId(UUID memberId, Pageable pageable) {
return videoRepository.findAllByUploaderIdAndDeletedAtIsNull(
memberId,
Expand All @@ -113,4 +120,20 @@ public Page<Video> findAllPublicByUploaderId(UUID memberId, Pageable pageable) {
);
}

public Page<Video> findAllPublicByTag(String tag, Pageable pageable) {
return videoRepository.findAllByTagsContainsAndVideoStatusAndDeletedAtIsNull(
tag,
VideoStatus.PUBLIC,
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize())
);
}

public Page<Video> findAllByUploaderAndTag(UUID memberId, String tag, Pageable pageable) {
return videoRepository.findAllByUploaderIdAndTagsContainsAndDeletedAtIsNull(
memberId,
tag,
PageRequest.of(pageable.getPageNumber(), pageable.getPageSize())
);
}

}

0 comments on commit bbd7c75

Please sign in to comment.