OwnerServiceImpl.java
2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
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<Comment> 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<Animal> 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;
}
}