Skip to content

Commit

Permalink
엔티티 전체 수정 - 비디오 생성, 조회, 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
chobeebee committed Jul 6, 2024
1 parent 04791bb commit ea0e3ec
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sparta.binplay.controller;

import com.sparta.binplay.dto.request.StreamRequestDto;
import com.sparta.binplay.dto.request.VideoRequestDto;
import com.sparta.binplay.dto.response.StreamResponseDto;
import com.sparta.binplay.dto.response.VideoAdResponseDto;
Expand Down Expand Up @@ -63,8 +64,8 @@ public String deleteVideo(@PathVariable Long videoId) throws Exception{

//비디오 재생
@PostMapping("/play/{video-id}") //<?> : hidden
public ResponseEntity<VideoResponseDto> videoPlay(@PathVariable("video-id") Long videoId, @AuthenticationPrincipal CustomOAuth2User user){
return ResponseEntity.ok().body(videoService.play(videoId, user.getUsername()));
public ResponseEntity<StreamResponseDto> videoPlay(@PathVariable("video-id") Long videoId, @AuthenticationPrincipal CustomOAuth2User user, @RequestBody StreamRequestDto streamRequestDto) {
return ResponseEntity.ok().body(streamService.play(videoId, user.getUsername(),streamRequestDto));
}

//비디오 중단
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package com.sparta.binplay.dto.request;

import com.sparta.binplay.entity.Ads;
import com.sparta.binplay.entity.AdViews;
import com.sparta.binplay.entity.Videos;
import lombok.Getter;

@Getter
public class VideoAdRequestDto {
private Long adViews;
private Videos video;
private Ads ad;
private AdViews adViews;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
@Getter
public class VideoRequestDto {
@NotBlank(message = "동영상 제목은 필수입니다.")
private Long videoId;
private String title;
private String description;
private long videoLength;

public VideoRequestDto(String title, String description, long videoLength) {
public VideoRequestDto(Long videoId, String title, String description, long videoLength) {
this.videoId = videoId;
this.title = title;
this.description = description;
this.videoLength = videoLength;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sparta.binplay.dto.response;

import com.sparta.binplay.entity.AdViews;
import com.sparta.binplay.entity.VideoAd;
import lombok.*;

Expand All @@ -10,12 +11,12 @@
@Builder
public class VideoAdResponseDto {
private Long videoAdId;
private Long adViews;
private AdViews adViews;

public static VideoAdResponseDto from(VideoAd videoAd) {
return VideoAdResponseDto.builder()
.videoAdId(videoAd.getVideoAdId())
.adViews(videoAd.getAdViews())
.adViews(videoAd.getAdView())
.build();
}
}
26 changes: 21 additions & 5 deletions src/main/java/com/sparta/binplay/entity/VideoAd.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

@Entity
@Getter
@Table(name="video_ads")
@Table(name="video_ad")
@NoArgsConstructor
@Builder
@AllArgsConstructor
Expand All @@ -19,8 +19,11 @@ public class VideoAd {
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long videoAdId;

@Column(name = "ad_views")
private Long adViews;
@Column(name="view_count", nullable = false)
private long viewCount;

@Column(name="stat_is")
private boolean stat_is;

@ManyToOne
@JoinColumn(name = "video_id", nullable = false)
Expand All @@ -30,7 +33,20 @@ public class VideoAd {
@JoinColumn(name = "ad_id", nullable = false)
private Ads ad;

public void countAd() {
this.adViews++;
@ManyToOne
@JoinColumn(name = "ad_view_id", nullable = false)
private AdViews adView;

// 생성자
public VideoAd(Ads ad, Videos video, int viewCount, boolean stat_is, AdViews adView) {
this.ad = ad;
this.video = video;
this.viewCount = viewCount;
this.stat_is = stat_is;
this.adView = adView;
}

public void setAdView(AdViews adView) {
this.adView = adView;
}
}
17 changes: 8 additions & 9 deletions src/main/java/com/sparta/binplay/entity/Videos.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,19 @@ public class Videos extends Timestamped {
@Builder.Default
@OneToMany(mappedBy = "video")
private List<VideoAd> videoAd = new ArrayList<>();
//
// @OneToMany(mappedBy = "videos")
// private List<Statistics> statistics = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "video")
private List<StatisticVideo> statisticsVideo = new ArrayList<>();

@Builder.Default
@OneToMany(mappedBy = "video")
private List<PaymentVideo> paymentsVideo = new ArrayList<>();

public Videos(String title, String description, long videoLength) {
this.title = title;
this.description = description;
// this.views = videoRequestDto.getViews();
this.videoLength = videoLength;
//this.user = videoRequestDto.getUser();
}

public static Videos of(Users user, VideoRequestDto videoRequestDto) {
Expand All @@ -80,8 +83,4 @@ public void viewUp() {
public void addVideoAd(VideoAd videoAd) {
this.videoAd.add(videoAd);
}




}
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.sparta.binplay.repository;

import com.sparta.binplay.entity.VideoAd;
import com.sparta.binplay.entity.Videos;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface VideoAdRepository extends JpaRepository<VideoAd, Long> {
List<VideoAd> findByVideo(Videos video);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.sparta.binplay.repository;

import com.sparta.binplay.entity.Users;
import com.sparta.binplay.entity.Videos;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
Expand All @@ -12,11 +11,6 @@
public interface VideoRepository extends JpaRepository<Videos, Long> {
List<Videos> findAll();


List<Videos> findAllByUser(Users user);
//
// List<Videos> findAllByUserId(Long userId);

void deleteByVideoId(Long videoId);

List<Videos> findByUserUserId(Long userId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public class VideoAdService {
public VideoAdResponseDto updateAdCount(Long videoAdId) {
VideoAd videoAd = videoAdRepository.findById(videoAdId).orElseThrow(() -> new RuntimeException("영상을 찾을 수 없습니다."));

videoAd.countAd();
videoAdRepository.save(videoAd);

return VideoAdResponseDto.from(videoAd);
Expand Down
69 changes: 38 additions & 31 deletions src/main/java/com/sparta/binplay/service/VideoService.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
public class VideoService {
private final VideoRepository videoRepository;
private final UserRepository userRepository;
private final StreamRepository streamRepository;
private final AdRepository adRepository;
private final AdViewRepository adViewRepository;
private final VideoAdRepository videoAdRepository;


//모든
public List<Videos> findAllVideos() {
return videoRepository.findAll();
}

//회원 찾기
private Users getAuthenticatedUser() throws Exception {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
CustomOAuth2User customUserDetails = (CustomOAuth2User) authentication.getPrincipal();
Expand All @@ -46,12 +46,13 @@ public VideoResponseDto createVideo(VideoRequestDto videoRequestDto) throws Exce
return VideoResponseDto.from(saveVideo);
}

@Transactional
public void matchAd(VideoRequestDto videoRequestDto) throws Exception {
Users user = getAuthenticatedUser();
Videos saveVideo = videoRepository.save(Videos.of(user, videoRequestDto));
Videos savedVideo = videoRepository.save(Videos.of(user, videoRequestDto));

// 광고 개수 계산 및 저장
int numberOfAds = (int) (saveVideo.getVideoLength() / (5 * 60)); // 5분마다 1개의 광고 (5분 = 300초)
int numberOfAds = (int) (savedVideo.getVideoLength() / (5 * 60)); // 5분마다 1개의 광고 (5분 = 300초)
List<Ads> adsList = adRepository.findAll();

Random random = new Random();
Expand All @@ -60,14 +61,24 @@ public void matchAd(VideoRequestDto videoRequestDto) throws Exception {
int randomAdIndex = random.nextInt(adsList.size());
Ads ad = adsList.get(randomAdIndex);

// AdViews 생성
AdViews adView = new AdViews();
AdViews savedAdView = adViewRepository.save(adView);

// VideoAd 생성 및 저장
VideoAd videoAd = VideoAd.builder()
.ad(ad)
.adViews(0L)
.video(saveVideo)
.viewCount(0)
.video(savedVideo)
.stat_is(false)
.adView(savedAdView)
.build();

// videoAd를 저장
videoAdRepository.save(videoAd);
VideoAd savedVideoAd = videoAdRepository.save(videoAd);

// AdViews와 VideoAd 연결
savedAdView.setVideoAd(savedVideoAd);
adViewRepository.save(savedAdView);
}
}

Expand All @@ -86,7 +97,7 @@ public List<VideoResponseDto> getVideoList() throws Exception {
@Transactional
public VideoResponseDto updateVideo(Long videoId, VideoRequestDto videoRequestDto) throws Exception {
Users user = getAuthenticatedUser(); //권한
Videos video = videoRepository.findByVideoId(videoId);
Videos video = videoRepository.findByVideoId(videoId).orElseThrow(() -> new RuntimeException("비디오를 찾을 수 없음"));

video.update(videoRequestDto);
videoRepository.save(video);
Expand All @@ -96,30 +107,26 @@ public VideoResponseDto updateVideo(Long videoId, VideoRequestDto videoRequestDt

// 비디오 삭제
@Transactional
public void deleteVideo(Long videoId) {
Videos video = getVideos(videoId);
Users user = video.getUser();
user.getVideos().remove(video); // User의 비디오 리스트에서 비디오 제거
videoRepository.delete(video);
}

// 비디오 재생 조회수
@Transactional
public VideoResponseDto play(Long videoId, String username) {

Videos videos = getVideos(videoId);
Users uploadUser = videos.getUser();
Users watchUser = getByUsername(username);
boolean matchUser = uploadUser.getUserId().equals(watchUser.getUserId()); //어뷰징에 사용

if (!matchUser) {
videos.viewUp();
}

return VideoResponseDto.from(videos);
public void deleteVideo(Long videoId) throws Exception {
// Videos video = videoRepository.findById(videoId)
// .orElseThrow(() -> new Exception("Video not found"));
//
// // VideoAd 엔티티의 관계를 해제
// List<VideoAd> videoAds = videoAdRepository.findByVideo(video);
// for (VideoAd videoAd : videoAds) {
// AdViews adView = videoAd.getAdViews();
// if (adView != null) {
// adView.setVideoAd(null); // 관계 해제
// adViewRepository.delete(adView);
// }
// videoAdRepository.delete(videoAd);
// }
//
// // Video 엔티티 삭제
// videoRepository.delete(video);
}

//회원 찾기 //ifpresent?
//회원 찾기
private Users getByUsername(String username) {
return userRepository.findByUsername(username).orElseThrow(() -> new RuntimeException("회원을 찾을 수 없음"));
}
Expand Down

0 comments on commit ea0e3ec

Please sign in to comment.