-
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 methods for controller. closes #15
- Loading branch information
1 parent
425338d
commit f77647f
Showing
5 changed files
with
77 additions
and
11 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
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
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); | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/guru/springframework/msscbeerservice/services/BeerServiceImpl.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,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)); | ||
} | ||
} |
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
7 changes: 7 additions & 0 deletions
7
src/main/java/guru/springframework/msscbeerservice/web/controller/NotFoundException.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,7 @@ | ||
package guru.springframework.msscbeerservice.web.controller; | ||
|
||
/** | ||
* Created by jt on 2019-06-06. | ||
*/ | ||
public class NotFoundException extends RuntimeException { | ||
} |
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