Commit d23d96d227a775f812c59ef8ebc0e6c9977dc5f3
1 parent
9e5908e7
Dernier commit
Showing
3 changed files
with
24 additions
and
5 deletions
Show diff stats
src/main/java/fr/plil/sio/examen/api/Owner.java
... | ... | @@ -2,6 +2,7 @@ package fr.plil.sio.examen.api; |
2 | 2 | |
3 | 3 | import java.util.LinkedList; |
4 | 4 | import java.util.List; |
5 | +import javax.persistence.CascadeType; | |
5 | 6 | import javax.persistence.Column; |
6 | 7 | import javax.persistence.Entity; |
7 | 8 | import javax.persistence.GeneratedValue; |
... | ... | @@ -24,7 +25,7 @@ public class Owner { |
24 | 25 | @Column(name="NAME_O",nullable = false) |
25 | 26 | private String name; |
26 | 27 | |
27 | - @OneToMany(mappedBy = "owner") | |
28 | + @OneToMany(mappedBy = "owner",cascade = CascadeType.REMOVE) | |
28 | 29 | private List<Animal> animals = new LinkedList<>(); |
29 | 30 | |
30 | 31 | public Long getId() { | ... | ... |
src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java
... | ... | @@ -26,6 +26,7 @@ public class AnimalServiceImpl implements AnimalService { |
26 | 26 | animal.setName(name); |
27 | 27 | animal.setOwner(owner); |
28 | 28 | animalRepository.save(animal); |
29 | + owner.getAnimals().add(animal); | |
29 | 30 | return animal; |
30 | 31 | } |
31 | 32 | |
... | ... | @@ -36,11 +37,15 @@ public class AnimalServiceImpl implements AnimalService { |
36 | 37 | if(animal==null){ |
37 | 38 | throw new IllegalArgumentException("animal must be not null"); |
38 | 39 | } |
40 | + //Si on ne trouve pas d'animaux avec le meme nom alors il n'est pas en base de données | |
39 | 41 | if(animalRepository.findByName(animal.getName()).size()==0){ |
42 | + //On lance donc une exception | |
40 | 43 | throw new IllegalArgumentException("animal not in database"); |
41 | 44 | } |
42 | 45 | |
43 | - animalRepository.delete(animal); | |
46 | + //Enfin, on le supprime | |
47 | + animalRepository.delete(animal.getId()); | |
48 | + animal.getOwner().getAnimals().remove(animal); | |
44 | 49 | } |
45 | 50 | |
46 | 51 | } | ... | ... |
src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java
... | ... | @@ -26,7 +26,9 @@ public class CommentServiceImpl implements CommentService { |
26 | 26 | @Override |
27 | 27 | @Transactional |
28 | 28 | public Comment add(Owner owner, Animal animal, String text) { |
29 | - if(animal== null) { | |
29 | + | |
30 | + //Tests sur les paramètres | |
31 | + if(animal== null) { | |
30 | 32 | throw new IllegalArgumentException("name must be not null"); |
31 | 33 | } |
32 | 34 | if(owner == null) { |
... | ... | @@ -35,26 +37,35 @@ public class CommentServiceImpl implements CommentService { |
35 | 37 | if(text == null){ |
36 | 38 | throw new IllegalArgumentException("text must be not null"); |
37 | 39 | } |
40 | + | |
41 | + //Création de l'object Comment | |
38 | 42 | Comment comment = new Comment(); |
39 | 43 | comment.setAnimal(animal); |
40 | 44 | comment.setReporter(owner); |
41 | 45 | comment.setMessage(text); |
42 | 46 | |
47 | + //On le sauvegarde | |
43 | 48 | commentRepository.save(comment); |
49 | + //Et one le return | |
44 | 50 | return comment; |
45 | 51 | } |
46 | 52 | |
47 | 53 | @Override |
48 | 54 | @Transactional |
49 | 55 | public void delete(Comment comment) { |
56 | + | |
57 | + //Tests sur les paramètres | |
50 | 58 | if(comment==null){ |
51 | 59 | throw new IllegalArgumentException("comment must be not null"); |
52 | 60 | } |
61 | + //Si pas de messages avec le meme texte alors le commentaire n'est pas en base | |
53 | 62 | if(commentRepository.findByMessage(comment.getMessage()).size()==0){ |
54 | - throw new IllegalArgumentException("Comment not in database"); | |
63 | + //On lance donc une exception | |
64 | + throw new IllegalArgumentException("Comment not in database"); | |
55 | 65 | } |
56 | 66 | |
57 | - commentRepository.delete(comment); | |
67 | + //On le supprime si pas d'exception ci dessus | |
68 | + commentRepository.delete(comment); | |
58 | 69 | } |
59 | 70 | |
60 | 71 | @Override |
... | ... | @@ -63,6 +74,7 @@ public class CommentServiceImpl implements CommentService { |
63 | 74 | if(animal == null){ |
64 | 75 | throw new IllegalArgumentException("animal must be not null"); |
65 | 76 | } |
77 | + //Appel de la méthode findByAnimal de la classe CommentRepository | |
66 | 78 | return commentRepository.findByAnimal(animal); |
67 | 79 | } |
68 | 80 | |
... | ... | @@ -72,6 +84,7 @@ public class CommentServiceImpl implements CommentService { |
72 | 84 | if(reporter == null){ |
73 | 85 | throw new IllegalArgumentException("reporter must be not null"); |
74 | 86 | } |
87 | + //Appel de la méthode findByReporter de la classe CommentRepository | |
75 | 88 | return commentRepository.findByReporter(reporter); |
76 | 89 | } |
77 | 90 | ... | ... |