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

[feat] 리뷰 생성, 삭제에 따라 봤어요 연동 기능 추가 #29

Merged
merged 1 commit into from
May 20, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@
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;
import com.mookive.mookive_backend.record.domain.service.RecordQueryService;
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;
Expand All @@ -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<Record> 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();
Expand All @@ -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<String> keywords = recordCreateRequest.getKeywords();
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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);
Expand All @@ -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)
Expand Down
Loading