Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Video Tags 구현 #19

Merged
merged 4 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.Getter;
Expand Down Expand Up @@ -47,6 +50,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 @@ -76,4 +82,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())
);
}

}