Skip to content

Commit

Permalink
imp: recommend type service
Browse files Browse the repository at this point in the history
  • Loading branch information
DongGeon0908 committed Sep 23, 2023
1 parent 3d69cfc commit 1efb158
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,23 +1,53 @@
package com.flickspick.recommendtype.application;

import org.springframework.stereotype.Service;

import com.flickspick.exception.dto.ErrorType;
import com.flickspick.exception.rec.RecommendTypeNotFoundException;
import com.flickspick.recommendtype.domain.RecommendType;
import com.flickspick.recommendtype.infrastructure.RecommendTypeRepository;
import com.flickspick.recommendtype.model.RecTypeModel;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
@RequiredArgsConstructor
public class RecommendTypeService {
private final RecommendTypeRepository recommendTypeRepository;
private final RecommendTypeRepository recommendTypeRepository;
private Map<Long, RecTypeModel> recTypeModels;

@Scheduled(fixedRate = 1000 * 60 * 5, initialDelayString = "0")
public void refreshRecTypeModels() {
log.info("refresh recTypeModels info start");
recTypeModels = refresh();
log.info("refresh recTypeModels info complete");
}

public RecTypeModel getRecTypeModel(Long recommendTypeId) {
RecommendType recommendType = recommendTypeRepository.findById(recommendTypeId)
.orElseThrow(() -> new RecommendTypeNotFoundException(ErrorType.RECOMMEND_TYPE_NOT_FOUND_ERROR));
return RecTypeModel.toModel(recommendType);
}

public Map<Long, RecTypeModel> refresh() {
return recommendTypeRepository.findAll()
.stream()
.map(RecTypeModel::toModel)
.collect(Collectors.toMap(RecTypeModel::getId, Function.identity()));
}

public RecTypeModel get(Long id) {
var rec = recTypeModels.get(id);

if (rec == null) {
throw new RecommendTypeNotFoundException(ErrorType.RECOMMEND_TYPE_NOT_FOUND_ERROR);
}

public RecTypeModel getRecTypeModel(Long recommendTypeId) {
RecommendType recommendType = recommendTypeRepository.findById(recommendTypeId)
.orElseThrow(() -> new RecommendTypeNotFoundException(ErrorType.RECOMMEND_TYPE_NOT_FOUND_ERROR));
return RecTypeModel.toModel(recommendType);
}
return rec;
}
}
12 changes: 4 additions & 8 deletions src/main/java/com/flickspick/share/application/ShareService.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package com.flickspick.share.application;

import com.flickspick.auth.model.AuthUser;
import com.flickspick.exception.dto.ErrorType;
import com.flickspick.exception.user.UserNotFoundException;
import com.flickspick.movie.model.MovieModel;
import com.flickspick.recommendtype.model.RecTypeModel;
import com.flickspick.share.dto.response.ShareResponse;
import com.flickspick.user.infrastructure.UserRepository;
import com.flickspick.user.model.UserModel;
import com.flickspick.user.application.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

Expand All @@ -16,14 +13,13 @@
@Service
@RequiredArgsConstructor
public class ShareService {
private final UserRepository userRepository;
private final UserService userService;

public ShareResponse share(AuthUser authUser) {
var user = userRepository.findById(authUser.getId())
.orElseThrow(() -> new UserNotFoundException(ErrorType.USER_NOT_FOUND_ERROR));
var user = userService.getUserModel(authUser.getId());

return new ShareResponse(
UserModel.from(user),
user,
List.of(
new RecTypeModel(
1L,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.flickspick.user.dto.response.UserResponse;
import com.flickspick.user.dto.response.UserSignResponse;
import com.flickspick.user.infrastructure.UserRepository;
import com.flickspick.user.model.UserModel;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
Expand Down Expand Up @@ -49,4 +50,11 @@ public UserResponse get(AuthUser authUser, Long id) {

return UserResponse.from(user);
}

public UserModel getUserModel(Long id) {
var user = userRepository.findById(id)
.orElseThrow(() -> new UserNotFoundException(ErrorType.USER_NOT_FOUND_ERROR));

return UserModel.from(user);
}
}

0 comments on commit 1efb158

Please sign in to comment.