Skip to content

Commit

Permalink
imp: completable future
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 23, 2023
1 parent dbb8dbd commit 0ffc726
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
68 changes: 38 additions & 30 deletions src/main/java/com/flickspick/movie/application/MovieService.java
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();
}
}
15 changes: 11 additions & 4 deletions src/main/java/com/flickspick/share/application/ShareService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
import com.flickspick.share.dto.response.ShareResponse;
import com.flickspick.user.application.UserService;
import com.flickspick.user_movie_history.application.UserMovieHistoryService;
import com.flickspick.user_movie_history.domain.UserMovieHistory;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@Service
@RequiredArgsConstructor
Expand All @@ -22,17 +23,23 @@ public class ShareService {
private final RecommendTypeService recommendTypeService;
private final MovieService movieService;

@SneakyThrows
public ShareResponse share(AuthUser authUser) {
var user = userService.getUserModel(authUser.getId());
UserMovieHistory userMovieHistory = userMovieHistoryService.getRecentHistory(user.getId());
var userCf = userService.asyncGetUserModel(authUser.getId());
var userMovieHistoryCf = userMovieHistoryService.asyncGetRecentHistory(authUser.getId());

CompletableFuture.allOf(userCf, userMovieHistoryCf).join();

var userMovieHistory = userMovieHistoryCf.get();

RecTypeModel recTypeModel = recommendTypeService.getRecTypeModel(userMovieHistory.getRecommendTypeId());

List<MovieModel> similarMovies = movieService.getMovieModelList(userMovieHistory.getMovieId(), 2);
MovieModel movieModel = movieService.getMovieModel(userMovieHistory.getMovieId());
similarMovies.add(movieModel);

return new ShareResponse(
user,
userCf.get(),
List.of(recTypeModel),
recTypeModel.getTags(),
similarMovies
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
import com.flickspick.user.infrastructure.UserRepository;
import com.flickspick.user.model.UserModel;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.CompletableFuture;

@Service
@RequiredArgsConstructor
public class UserService {
Expand Down Expand Up @@ -57,4 +60,9 @@ public UserModel getUserModel(Long id) {

return UserModel.from(user);
}

@Async(value = "taskExecutor")
public CompletableFuture<UserModel> asyncGetUserModel(Long id) {
return CompletableFuture.completedFuture(getUserModel(id));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
import com.flickspick.user_movie_history.domain.UserMovieHistory;
import com.flickspick.user_movie_history.infrastructure.UserMovieHistoryRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.CompletableFuture;

@Service
@RequiredArgsConstructor
public class UserMovieHistoryService {
Expand All @@ -28,4 +31,9 @@ public UserMovieHistory getRecentHistory(Long userId) {

return userMovieHistory.get();
}

@Async
public CompletableFuture<UserMovieHistory> asyncGetRecentHistory(Long userId) {
return CompletableFuture.completedFuture(getRecentHistory(userId));
}
}

0 comments on commit 0ffc726

Please sign in to comment.