-
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
3d69cfc
commit 1efb158
Showing
3 changed files
with
51 additions
and
17 deletions.
There are no files selected for viewing
48 changes: 39 additions & 9 deletions
48
src/main/java/com/flickspick/recommendtype/application/RecommendTypeService.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,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; | ||
} | ||
} |
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