|
| 1 | +package com.siriusxi.ms.store.rs.service; |
| 2 | + |
| 3 | +import com.mongodb.DuplicateKeyException; |
| 4 | + |
| 5 | +import com.siriusxi.ms.store.api.core.recommendation.RecommendationService; |
| 6 | +import com.siriusxi.ms.store.api.core.recommendation.dto.Recommendation; |
| 7 | +import com.siriusxi.ms.store.rs.persistence.RecommendationEntity; |
| 8 | +import com.siriusxi.ms.store.rs.persistence.RecommendationRepository; |
| 9 | +import com.siriusxi.ms.store.util.exceptions.InvalidInputException; |
| 10 | +import com.siriusxi.ms.store.util.http.ServiceUtil; |
| 11 | +import lombok.extern.log4j.Log4j2; |
| 12 | +import org.springframework.beans.factory.annotation.Autowired; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +import java.util.List; |
| 16 | + |
| 17 | +@Service("RecommendationServiceImpl") |
| 18 | +@Log4j2 |
| 19 | +public class RecommendationServiceImpl implements RecommendationService { |
| 20 | + |
| 21 | + private final RecommendationRepository repository; |
| 22 | + |
| 23 | + private final RecommendationMapper mapper; |
| 24 | + |
| 25 | + private final ServiceUtil serviceUtil; |
| 26 | + |
| 27 | + @Autowired |
| 28 | + public RecommendationServiceImpl( |
| 29 | + RecommendationRepository repository, RecommendationMapper mapper, ServiceUtil serviceUtil) { |
| 30 | + this.repository = repository; |
| 31 | + this.mapper = mapper; |
| 32 | + this.serviceUtil = serviceUtil; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public Recommendation createRecommendation(Recommendation body) { |
| 37 | + try { |
| 38 | + RecommendationEntity entity = mapper.apiToEntity(body); |
| 39 | + RecommendationEntity newEntity = repository.save(entity); |
| 40 | + |
| 41 | + log.debug( |
| 42 | + "createRecommendation: created a recommendation entity: {}/{}", |
| 43 | + body.getProductId(), |
| 44 | + body.getRecommendationId()); |
| 45 | + |
| 46 | + return mapper.entityToApi(newEntity); |
| 47 | + |
| 48 | + } catch (DuplicateKeyException dke) { |
| 49 | + throw new InvalidInputException( |
| 50 | + "Duplicate key, Product Id: " |
| 51 | + + body.getProductId() |
| 52 | + + ", Recommendation Id:" |
| 53 | + + body.getRecommendationId()); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public List<Recommendation> getRecommendations(int productId) { |
| 59 | + |
| 60 | + if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId); |
| 61 | + |
| 62 | + List<RecommendationEntity> entityList = repository.findByProductId(productId); |
| 63 | + List<Recommendation> list = mapper.entityListToApiList(entityList); |
| 64 | + list.forEach(e -> e.setServiceAddress(serviceUtil.getServiceAddress())); |
| 65 | + |
| 66 | + log.debug("getRecommendations: response size: {}", list.size()); |
| 67 | + |
| 68 | + return list; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void deleteRecommendations(int productId) { |
| 73 | + log.debug( |
| 74 | + "deleteRecommendations: tries to delete recommendations for the product with " |
| 75 | + + "productId: {}", |
| 76 | + productId); |
| 77 | + repository.deleteAll(repository.findByProductId(productId)); |
| 78 | + } |
| 79 | +} |
0 commit comments