Created
June 12, 2019 19:04
-
-
Save jbleduigou/46dc210d6bf12386dc846504f2f1fe34 to your computer and use it in GitHub Desktop.
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
package com.github.jbleduigou.beer.service; | |
import com.github.jbleduigou.beer.exception.EntityNotFoundException; | |
import com.github.jbleduigou.beer.model.Beer; | |
import com.github.jbleduigou.beer.repository.BeerRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import javax.validation.constraints.NotNull; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
@Service | |
public class BeerService { | |
private final BeerRepository repository; | |
@Autowired | |
public BeerService(BeerRepository repository) { | |
this.repository = repository; | |
} | |
public List<Beer> getAllBeers() { | |
return repository.findAll(); | |
} | |
public List<Beer> getAllNonAlcoholicBeers() { | |
return repository.findAll().stream() | |
.filter(Beer::isAlcoholFree) | |
.collect(Collectors.toList()); | |
} | |
public Beer getBeerById(@NotNull Long beerId) { | |
return repository.findById(beerId).orElseThrow(() -> new EntityNotFoundException("Beer", beerId)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment