-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dbb8dbd
commit 0ffc726
Showing
4 changed files
with
65 additions
and
34 deletions.
There are no files selected for viewing
68 changes: 38 additions & 30 deletions
68
src/main/java/com/flickspick/movie/application/MovieService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,51 @@ | ||
package com.flickspick.movie.application; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.springframework.stereotype.Service; | ||
|
||
import com.flickspick.exception.dto.ErrorType; | ||
import com.flickspick.exception.movie.MovieNotFoundException; | ||
import com.flickspick.movie.domain.Movie; | ||
import com.flickspick.movie.infrastructure.MovieRepository; | ||
import com.flickspick.movie.model.MovieModel; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.springframework.scheduling.annotation.Async; | ||
import org.springframework.stereotype.Service; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.stream.Collectors; | ||
|
||
@Service | ||
@RequiredArgsConstructor | ||
public class MovieService { | ||
private final MovieRepository movieRepository; | ||
|
||
public MovieModel getMovieModel(Long movieId) { | ||
Movie movie = movieRepository.findById(movieId) | ||
.orElseThrow(() -> new MovieNotFoundException(ErrorType.MOVIE_NOT_FOUND_ERROR)); | ||
return MovieModel.toModel(movie); | ||
} | ||
|
||
public List<MovieModel> getMovieModelList(Long movieId, int count) { | ||
List<Movie> movieList = movieRepository.findAll() | ||
.stream().filter(movie -> movie.getId() != movieId) | ||
.collect(Collectors.toList()); | ||
Collections.shuffle(movieList); | ||
|
||
int limit = Math.min(movieList.size(), count); | ||
return movieList.subList(0, limit).stream().map( | ||
movie -> MovieModel.toModel(movie) | ||
).collect(Collectors.toList()); | ||
} | ||
|
||
public Long getMovieCount() { | ||
return movieRepository.count(); | ||
} | ||
private final MovieRepository movieRepository; | ||
|
||
public MovieModel getMovieModel(Long movieId) { | ||
Movie movie = movieRepository.findById(movieId) | ||
.orElseThrow(() -> new MovieNotFoundException(ErrorType.MOVIE_NOT_FOUND_ERROR)); | ||
return MovieModel.toModel(movie); | ||
} | ||
|
||
public List<MovieModel> getMovieModelList(Long movieId, int count) { | ||
List<Movie> movieList = movieRepository.findAll() | ||
.stream().filter(movie -> movie.getId() != movieId) | ||
.collect(Collectors.toList()); | ||
|
||
Collections.shuffle(movieList); | ||
|
||
int limit = Math.min(movieList.size(), count); | ||
|
||
return movieList.subList(0, limit) | ||
.stream() | ||
.map(MovieModel::toModel) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
@Async(value = "taskExecutor") | ||
public CompletableFuture<List<MovieModel>> asyncGetMovieModelList(Long movieId, int count) { | ||
return CompletableFuture.completedFuture(getMovieModelList(movieId, count)); | ||
} | ||
|
||
public Long getMovieCount() { | ||
return movieRepository.count(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters