Skip to content

Commit

Permalink
Merge pull request #41 from syyling/feat/36
Browse files Browse the repository at this point in the history
[feat] 장르별 보관함 목록 조회 기능 추가
  • Loading branch information
syyling authored May 28, 2024
2 parents c0e8cb7 + a509fc1 commit 1739e87
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import com.mookive.mookive_backend.movie.domain.entity.Movie;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface MovieRepository extends JpaRepository<Movie, Long> {

Movie findByTitle(String title);

Movie findByTmdbId(String tmdbId);

Optional<Movie> findTop1ByGenreStartsWithOrderByYearDescCreatedAtDesc(String genre);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Optional;

@Service
@RequiredArgsConstructor
@Transactional
Expand All @@ -25,4 +27,8 @@ public Movie findById(Long id) {
public Movie findByTmdbId(String tmdbId) {
return movieRepository.findByTmdbId(tmdbId);
}

public Optional<Movie> findTop1ByGenreStartsWithOrderByYearDescCreatedAtDesc(String genre) {
return movieRepository.findTop1ByGenreStartsWithOrderByYearDescCreatedAtDesc(genre);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,31 @@ public WatchedCheckResponse(boolean isWatched) {

@Getter
@NoArgsConstructor
public static class WatchedGenreResponse {
public static class WatchedGenreMovieResponse {
private String poster;
private String title;
private String rating;
private String tmdbId;

@Builder
public WatchedGenreResponse(String poster, String title, String rating, String tmdbId) {
public WatchedGenreMovieResponse(String poster, String title, String rating, String tmdbId) {
this.poster = poster;
this.title = title;
this.rating = rating;
this.tmdbId = tmdbId;
}
}

@Getter
@NoArgsConstructor
public static class WatchedGenreListResponse {
private String genre;
private String poster;

@Builder
public WatchedGenreListResponse(String genre, String poster) {
this.genre = genre;
this.poster = poster;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,20 @@ public static WatchedResponse.WatchedCheckResponse mapToWatchedCheckResponse(boo
.isWatched(isWatched)
.build();
}

public static WatchedResponse.WatchedGenreMovieResponse mapToWatchedGenreMovieResponse(Movie movie, String rating) {
return WatchedResponse.WatchedGenreMovieResponse.builder()
.tmdbId(movie.getTmdbId())
.title(movie.getTitle())
.poster(movie.getPoster())
.rating(rating)
.build();
}

public static WatchedResponse.WatchedGenreListResponse mapToWatchedGenreListResponse(String genre, String poster) {
return WatchedResponse.WatchedGenreListResponse.builder()
.genre(genre)
.poster(poster)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.mookive.mookive_backend.record.domain.entity.Record;
import com.mookive.mookive_backend.record.domain.service.RecordQueryService;
import com.mookive.mookive_backend.watched.application.dto.response.WatchedResponse;
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 jakarta.transaction.Transactional;
Expand All @@ -25,9 +26,12 @@ public class WatchedGenreService {
private final MovieQueryService movieQueryService;
private final RecordQueryService recordQueryService;

public List<WatchedResponse.WatchedGenreResponse> getWatchedGenre(Long userId, String genre) {
private static final int NUMBER_OF_GENRE = 9;
private static final String[] GENRE_LIST = {"액션", "드라마", "로맨스", "애니메이션", "SF", "미스터리", "범죄", "공포", "판타지", "코미디"};

public List<WatchedResponse.WatchedGenreMovieResponse> getWatchedGenreMovie(Long userId, String genre) {
List<Watched> watchedList = watchedQueryService.findByUserId(userId);
List<WatchedResponse.WatchedGenreResponse> genreResponseList = new ArrayList<>();
List<WatchedResponse.WatchedGenreMovieResponse> genreResponseList = new ArrayList<>();
for(Watched watched : watchedList) {
Movie movie = movieQueryService.findById(watched.getMovie().getId());
if(movie.getGenre().contains(genre)) {
Expand All @@ -36,15 +40,22 @@ public List<WatchedResponse.WatchedGenreResponse> getWatchedGenre(Long userId, S
if(record.isPresent()) {
rating = record.get().getRating();
}
WatchedResponse.WatchedGenreResponse watchedGenreResponse = WatchedResponse.WatchedGenreResponse.builder()
.tmdbId(movie.getTmdbId())
.title(movie.getTitle())
.poster(movie.getPoster())
.rating(rating)
.build();
genreResponseList.add(watchedGenreResponse);
WatchedResponse.WatchedGenreMovieResponse watchedGenreMovieResponse = WatchedMapper.mapToWatchedGenreMovieResponse(movie, rating);;
genreResponseList.add(watchedGenreMovieResponse);
}
}
return genreResponseList;
}

public List<WatchedResponse.WatchedGenreListResponse> getWatchedGenreList(Long userId) {
List<WatchedResponse.WatchedGenreListResponse> genreList = new ArrayList<>(NUMBER_OF_GENRE);
for(String genre : GENRE_LIST) {
String poster = null;
Optional<Movie> movie = movieQueryService.findTop1ByGenreStartsWithOrderByYearDescCreatedAtDesc(genre);
if(movie.isPresent()) poster = movie.get().getPoster();
WatchedResponse.WatchedGenreListResponse watchedGenreListResponse = WatchedMapper.mapToWatchedGenreListResponse(genre, poster);
genreList.add(watchedGenreListResponse);
}
return genreList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,16 @@ public WatchedResponse.WatchedCheckResponse checkHeart(@RequestParam Long userId
return watchedGetService.checkWatched(userId, tmdbId);
}

@GetMapping("/watched/genre")
public List<WatchedResponse.WatchedGenreResponse> getWatchedGenre(@RequestParam Long userId, @RequestParam String genre) {
return watchedGenreService.getWatchedGenre(userId, genre);
/**
* Todo: 장르 도메인 따로 설정하기
*/
@GetMapping("/watched/genre/movie")
public List<WatchedResponse.WatchedGenreMovieResponse> getWatchedGenreMovie(@RequestParam Long userId, @RequestParam String genre) {
return watchedGenreService.getWatchedGenreMovie(userId, genre);
}

@GetMapping("/watched/genre/list")
public List<WatchedResponse.WatchedGenreListResponse> getWatchedGenreList(@RequestParam Long userId) {
return watchedGenreService.getWatchedGenreList(userId);
}
}

0 comments on commit 1739e87

Please sign in to comment.