Commit bba62e8545a611d789836d4c4d8763c1b9a98bfa
1 parent
5fd5c185
Ajout de tests pour vérifications.
La suppression de l'owner semble ne pas effacer les commentaires associés
Showing
1 changed file
with
39 additions
and
1 deletions
Show diff stats
src/main/java/fr/plil/sio/examen/services/OwnerServiceImpl.java
1 | package fr.plil.sio.examen.services; | 1 | package fr.plil.sio.examen.services; |
2 | 2 | ||
3 | import fr.plil.sio.examen.api.Owner; | 3 | import fr.plil.sio.examen.api.Owner; |
4 | +import fr.plil.sio.examen.api.Comment; | ||
5 | +import fr.plil.sio.examen.api.Animal; | ||
6 | +import fr.plil.sio.examen.repositories.AnimalRepository; | ||
7 | +import fr.plil.sio.examen.repositories.CommentRepository; | ||
4 | import fr.plil.sio.examen.repositories.OwnerRepository; | 8 | import fr.plil.sio.examen.repositories.OwnerRepository; |
9 | +import java.util.ArrayList; | ||
10 | +import java.util.List; | ||
5 | import org.springframework.beans.factory.annotation.Autowired; | 11 | import org.springframework.beans.factory.annotation.Autowired; |
6 | import org.springframework.stereotype.Service; | 12 | import org.springframework.stereotype.Service; |
7 | 13 | ||
@@ -10,10 +16,42 @@ public class OwnerServiceImpl implements OwnerService { | @@ -10,10 +16,42 @@ public class OwnerServiceImpl implements OwnerService { | ||
10 | 16 | ||
11 | @Autowired | 17 | @Autowired |
12 | private OwnerRepository ownerRepository; | 18 | private OwnerRepository ownerRepository; |
19 | + | ||
20 | + @Autowired | ||
21 | + private CommentRepository commentRepository; | ||
22 | + | ||
23 | + @Autowired | ||
24 | + private AnimalRepository animalRepository; | ||
13 | 25 | ||
14 | @Override | 26 | @Override |
15 | public void delete(Owner owner) { | 27 | public void delete(Owner owner) { |
16 | - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. | 28 | + if(owner==null){ |
29 | + throw new IllegalArgumentException("owner must be not null"); | ||
30 | + } | ||
31 | + if(ownerRepository.findByName(owner.getName()).size()==0){ | ||
32 | + throw new IllegalArgumentException("owner not in database"); | ||
33 | + } | ||
34 | + | ||
35 | + //Suppression de ses commentaires | ||
36 | + | ||
37 | + //Création d'une liste pour stocker les commentaires | ||
38 | + List<Comment> listComments = commentRepository.findByReporter(owner); | ||
39 | + | ||
40 | + //Boucle pour la suppression de ses commentaires | ||
41 | + for(Comment comment : listComments){ | ||
42 | + commentRepository.delete(comment); | ||
43 | + } | ||
44 | + | ||
45 | + //Attention, les animaux ne peuvent pas avoir un owner null | ||
46 | + List<Animal> listAnimals = animalRepository.findByOwner(owner); | ||
47 | + for(Animal animal : listAnimals){ | ||
48 | + //On set le nouvel owner a un owner de base (du constructeur par défaut) | ||
49 | + animal.setOwner(new Owner()); | ||
50 | + } | ||
51 | + | ||
52 | + //Suppression de l'owner | ||
53 | + ownerRepository.delete(owner); | ||
54 | + | ||
17 | } | 55 | } |
18 | 56 | ||
19 | @Override | 57 | @Override |