Skip to content

Commit

Permalink
adding methods for controller. closes #15
Browse files Browse the repository at this point in the history
  • Loading branch information
springframeworkguru committed Jun 6, 2019
1 parent 425338d commit f77647f
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package guru.springframework.msscbeerservice.services;

import guru.springframework.msscbeerservice.web.model.BeerDto;

import java.util.UUID;

/**
* Created by jt on 2019-06-06.
*/
public interface BeerService {
BeerDto getById(UUID beerId);

BeerDto saveNewBeer(BeerDto beerDto);

BeerDto updateBeer(UUID beerId, BeerDto beerDto);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package guru.springframework.msscbeerservice.services;

import guru.springframework.msscbeerservice.domain.Beer;
import guru.springframework.msscbeerservice.repositories.BeerRepository;
import guru.springframework.msscbeerservice.web.controller.NotFoundException;
import guru.springframework.msscbeerservice.web.mappers.BeerMapper;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.UUID;

/**
* Created by jt on 2019-06-06.
*/
@RequiredArgsConstructor
@Service
public class BeerServiceImpl implements BeerService {
private final BeerRepository beerRepository;
private final BeerMapper beerMapper;

@Override
public BeerDto getById(UUID beerId) {
return beerMapper.beerToBeerDto(
beerRepository.findById(beerId).orElseThrow(NotFoundException::new)
);
}

@Override
public BeerDto saveNewBeer(BeerDto beerDto) {
return beerMapper.beerToBeerDto(beerRepository.save(beerMapper.beerDtoToBeer(beerDto)));
}

@Override
public BeerDto updateBeer(UUID beerId, BeerDto beerDto) {
Beer beer = beerRepository.findById(beerId).orElseThrow(NotFoundException::new);

beer.setBeerName(beerDto.getBeerName());
beer.setBeerStyle(beerDto.getBeerStyle().name());
beer.setPrice(beerDto.getPrice());
beer.setUpc(beerDto.getUpc());

return beerMapper.beerToBeerDto(beerRepository.save(beer));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package guru.springframework.msscbeerservice.web.controller;

import guru.springframework.msscbeerservice.services.BeerService;
import guru.springframework.msscbeerservice.web.model.BeerDto;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
Expand All @@ -11,30 +13,26 @@
/**
* Created by jt on 2019-05-12.
*/
@RequiredArgsConstructor
@RequestMapping("/api/v1/beer")
@RestController
public class BeerController {

private final BeerService beerService;

@GetMapping("/{beerId}")
public ResponseEntity<BeerDto> getBeerById(@PathVariable("beerId") UUID beerId){

//todo impl
return new ResponseEntity<>(BeerDto.builder().build(), HttpStatus.OK);
return new ResponseEntity<>(beerService.getById(beerId), HttpStatus.OK);
}

@PostMapping
public ResponseEntity saveNewBeer(@RequestBody @Validated BeerDto beerDto){

//todo impl
return new ResponseEntity(HttpStatus.CREATED);
return new ResponseEntity<>(beerService.saveNewBeer(beerDto), HttpStatus.CREATED);
}

@PutMapping("/{beerId}")
public ResponseEntity updateBeerById(@PathVariable("beerId") UUID beerId, @RequestBody @Validated BeerDto beerDto){

//todo impl
return new ResponseEntity(HttpStatus.NO_CONTENT);
return new ResponseEntity<>(beerService.updateBeer(beerId, beerDto), HttpStatus.NO_CONTENT);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package guru.springframework.msscbeerservice.web.controller;

/**
* Created by jt on 2019-06-06.
*/
public class NotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Mapper(uses = {DateMapper.class})
public interface BeerMapper {

BeerDto BeerToBeerDto(Beer beer);
BeerDto beerToBeerDto(Beer beer);

Beer BeerDtoToBeer(BeerDto dto);
Beer beerDtoToBeer(BeerDto dto);
}

0 comments on commit f77647f

Please sign in to comment.