Skip to content

Instantly share code, notes, and snippets.

@jbleduigou
Created June 12, 2019 19:04
Show Gist options
  • Save jbleduigou/46dc210d6bf12386dc846504f2f1fe34 to your computer and use it in GitHub Desktop.
Save jbleduigou/46dc210d6bf12386dc846504f2f1fe34 to your computer and use it in GitHub Desktop.
BeerService.java
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