|
| 1 | +package com.siriusxi.ms.store.ps.service; |
| 2 | + |
| 3 | +import com.siriusxi.ms.store.api.core.product.ProductService; |
| 4 | +import com.siriusxi.ms.store.api.core.product.dto.Product; |
| 5 | +import com.siriusxi.ms.store.ps.persistence.ProductEntity; |
| 6 | +import com.siriusxi.ms.store.ps.persistence.ProductRepository; |
| 7 | +import com.siriusxi.ms.store.util.exceptions.InvalidInputException; |
| 8 | +import com.siriusxi.ms.store.util.exceptions.NotFoundException; |
| 9 | +import com.siriusxi.ms.store.util.http.ServiceUtil; |
| 10 | +import lombok.extern.log4j.Log4j2; |
| 11 | +import org.springframework.beans.factory.annotation.Autowired; |
| 12 | +import org.springframework.dao.DuplicateKeyException; |
| 13 | +import org.springframework.stereotype.Service; |
| 14 | + |
| 15 | +@Service("ProductServiceImpl") |
| 16 | +@Log4j2 |
| 17 | +public class ProductServiceImpl implements ProductService { |
| 18 | + |
| 19 | + private final ServiceUtil serviceUtil; |
| 20 | + |
| 21 | + private final ProductRepository repository; |
| 22 | + |
| 23 | + private final ProductMapper mapper; |
| 24 | + |
| 25 | + @Autowired |
| 26 | + public ProductServiceImpl( |
| 27 | + ProductRepository repository, ProductMapper mapper, ServiceUtil serviceUtil) { |
| 28 | + this.repository = repository; |
| 29 | + this.mapper = mapper; |
| 30 | + this.serviceUtil = serviceUtil; |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public Product createProduct(Product body) { |
| 35 | + try { |
| 36 | + ProductEntity entity = mapper.apiToEntity(body); |
| 37 | + ProductEntity newEntity = repository.save(entity); |
| 38 | + |
| 39 | + log.debug("createProduct: entity created for productId: {}", body.getProductId()); |
| 40 | + return mapper.entityToApi(newEntity); |
| 41 | + |
| 42 | + } catch (DuplicateKeyException dke) { |
| 43 | + throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId()); |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + @Override |
| 48 | + public Product getProduct(int productId) { |
| 49 | + if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId); |
| 50 | + |
| 51 | + ProductEntity entity = |
| 52 | + repository |
| 53 | + .findByProductId(productId) |
| 54 | + .orElseThrow( |
| 55 | + () -> new NotFoundException("No product found for productId: " + productId)); |
| 56 | + |
| 57 | + Product response = mapper.entityToApi(entity); |
| 58 | + response.setServiceAddress(serviceUtil.getServiceAddress()); |
| 59 | + |
| 60 | + log.debug("getProduct: found productId: {}", response.getProductId()); |
| 61 | + |
| 62 | + return response; |
| 63 | + } |
| 64 | + |
| 65 | + /* |
| 66 | + Implementation is idempotent, that is, |
| 67 | + it will not report any failure if the entity is not found Always 200 |
| 68 | + */ |
| 69 | + @Override |
| 70 | + public void deleteProduct(int productId) { |
| 71 | + log.debug("deleteProduct: tries to delete an entity with productId: {}", productId); |
| 72 | + repository.findByProductId(productId).ifPresent(repository::delete); |
| 73 | + } |
| 74 | +} |
0 commit comments