package fr.plil.sio.examen.services; import fr.plil.sio.examen.api.Animal; import fr.plil.sio.examen.api.Comment; import fr.plil.sio.examen.api.Owner; import fr.plil.sio.examen.repositories.AnimalRepository; import fr.plil.sio.examen.repositories.CommentRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @Service public class AnimalServiceImpl implements AnimalService { @Autowired private AnimalRepository animalRepository; @Autowired private CommentRepository commentRepository; @Override @Transactional public Animal create(String name, Owner owner) { if(name == null) { throw new IllegalArgumentException("name must be not null"); } if(owner == null) { throw new IllegalArgumentException("owner must be not null"); } Animal animal = new Animal(); animal.setName(name); animal.setOwner(owner); animalRepository.save(animal); return animal; } @Override @Transactional public void delete(Animal animal) { if(animal == null) throw new IllegalArgumentException("Animal must not be null"); if(animal.getId() == null) throw new IllegalArgumentException("Animal doesn't exist"); for(Comment c : animal.getComments()) commentRepository.delete(c); animalRepository.delete(animal); } }