-
Notifications
You must be signed in to change notification settings - Fork 179
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
766c50d
commit e89c43f
Showing
5 changed files
with
332 additions
and
291 deletions.
There are no files selected for viewing
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,3 +1,6 @@ | ||
## Mac files | ||
.DS_Store | ||
|
||
## Drawio source files ## | ||
*.drawio | ||
|
||
|
86 changes: 44 additions & 42 deletions
86
product-service/src/main/java/com/siriusxi/ms/store/ps/service/ProductServiceImpl.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,72 +1,74 @@ | ||
package com.siriusxi.ms.store.ps.service; | ||
|
||
import com.mongodb.DuplicateKeyException; | ||
import com.siriusxi.ms.store.api.core.product.dto.Product; | ||
import com.siriusxi.ms.store.api.core.product.ProductService; | ||
import com.siriusxi.ms.store.api.core.product.dto.Product; | ||
import com.siriusxi.ms.store.ps.persistence.ProductEntity; | ||
import com.siriusxi.ms.store.ps.persistence.ProductRepository; | ||
import com.siriusxi.ms.store.util.exceptions.InvalidInputException; | ||
import com.siriusxi.ms.store.util.exceptions.NotFoundException; | ||
import com.siriusxi.ms.store.util.http.ServiceUtil; | ||
import lombok.extern.log4j.Log4j2; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.dao.DuplicateKeyException; | ||
import org.springframework.stereotype.Service; | ||
|
||
@Service("ProductServiceImpl") | ||
@Log4j2 | ||
public class ProductServiceImpl implements ProductService { | ||
|
||
private final ServiceUtil serviceUtil; | ||
private final ServiceUtil serviceUtil; | ||
|
||
private final ProductRepository repository; | ||
private final ProductRepository repository; | ||
|
||
private final ProductMapper mapper; | ||
private final ProductMapper mapper; | ||
|
||
@Autowired | ||
public ProductServiceImpl(ProductRepository repository, | ||
ProductMapper mapper, | ||
ServiceUtil serviceUtil) { | ||
this.repository = repository; | ||
this.mapper = mapper; | ||
this.serviceUtil = serviceUtil; | ||
} | ||
@Autowired | ||
public ProductServiceImpl( | ||
ProductRepository repository, ProductMapper mapper, ServiceUtil serviceUtil) { | ||
this.repository = repository; | ||
this.mapper = mapper; | ||
this.serviceUtil = serviceUtil; | ||
} | ||
|
||
@Override | ||
public Product createProduct(Product body) { | ||
try { | ||
ProductEntity entity = mapper.apiToEntity(body); | ||
ProductEntity newEntity = repository.save(entity); | ||
@Override | ||
public Product createProduct(Product body) { | ||
try { | ||
ProductEntity entity = mapper.apiToEntity(body); | ||
ProductEntity newEntity = repository.save(entity); | ||
|
||
log.debug("createProduct: entity created for productId: {}", body.getProductId()); | ||
return mapper.entityToApi(newEntity); | ||
log.debug("createProduct: entity created for productId: {}", body.getProductId()); | ||
return mapper.entityToApi(newEntity); | ||
|
||
} catch (DuplicateKeyException dke) { | ||
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId()); | ||
} | ||
} catch (DuplicateKeyException dke) { | ||
throw new InvalidInputException("Duplicate key, Product Id: " + body.getProductId()); | ||
} | ||
} | ||
|
||
@Override | ||
public Product getProduct(int productId) { | ||
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId); | ||
@Override | ||
public Product getProduct(int productId) { | ||
if (productId < 1) throw new InvalidInputException("Invalid productId: " + productId); | ||
|
||
ProductEntity entity = repository.findByProductId(productId) | ||
.orElseThrow(() -> new NotFoundException("No product found for productId: " + productId)); | ||
ProductEntity entity = | ||
repository | ||
.findByProductId(productId) | ||
.orElseThrow( | ||
() -> new NotFoundException("No product found for productId: " + productId)); | ||
|
||
Product response = mapper.entityToApi(entity); | ||
response.setServiceAddress(serviceUtil.getServiceAddress()); | ||
Product response = mapper.entityToApi(entity); | ||
response.setServiceAddress(serviceUtil.getServiceAddress()); | ||
|
||
log.debug("getProduct: found productId: {}", response.getProductId()); | ||
log.debug("getProduct: found productId: {}", response.getProductId()); | ||
|
||
return response; | ||
} | ||
return response; | ||
} | ||
|
||
/* | ||
Implementation is idempotent, that is, | ||
it will not report any failure if the entity is not found Always 200 | ||
*/ | ||
@Override | ||
public void deleteProduct(int productId) { | ||
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId); | ||
repository.findByProductId(productId).ifPresent(repository::delete); | ||
} | ||
/* | ||
Implementation is idempotent, that is, | ||
it will not report any failure if the entity is not found Always 200 | ||
*/ | ||
@Override | ||
public void deleteProduct(int productId) { | ||
log.debug("deleteProduct: tries to delete an entity with productId: {}", productId); | ||
repository.findByProductId(productId).ifPresent(repository::delete); | ||
} | ||
} |
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
Oops, something went wrong.