diff --git a/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java b/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java index ed144bc..737d6de 100644 --- a/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java +++ b/src/main/java/com/mookive/mookive_backend/heart/application/service/HeartCreateService.java @@ -4,8 +4,13 @@ import com.mookive.mookive_backend.heart.domain.service.HeartDeleteService; import com.mookive.mookive_backend.heart.domain.service.HeartQueryService; import com.mookive.mookive_backend.heart.domain.service.HeartSaveService; +import com.mookive.mookive_backend.heartPlaylist.service.HeartPlaylistQueryService; 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.MovieInPlaylistDeleteService; +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 jakarta.transaction.Transactional; @@ -22,18 +27,30 @@ public class HeartCreateService { private final HeartDeleteService heartDeleteService; private final UserQueryService userQueryService; private final MovieQueryService movieQueryService; + private final HeartPlaylistQueryService heartPlaylistQueryService; + private final MovieInPlaylistSaveService movieInPlaylistSaveService; + private final MovieInPlaylistDeleteService movieInPlaylistDeleteService; public void createHeart(Long userId, Long movieId) { + User user = userQueryService.findById(userId); + Movie movie = movieQueryService.findById(movieId); + Playlist playlist = heartPlaylistQueryService.findByUserId(userId).getPlaylist(); + if (heartQueryService.existsByUserIdAndMovieId(userId, movieId)) { heartDeleteService.deleteByUserIdAndMovieId(userId, movieId); + movieInPlaylistDeleteService.deleteByPlaylistIdAndMovieId(playlist.getId(), movieId); } else { - User user = userQueryService.findById(userId); - Movie movie = movieQueryService.findById(movieId); Heart heart = Heart.builder() .user(user) .movie(movie) .build(); heartSaveService.save(heart); + + MovieInPlaylist movieInPlaylist = MovieInPlaylist.builder() + .movie(movie) + .playlist(playlist) + .build(); + movieInPlaylistSaveService.save(movieInPlaylist); } } } diff --git a/src/main/java/com/mookive/mookive_backend/heartPlaylist/service/HeartPlaylistQueryService.java b/src/main/java/com/mookive/mookive_backend/heartPlaylist/service/HeartPlaylistQueryService.java new file mode 100644 index 0000000..7b56c0f --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/heartPlaylist/service/HeartPlaylistQueryService.java @@ -0,0 +1,19 @@ +package com.mookive.mookive_backend.heartPlaylist.service; + +import com.mookive.mookive_backend.heartPlaylist.domain.HeartPlaylist; +import com.mookive.mookive_backend.heartPlaylist.repository.HeartPlaylistRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class HeartPlaylistQueryService { + + private final HeartPlaylistRepository heartPlaylistRepository; + + public HeartPlaylist findByUserId(Long userId) { + return heartPlaylistRepository.findByUserId(userId); + } +} diff --git a/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/respository/MovieInPlaylistRepository.java b/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/respository/MovieInPlaylistRepository.java index a184fd8..f330787 100644 --- a/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/respository/MovieInPlaylistRepository.java +++ b/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/respository/MovieInPlaylistRepository.java @@ -8,4 +8,6 @@ public interface MovieInPlaylistRepository extends JpaRepository { List findByPlaylistId(Long playlistId); + + void deleteByPlaylistIdAndMovieId(Long playlistId, Long movieId); } diff --git a/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/service/MovieInPlaylistDeleteService.java b/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/service/MovieInPlaylistDeleteService.java new file mode 100644 index 0000000..4a0084f --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/movieInPlaylist/domain/service/MovieInPlaylistDeleteService.java @@ -0,0 +1,18 @@ +package com.mookive.mookive_backend.movieInPlaylist.domain.service; + +import com.mookive.mookive_backend.movieInPlaylist.domain.respository.MovieInPlaylistRepository; +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +@Transactional +public class MovieInPlaylistDeleteService { + + private final MovieInPlaylistRepository movieInPlaylistRepository; + + public void deleteByPlaylistIdAndMovieId(Long playlistId, Long movieId) { + movieInPlaylistRepository.deleteByPlaylistIdAndMovieId(playlistId, movieId); + } +}