Skip to content

Commit

Permalink
Adding list beers, enhancing beerdto with inventory information. closes
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Jun 8, 2019
1 parent 1e4050d commit 025c654
Show file tree
Hide file tree
Showing 12 changed files with 236 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;
import org.hibernate.annotations.UpdateTimestamp;

import javax.persistence.*;
Expand All @@ -24,6 +25,7 @@ public class Beer {
@Id
@GeneratedValue(generator = "UUID")
@GenericGenerator(name = "UUID", strategy = "org.hibernate.id.UUIDGenerator")
@Type(type="org.hibernate.type.UUIDCharType")
@Column(length = 36, columnDefinition = "varchar", updatable = false, nullable = false)
private UUID id;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package guru.springframework.msscbeerservice.repositories;

import guru.springframework.msscbeerservice.domain.Beer;
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.PagingAndSortingRepository;

import java.util.UUID;
Expand All @@ -9,4 +12,9 @@
* Created by jt on 2019-05-17.
*/
public interface BeerRepository extends PagingAndSortingRepository<Beer, UUID> {
Page<Beer> findAllByBeerName(String beerName, Pageable pageable);

Page<Beer> findAllByBeerStyle(BeerStyleEnum beerStyle, Pageable pageable);

Page<Beer> findAllByBeerNameAndBeerStyle(String beerName, BeerStyleEnum beerStyle, Pageable pageable);
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package guru.springframework.msscbeerservice.services;

import guru.springframework.msscbeerservice.web.model.BeerDto;
import guru.springframework.msscbeerservice.web.model.BeerPagedList;
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import org.springframework.data.domain.PageRequest;

import java.util.UUID;

/**
* Created by jt on 2019-06-06.
*/
public interface BeerService {
BeerPagedList listBeers(String beerName, BeerStyleEnum beerStyle, PageRequest pageRequest);

BeerDto getById(UUID beerId);

BeerDto saveNewBeer(BeerDto beerDto);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
import guru.springframework.msscbeerservice.web.controller.NotFoundException;
import guru.springframework.msscbeerservice.web.mappers.BeerMapper;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import guru.springframework.msscbeerservice.web.model.BeerPagedList;
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.UUID;
import java.util.stream.Collectors;

/**
* Created by jt on 2019-06-06.
Expand All @@ -19,6 +25,38 @@ public class BeerServiceImpl implements BeerService {
private final BeerRepository beerRepository;
private final BeerMapper beerMapper;

@Override
public BeerPagedList listBeers(String beerName, BeerStyleEnum beerStyle, PageRequest pageRequest) {

BeerPagedList beerPagedList;
Page<Beer> beerPage;

if (!StringUtils.isEmpty(beerName) && !StringUtils.isEmpty(beerStyle)) {
//search both
beerPage = beerRepository.findAllByBeerNameAndBeerStyle(beerName, beerStyle, pageRequest);
} else if (!StringUtils.isEmpty(beerName) && StringUtils.isEmpty(beerStyle)) {
//search beer_service name
beerPage = beerRepository.findAllByBeerName(beerName, pageRequest);
} else if (StringUtils.isEmpty(beerName) && !StringUtils.isEmpty(beerStyle)) {
//search beer_service style
beerPage = beerRepository.findAllByBeerStyle(beerStyle, pageRequest);
} else {
beerPage = beerRepository.findAll(pageRequest);
}

beerPagedList = new BeerPagedList(beerPage
.getContent()
.stream()
.map(beerMapper::beerToBeerDto)
.collect(Collectors.toList()),
PageRequest
.of(beerPage.getPageable().getPageNumber(),
beerPage.getPageable().getPageSize()),
beerPage.getTotalElements());

return beerPagedList;
}

@Override
public BeerDto getById(UUID beerId) {
return beerMapper.beerToBeerDto(
Expand Down
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);
}
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;
}
}
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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@

import guru.springframework.msscbeerservice.services.BeerService;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import guru.springframework.msscbeerservice.web.model.BeerPagedList;
import guru.springframework.msscbeerservice.web.model.BeerStyleEnum;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.PageRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -18,8 +21,30 @@
@RestController
public class BeerController {

private static final Integer DEFAULT_PAGE_NUMBER = 0;
private static final Integer DEFAULT_PAGE_SIZE = 25;

private final BeerService beerService;

@GetMapping(produces = { "application/json" })
public ResponseEntity<BeerPagedList> listBeers(@RequestParam(value = "pageNumber", required = false) Integer pageNumber,
@RequestParam(value = "pageSize", required = false) Integer pageSize,
@RequestParam(value = "beerName", required = false) String beerName,
@RequestParam(value = "beerStyle", required = false) BeerStyleEnum beerStyle){

if (pageNumber == null || pageNumber < 0){
pageNumber = DEFAULT_PAGE_NUMBER;
}

if (pageSize == null || pageSize < 1) {
pageSize = DEFAULT_PAGE_SIZE;
}

BeerPagedList beerList = beerService.listBeers(beerName, beerStyle, PageRequest.of(pageNumber, pageSize));

return new ResponseEntity<>(beerList, HttpStatus.OK);
}

@GetMapping("/{beerId}")
public ResponseEntity<BeerDto> getBeerById(@PathVariable("beerId") UUID beerId){
return new ResponseEntity<>(beerService.getById(beerId), HttpStatus.OK);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import guru.springframework.msscbeerservice.domain.Beer;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import org.mapstruct.DecoratedWith;
import org.mapstruct.Mapper;

/**
* Created by jt on 2019-05-25.
*/
@Mapper(uses = {DateMapper.class})
@DecoratedWith(BeerMapperDecorator.class)
public interface BeerMapper {

BeerDto beerToBeerDto(Beer beer);
Expand Down
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);
}
}
1 change: 1 addition & 0 deletions src/main/resources/application.properties
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
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);

}
}

0 comments on commit 025c654

Please sign in to comment.