diff --git a/src/main/java/com/mookive/mookive_backend/record/application/service/RecordCreateService.java b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordCreateService.java index 12d59ff..3904f7f 100644 --- a/src/main/java/com/mookive/mookive_backend/record/application/service/RecordCreateService.java +++ b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordCreateService.java @@ -7,6 +7,9 @@ import com.mookive.mookive_backend.keyword.domain.service.KeywordSaveService; 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.MovieInPlaylistSaveService; +import com.mookive.mookive_backend.playlist.domain.entity.Playlist; import com.mookive.mookive_backend.record.application.dto.request.RecordRequest; import com.mookive.mookive_backend.record.application.mapper.RecordMapper; import com.mookive.mookive_backend.record.domain.entity.Record; @@ -14,6 +17,11 @@ import com.mookive.mookive_backend.record.domain.service.RecordSaveService; import com.mookive.mookive_backend.user.domain.entity.User; import com.mookive.mookive_backend.user.domain.service.UserQueryService; +import com.mookive.mookive_backend.watched.application.mapper.WatchedMapper; +import com.mookive.mookive_backend.watched.domain.entity.Watched; +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; @@ -33,11 +41,33 @@ public class RecordCreateService { private final KeywordSaveService keywordSaveService; private final KeywordDeleteService keywordDeleteService; private final RecordQueryService recordQueryService; + private final WatchedQueryService watchedQueryService; + private final WatchedSaveService watchedSaveService; + private final WatchedPlaylistQueryService watchedPlaylistQueryService; + private final MovieInPlaylistSaveService movieInPlaylistSaveService; + /** + * Todo + * WatchedCreateService와 중복되는 코드 많음 -> 리팩토링 필요 + */ public void createRecord(RecordRequest.RecordCreateRequest recordCreateRequest) { Long movieId = recordCreateRequest.getMovieId(); Long userId = recordCreateRequest.getUserId(); + Movie movie = movieQueryService.findById(movieId); + User user = userQueryService.findById(userId); Optional optionalRecord = recordQueryService.findByUserIdAndMovieId(userId, movieId); + if(!watchedQueryService.existsByUserIdAndMovieId(userId, movieId)){ + Watched watched = WatchedMapper.mapToWatched(user, movie); + watchedSaveService.save(watched); + + Playlist playlist = watchedPlaylistQueryService.findByUserId(userId).getPlaylist(); + System.out.println(playlist.getId()); + MovieInPlaylist movieInPlaylist = MovieInPlaylist.builder() + .movie(movie) + .playlist(playlist) + .build(); + movieInPlaylistSaveService.save(movieInPlaylist); + } if(optionalRecord.isPresent()){ Record record = optionalRecord.get(); LocalDate date = recordCreateRequest.getDate(); @@ -49,8 +79,6 @@ public void createRecord(RecordRequest.RecordCreateRequest recordCreateRequest) saveKeywords(keywords, record, movieId); } else { - Movie movie = movieQueryService.findById(movieId); - User user = userQueryService.findById(userId); Record record = RecordMapper.mapToRecord(recordCreateRequest, movie, user); recordSaveService.saveRecord(record); ArrayList keywords = recordCreateRequest.getKeywords(); diff --git a/src/main/java/com/mookive/mookive_backend/record/application/service/RecordDeleteService.java b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordDeleteService.java index 4334360..5bf30c6 100644 --- a/src/main/java/com/mookive/mookive_backend/record/application/service/RecordDeleteService.java +++ b/src/main/java/com/mookive/mookive_backend/record/application/service/RecordDeleteService.java @@ -1,7 +1,11 @@ package com.mookive.mookive_backend.record.application.service; import com.mookive.mookive_backend.keyword.domain.service.KeywordDeleteService; +import com.mookive.mookive_backend.movieInPlaylist.domain.service.MovieInPlaylistDeleteDomainService; import com.mookive.mookive_backend.record.domain.service.RecordDeleteDomainService; +import com.mookive.mookive_backend.record.domain.service.RecordQueryService; +import com.mookive.mookive_backend.watched.domain.service.WatchedDeleteService; +import com.mookive.mookive_backend.watchedPlaylist.domain.service.WatchedPlaylistQueryService; import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -14,12 +18,23 @@ public class RecordDeleteService { /** * 추후 annotation 도입 시 변경 예정 */ + private final RecordQueryService recordQueryService; private final RecordDeleteDomainService recordDeleteDomainService; private final KeywordDeleteService keywordDeleteService; + private final WatchedDeleteService watchedDeleteService; + private final WatchedPlaylistQueryService watchedPlaylistQueryService; + private final MovieInPlaylistDeleteDomainService movieInPlaylistDeleteService; + + public void deleteRecord(Long userId, Long movieId) { + recordQueryService.findByUserIdAndMovieId(userId, movieId) + .ifPresent(record -> { + keywordDeleteService.deleteAllByRecordId(record.getId()); + recordDeleteDomainService.delete(record.getId()); + watchedDeleteService.deleteByUserIdAndMovieId(userId, movieId); + Long watchedPlaylistId = watchedPlaylistQueryService.findByUserId(userId).getPlaylist().getId(); + movieInPlaylistDeleteService.deleteByPlaylistIdAndMovieId(watchedPlaylistId, movieId); + }); - public void deleteRecord(Long recordId) { - keywordDeleteService.deleteAllByRecordId(recordId); - recordDeleteDomainService.delete(recordId); } } diff --git a/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java b/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java index 525efdc..627f8b4 100644 --- a/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java +++ b/src/main/java/com/mookive/mookive_backend/record/presentation/RecordController.java @@ -27,9 +27,9 @@ public RecordResponse.RecordDetailResponse getRecord(@PathVariable Long recordId return recordGetService.getRecord(recordId); } - @DeleteMapping("/record/{recordId}") - public void deleteRecord(@PathVariable Long recordId) { - recordDeleteService.deleteRecord(recordId); + @DeleteMapping("/record") + public void deleteRecord(@RequestParam Long userId, @RequestParam Long movieId) { + recordDeleteService.deleteRecord(userId, movieId); } @GetMapping("/record/calendar") diff --git a/src/main/java/com/mookive/mookive_backend/watched/application/mapper/WatchedMapper.java b/src/main/java/com/mookive/mookive_backend/watched/application/mapper/WatchedMapper.java new file mode 100644 index 0000000..a06b0a6 --- /dev/null +++ b/src/main/java/com/mookive/mookive_backend/watched/application/mapper/WatchedMapper.java @@ -0,0 +1,15 @@ +package com.mookive.mookive_backend.watched.application.mapper; + +import com.mookive.mookive_backend.movie.domain.entity.Movie; +import com.mookive.mookive_backend.user.domain.entity.User; +import com.mookive.mookive_backend.watched.domain.entity.Watched; + +public class WatchedMapper { + + public static Watched mapToWatched(User user, Movie movie) { + return Watched.builder() + .user(user) + .movie(movie) + .build(); + } +} diff --git a/src/main/java/com/mookive/mookive_backend/watched/application/service/WatchedCreateService.java b/src/main/java/com/mookive/mookive_backend/watched/application/service/WatchedCreateService.java index fd568c8..83637f4 100644 --- a/src/main/java/com/mookive/mookive_backend/watched/application/service/WatchedCreateService.java +++ b/src/main/java/com/mookive/mookive_backend/watched/application/service/WatchedCreateService.java @@ -1,11 +1,14 @@ package com.mookive.mookive_backend.watched.application.service; +import com.mookive.mookive_backend.keyword.domain.service.KeywordDeleteService; 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.record.domain.service.RecordDeleteDomainService; +import com.mookive.mookive_backend.record.domain.service.RecordQueryService; 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; @@ -30,6 +33,9 @@ public class WatchedCreateService { private final MovieInPlaylistDeleteDomainService movieInPlaylistDeleteService; private final WatchedSaveService watchedSaveService; private final MovieInPlaylistSaveService movieInPlaylistSaveService; + private final RecordQueryService recordQueryService; + private final RecordDeleteDomainService recordDeleteDomainService; + private final KeywordDeleteService keywordDeleteService; public void createWatched(Long userId, Long movieId) { User user = userQueryService.findById(userId); @@ -39,6 +45,12 @@ public void createWatched(Long userId, Long movieId) { if(watchedQueryService.existsByUserIdAndMovieId(userId, movieId)) { watchedDeleteService.deleteByUserIdAndMovieId(userId, movieId); movieInPlaylistDeleteService.deleteByPlaylistIdAndMovieId(playlist.getId(), movieId); + recordQueryService.findByUserIdAndMovieId(userId, movieId) + .ifPresent(record -> { + keywordDeleteService.deleteAllByRecordId(record.getId()); + recordDeleteDomainService.delete(record.getId()); + }); + } else { Watched watched = Watched.builder() .user(user)