589e1ad3
jcartign
First version
|
1
2
3
|
package fr.plil.sio.examen.services;
import fr.plil.sio.examen.api.Owner;
|
bba62e85
rvangrev
Ajout de tests po...
|
4
5
6
7
|
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;
|
589e1ad3
jcartign
First version
|
8
|
import fr.plil.sio.examen.repositories.OwnerRepository;
|
bba62e85
rvangrev
Ajout de tests po...
|
9
10
|
import java.util.ArrayList;
import java.util.List;
|
589e1ad3
jcartign
First version
|
11
12
13
14
15
16
17
18
|
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class OwnerServiceImpl implements OwnerService {
@Autowired
private OwnerRepository ownerRepository;
|
bba62e85
rvangrev
Ajout de tests po...
|
19
20
21
22
23
24
|
@Autowired
private CommentRepository commentRepository;
@Autowired
private AnimalRepository animalRepository;
|
589e1ad3
jcartign
First version
|
25
26
27
|
@Override
public void delete(Owner owner) {
|
bba62e85
rvangrev
Ajout de tests po...
|
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
|
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);
|
589e1ad3
jcartign
First version
|
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
}
@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;
}
}
|