-
Notifications
You must be signed in to change notification settings - Fork 794
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding list beers, enhancing beerdto with inventory information. closes
- Loading branch information
1 parent
1e4050d
commit 025c654
Showing
12 changed files
with
236 additions
and
0 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
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
5 changes: 5 additions & 0 deletions
5
src/main/java/guru/springframework/msscbeerservice/services/BeerService.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
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
11 changes: 11 additions & 0 deletions
11
...in/java/guru/springframework/msscbeerservice/services/inventory/BeerInventoryService.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 |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package guru.springframework.msscbeerservice.services.inventory; | ||
|
||
import java.util.UUID; | ||
|
||
/** | ||
* Created by jt on 2019-06-07. | ||
*/ | ||
public interface BeerInventoryService { | ||
|
||
Integer getOnhandInventory(UUID beerId); | ||
} |
55 changes: 55 additions & 0 deletions
55
...ingframework/msscbeerservice/services/inventory/BeerInventoryServiceRestTemplateImpl.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package guru.springframework.msscbeerservice.services.inventory; | ||
|
||
import guru.springframework.msscbeerservice.services.inventory.model.BeerInventoryDto; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.web.client.RestTemplateBuilder; | ||
import org.springframework.core.ParameterizedTypeReference; | ||
import org.springframework.http.HttpMethod; | ||
import org.springframework.http.ResponseEntity; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Created by jt on 2019-06-07. | ||
*/ | ||
@Slf4j | ||
@ConfigurationProperties(prefix = "sfg.brewery", ignoreUnknownFields = false) | ||
@Component | ||
public class BeerInventoryServiceRestTemplateImpl implements BeerInventoryService { | ||
|
||
private final String INVENTORY_PATH = "/api/v1/beer/{beerId}/inventory"; | ||
private final RestTemplate restTemplate; | ||
|
||
private String beerInventoryServiceHost; | ||
|
||
public void setBeerInventoryServiceHost(String beerInventoryServiceHost) { | ||
this.beerInventoryServiceHost = beerInventoryServiceHost; | ||
} | ||
|
||
public BeerInventoryServiceRestTemplateImpl(RestTemplateBuilder restTemplateBuilder) { | ||
this.restTemplate = restTemplateBuilder.build(); | ||
} | ||
|
||
@Override | ||
public Integer getOnhandInventory(UUID beerId) { | ||
|
||
log.debug("Calling Inventory Service"); | ||
|
||
ResponseEntity<List<BeerInventoryDto>> responseEntity = restTemplate | ||
.exchange(beerInventoryServiceHost + INVENTORY_PATH, HttpMethod.GET, null, | ||
new ParameterizedTypeReference<List<BeerInventoryDto>>(){}, (Object) beerId); | ||
|
||
//sum from inventory list | ||
Integer onHand = Objects.requireNonNull(responseEntity.getBody()) | ||
.stream() | ||
.mapToInt(BeerInventoryDto::getQuantityOnHand) | ||
.sum(); | ||
|
||
return onHand; | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
.../java/guru/springframework/msscbeerservice/services/inventory/model/BeerInventoryDto.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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package guru.springframework.msscbeerservice.services.inventory.model; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.NoArgsConstructor; | ||
|
||
import java.time.OffsetDateTime; | ||
import java.util.UUID; | ||
|
||
/** | ||
* Created by jt on 2019-06-07. | ||
*/ | ||
@Data | ||
@NoArgsConstructor | ||
@AllArgsConstructor | ||
@Builder | ||
public class BeerInventoryDto { | ||
private UUID id; | ||
private OffsetDateTime createdDate; | ||
private OffsetDateTime lastModifiedDate; | ||
private UUID beerId; | ||
private Integer quantityOnHand; | ||
} |
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
36 changes: 36 additions & 0 deletions
36
src/main/java/guru/springframework/msscbeerservice/web/mappers/BeerMapperDecorator.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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package guru.springframework.msscbeerservice.web.mappers; | ||
|
||
import guru.springframework.msscbeerservice.domain.Beer; | ||
import guru.springframework.msscbeerservice.services.inventory.BeerInventoryService; | ||
import guru.springframework.msscbeerservice.web.model.BeerDto; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
/** | ||
* Created by jt on 2019-06-08. | ||
*/ | ||
public abstract class BeerMapperDecorator implements BeerMapper { | ||
private BeerInventoryService beerInventoryService; | ||
private BeerMapper mapper; | ||
|
||
@Autowired | ||
public void setBeerInventoryService(BeerInventoryService beerInventoryService) { | ||
this.beerInventoryService = beerInventoryService; | ||
} | ||
|
||
@Autowired | ||
public void setMapper(BeerMapper mapper) { | ||
this.mapper = mapper; | ||
} | ||
|
||
@Override | ||
public BeerDto beerToBeerDto(Beer beer) { | ||
BeerDto dto = mapper.beerToBeerDto(beer); | ||
dto.setQuantityOnHand(beerInventoryService.getOnhandInventory(beer.getId())); | ||
return dto; | ||
} | ||
|
||
@Override | ||
public Beer beerDtoToBeer(BeerDto beerDto) { | ||
return mapper.beerDtoToBeer(beerDto); | ||
} | ||
} |
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 +1,2 @@ | ||
sfg.brewery.beer-inventory-service-host=http://localhost:8082 | ||
spring.datasource.initialization-mode=EMBEDDED |
29 changes: 29 additions & 0 deletions
29
...ramework/msscbeerservice/services/inventory/BeerInventoryServiceRestTemplateImplTest.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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package guru.springframework.msscbeerservice.services.inventory; | ||
|
||
import guru.springframework.msscbeerservice.bootstrap.BeerLoader; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Disabled; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
|
||
@Disabled // utility for manual testing | ||
@SpringBootTest | ||
class BeerInventoryServiceRestTemplateImplTest { | ||
|
||
@Autowired | ||
BeerInventoryService beerInventoryService; | ||
|
||
@BeforeEach | ||
void setUp() { | ||
|
||
} | ||
|
||
@Test | ||
void getOnhandInventory() { | ||
Integer qoh = beerInventoryService.getOnhandInventory(BeerLoader.BEER_1_UUID); | ||
|
||
System.out.println(qoh); | ||
|
||
} | ||
} |