package fr.plil.sio.examen.services; import fr.plil.sio.examen.api.Owner; import fr.plil.sio.examen.api.Comment; import fr.plil.sio.examen.api.Animal; import fr.plil.sio.examen.repositories.AnimalRepository; import fr.plil.sio.examen.repositories.CommentRepository; import fr.plil.sio.examen.repositories.OwnerRepository; import java.util.ArrayList; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class OwnerServiceImpl implements OwnerService { @Autowired private OwnerRepository ownerRepository; @Autowired private CommentRepository commentRepository; @Autowired private AnimalRepository animalRepository; @Override public void delete(Owner owner) { if(owner==null){ throw new IllegalArgumentException("owner must be not null"); } if(ownerRepository.findByName(owner.getName()).size()==0){ throw new IllegalArgumentException("owner not in database"); } //Suppression de ses commentaires //Création d'une liste pour stocker les commentaires List listComments = commentRepository.findByReporter(owner); //Boucle pour la suppression de ses commentaires for(Comment comment : listComments){ commentRepository.delete(comment); } //Attention, les animaux ne peuvent pas avoir un owner null List listAnimals = animalRepository.findByOwner(owner); for(Animal animal : listAnimals){ //On set le nouvel owner a un owner de base (du constructeur par défaut) animal.setOwner(new Owner()); } //Suppression de l'owner ownerRepository.delete(owner); } @Override public Owner create(String name) { if(name == null) { throw new IllegalArgumentException("name must be not null"); } Owner owner = new Owner(); owner.setName(name); ownerRepository.save(owner); return owner; } }