Skip to content

Commit

Permalink
[feat] 봤어요 변동에 따른 플레이리스트 반영 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
syyling committed May 20, 2024
1 parent 5ce5de8 commit a55f0dc
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mookive.mookive_backend.watched.application.service;

import com.mookive.mookive_backend.movie.domain.entity.Movie;
import com.mookive.mookive_backend.movie.domain.service.MovieQueryService;
import com.mookive.mookive_backend.movieInPlaylist.domain.entity.MovieInPlaylist;
import com.mookive.mookive_backend.movieInPlaylist.domain.service.MovieInPlaylistDeleteDomainService;
import com.mookive.mookive_backend.movieInPlaylist.domain.service.MovieInPlaylistSaveService;
import com.mookive.mookive_backend.playlist.domain.entity.Playlist;
import com.mookive.mookive_backend.user.domain.entity.User;
import com.mookive.mookive_backend.user.domain.service.UserQueryService;
import com.mookive.mookive_backend.watched.domain.entity.Watched;
import com.mookive.mookive_backend.watched.domain.service.WatchedDeleteService;
import com.mookive.mookive_backend.watched.domain.service.WatchedQueryService;
import com.mookive.mookive_backend.watched.domain.service.WatchedSaveService;
import com.mookive.mookive_backend.watchedPlaylist.domain.service.WatchedPlaylistQueryService;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class WatchedCreateService {

private final UserQueryService userQueryService;
private final MovieQueryService movieQueryService;
private final WatchedPlaylistQueryService watchedPlaylistQueryService;
private final WatchedQueryService watchedQueryService;
private final WatchedDeleteService watchedDeleteService;
private final MovieInPlaylistDeleteDomainService movieInPlaylistDeleteService;
private final WatchedSaveService watchedSaveService;
private final MovieInPlaylistSaveService movieInPlaylistSaveService;

public void createWatched(Long userId, Long movieId) {
User user = userQueryService.findById(userId);
Movie movie = movieQueryService.findById(movieId);
Playlist playlist = watchedPlaylistQueryService.findByUserId(userId).getPlaylist();

if(watchedQueryService.existsByUserIdAndMovieId(userId, movieId)) {
watchedDeleteService.deleteByUserIdAndMovieId(userId, movieId);
movieInPlaylistDeleteService.deleteByPlaylistIdAndMovieId(playlist.getId(), movieId);
} else {
Watched watched = Watched.builder()
.user(user)
.movie(movie)
.build();
watchedSaveService.save(watched);

MovieInPlaylist movieInPlaylist = MovieInPlaylist.builder()
.movie(movie)
.playlist(playlist)
.build();
movieInPlaylistSaveService.save(movieInPlaylist);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,7 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface WatchedRepository extends JpaRepository<Watched, Long> {

boolean existsByUserIdAndMovieId(Long userId, Long movieId);
void deleteByUserIdAndMovieId(Long userId, Long movieId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mookive.mookive_backend.watched.domain.service;

import com.mookive.mookive_backend.watched.domain.repository.WatchedRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Transactional
@RequiredArgsConstructor
@Service
public class WatchedDeleteService {

private final WatchedRepository watchedRepository;

public void deleteByUserIdAndMovieId(Long userId, Long movieId) {
watchedRepository.deleteByUserIdAndMovieId(userId, movieId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mookive.mookive_backend.watched.domain.service;

import com.mookive.mookive_backend.watched.domain.repository.WatchedRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Transactional
@RequiredArgsConstructor
@Service
public class WatchedQueryService {

private final WatchedRepository watchedRepository;

public boolean existsByUserIdAndMovieId(Long userId, Long movieId) {
return watchedRepository.existsByUserIdAndMovieId(userId, movieId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mookive.mookive_backend.watched.domain.service;


import com.mookive.mookive_backend.watched.domain.entity.Watched;
import com.mookive.mookive_backend.watched.domain.repository.WatchedRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Transactional
@RequiredArgsConstructor
@Service
public class WatchedSaveService {

private final WatchedRepository watchedRepository;

public void save(Watched watched) {
watchedRepository.save(watched);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mookive.mookive_backend.watched.presentation;


import com.mookive.mookive_backend.watched.application.service.WatchedCreateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class WatchedController {

private final WatchedCreateService watchedCreateService;

@PostMapping("/watched")
public void createWatched(@RequestParam Long userId, @RequestParam Long movieId) {
watchedCreateService.createWatched(userId, movieId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@
import org.springframework.data.jpa.repository.JpaRepository;

public interface WatchedPlaylistRepository extends JpaRepository<WatchedPlaylist, Long> {

WatchedPlaylist findByUserId(Long userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.mookive.mookive_backend.watchedPlaylist.domain.service;

import com.mookive.mookive_backend.watchedPlaylist.domain.entity.WatchedPlaylist;
import com.mookive.mookive_backend.watchedPlaylist.domain.repository.WatchedPlaylistRepository;
import jakarta.transaction.Transactional;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Transactional
public class WatchedPlaylistQueryService {

private final WatchedPlaylistRepository watchedPlaylistRepository;

public WatchedPlaylist findByUserId(Long userId) {
return watchedPlaylistRepository.findByUserId(userId);
}
}

0 comments on commit a55f0dc

Please sign in to comment.