Skip to content

Commit bff4969

Browse files
committed
Merge branch 'release/3.5'
2 parents b673db6 + e89c43f commit bff4969

File tree

38 files changed

+1271
-741
lines changed

38 files changed

+1271
-741
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## Mac files
2+
.DS_Store
3+
14
## Drawio source files ##
25
*.drawio
36

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.siriusxi.ms.store.ps.api;
2+
3+
import com.siriusxi.ms.store.api.core.product.ProductEndpoint;
4+
import com.siriusxi.ms.store.api.core.product.ProductService;
5+
import com.siriusxi.ms.store.api.core.product.dto.Product;
6+
import lombok.extern.log4j.Log4j2;
7+
import org.springframework.beans.factory.annotation.Autowired;
8+
import org.springframework.beans.factory.annotation.Qualifier;
9+
import org.springframework.web.bind.annotation.RestController;
10+
11+
/**
12+
* Class <code>ProductController</code> is the implementation of the main Product Endpoint API
13+
* definition.
14+
*
15+
* @see ProductEndpoint
16+
* @author mohamed.taman
17+
* @version v1.0
18+
* @since v3.0 codename Storm
19+
*/
20+
@RestController
21+
@Log4j2
22+
public class ProductController implements ProductEndpoint {
23+
24+
/** Product service business logic interface. */
25+
private final ProductService prodService;
26+
27+
@Autowired
28+
public ProductController(@Qualifier("ProductServiceImpl") ProductService prodService) {
29+
this.prodService = prodService;
30+
}
31+
32+
/** {@inheritDoc} */
33+
@Override
34+
public Product getProduct(int id) {
35+
return prodService.getProduct(id);
36+
}
37+
38+
/** {@inheritDoc} */
39+
@Override
40+
public Product createProduct(Product body) {
41+
return prodService.createProduct(body);
42+
}
43+
44+
/** {@inheritDoc} */
45+
@Override
46+
public void deleteProduct(int id) {
47+
prodService.deleteProduct(id);
48+
}
49+
}

product-service/src/main/java/com/siriusxi/ms/store/ps/controller/ProductServiceImpl.java

-68
This file was deleted.

product-service/src/main/java/com/siriusxi/ms/store/ps/controller/ProductMapper.java renamed to product-service/src/main/java/com/siriusxi/ms/store/ps/service/ProductMapper.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
package com.siriusxi.ms.store.ps.controller;
1+
package com.siriusxi.ms.store.ps.service;
22

3-
import com.siriusxi.ms.store.api.core.product.Product;
3+
import com.siriusxi.ms.store.api.core.product.dto.Product;
44
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
55
import org.mapstruct.Mapper;
66
import org.mapstruct.Mapping;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
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+
}

product-service/src/main/resources/application.yaml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ spring:
66
host: localhost
77
port: 27017
88
database: product-db
9-
9+
auto-index-creation: true
10+
1011
server:
1112
port: 9081
1213

@@ -39,4 +40,4 @@ spring:
3940
host: mongodb
4041

4142
server:
42-
port: 8080
43+
port: 8080

product-service/src/test/java/com/siriusxi/ms/store/ps/MapperTests.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
package com.siriusxi.ms.store.ps;
22

3-
import com.siriusxi.ms.store.api.core.product.Product;
4-
import com.siriusxi.ms.store.ps.controller.ProductMapper;
3+
import com.siriusxi.ms.store.api.core.product.dto.Product;
4+
import com.siriusxi.ms.store.ps.service.ProductMapper;
55
import com.siriusxi.ms.store.ps.persistence.ProductEntity;
66
import org.junit.jupiter.api.Test;
77

88
import static org.junit.jupiter.api.Assertions.*;
99

1010
public class MapperTests {
1111

12-
private final ProductMapper mapper = ProductMapper.INSTANCE;
12+
private final ProductMapper mapper = ProductMapper.INSTANCE;
1313

1414
@Test
1515
public void mapperTests() {

product-service/src/test/java/com/siriusxi/ms/store/ps/PersistenceTests.java

-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.siriusxi.ms.store.ps.persistence.ProductRepository;
55
import org.junit.jupiter.api.Assertions;
66
import org.junit.jupiter.api.BeforeEach;
7-
import org.junit.jupiter.api.Disabled;
87
import org.junit.jupiter.api.Test;
98
import org.springframework.beans.factory.annotation.Autowired;
109
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
@@ -76,9 +75,7 @@ public void getByProductId() {
7675
assertEqualsProduct(savedEntity, entity.get());
7776
}
7877

79-
//FIXME error which is not thrown
8078
@Test
81-
@Disabled
8279
public void duplicateError() {
8380

8481
Assertions.assertThrows(

0 commit comments

Comments
 (0)