Suggest a feature
×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Java
Posted by: Andras Madi-Szabo
Added: May 19, 2020 10:56 AM
Views: 130
Tags: pet vizsga
  1. import com.greenfoxacademy.pet_shelter.models.Human;
  2. import com.greenfoxacademy.pet_shelter.repositories.HumanRepository;
  3. import java.util.List;
  4. import javassist.NotFoundException;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7.  
  8. @Service
  9. public class HumanServiceImpl implements HumanService {
  10.  
  11.   @Autowired
  12.   private HumanRepository humanRepository;
  13.  
  14.   @Override
  15.   public List<Human> returnAllHuman() {
  16.     return (List<Human>) humanRepository.findAll();
  17.   }
  18.  
  19.   @Override
  20.   public Human returnHumanById(Long id) {
  21.     return humanRepository.findById(id).orElse(null);
  22.   }
  23.  
  24.   @Override
  25.   public void saveHuman(Human human) {
  26.     humanRepository.save(human);
  27.   }
  28.  
  29.   @Override
  30.   public void deleteHuman(Long id) throws NotFoundException {
  31.     if (humanRepository.findById(id).isPresent()) {
  32.       humanRepository.deleteById(id);
  33.     } else {
  34.       throw new NotFoundException("No human on the given index");
  35.     }
  36.   }
  37.  
  38.   @Override
  39.   public void editHuman(Human human) {
  40.     humanRepository.save(human);
  41.   }
  42.  
  43. }