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,6 +2,7 @@ package fr.plil.sio.examen.api; | ||
2 | 2 | ||
3 | import java.util.LinkedList; | 3 | import java.util.LinkedList; |
4 | import java.util.List; | 4 | import java.util.List; |
5 | +import javax.persistence.CascadeType; | ||
5 | import javax.persistence.Column; | 6 | import javax.persistence.Column; |
6 | import javax.persistence.Entity; | 7 | import javax.persistence.Entity; |
7 | import javax.persistence.GeneratedValue; | 8 | import javax.persistence.GeneratedValue; |
@@ -24,7 +25,7 @@ public class Owner { | @@ -24,7 +25,7 @@ public class Owner { | ||
24 | @Column(name="NAME_O",nullable = false) | 25 | @Column(name="NAME_O",nullable = false) |
25 | private String name; | 26 | private String name; |
26 | 27 | ||
27 | - @OneToMany(mappedBy = "owner") | 28 | + @OneToMany(mappedBy = "owner",cascade = CascadeType.REMOVE) |
28 | private List<Animal> animals = new LinkedList<>(); | 29 | private List<Animal> animals = new LinkedList<>(); |
29 | 30 | ||
30 | public Long getId() { | 31 | public Long getId() { |
src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java
@@ -26,6 +26,7 @@ public class AnimalServiceImpl implements AnimalService { | @@ -26,6 +26,7 @@ public class AnimalServiceImpl implements AnimalService { | ||
26 | animal.setName(name); | 26 | animal.setName(name); |
27 | animal.setOwner(owner); | 27 | animal.setOwner(owner); |
28 | animalRepository.save(animal); | 28 | animalRepository.save(animal); |
29 | + owner.getAnimals().add(animal); | ||
29 | return animal; | 30 | return animal; |
30 | } | 31 | } |
31 | 32 | ||
@@ -36,11 +37,15 @@ public class AnimalServiceImpl implements AnimalService { | @@ -36,11 +37,15 @@ public class AnimalServiceImpl implements AnimalService { | ||
36 | if(animal==null){ | 37 | if(animal==null){ |
37 | throw new IllegalArgumentException("animal must be not null"); | 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 | if(animalRepository.findByName(animal.getName()).size()==0){ | 41 | if(animalRepository.findByName(animal.getName()).size()==0){ |
42 | + //On lance donc une exception | ||
40 | throw new IllegalArgumentException("animal not in database"); | 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,7 +26,9 @@ public class CommentServiceImpl implements CommentService { | ||
26 | @Override | 26 | @Override |
27 | @Transactional | 27 | @Transactional |
28 | public Comment add(Owner owner, Animal animal, String text) { | 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 | throw new IllegalArgumentException("name must be not null"); | 32 | throw new IllegalArgumentException("name must be not null"); |
31 | } | 33 | } |
32 | if(owner == null) { | 34 | if(owner == null) { |
@@ -35,26 +37,35 @@ public class CommentServiceImpl implements CommentService { | @@ -35,26 +37,35 @@ public class CommentServiceImpl implements CommentService { | ||
35 | if(text == null){ | 37 | if(text == null){ |
36 | throw new IllegalArgumentException("text must be not null"); | 38 | throw new IllegalArgumentException("text must be not null"); |
37 | } | 39 | } |
40 | + | ||
41 | + //Création de l'object Comment | ||
38 | Comment comment = new Comment(); | 42 | Comment comment = new Comment(); |
39 | comment.setAnimal(animal); | 43 | comment.setAnimal(animal); |
40 | comment.setReporter(owner); | 44 | comment.setReporter(owner); |
41 | comment.setMessage(text); | 45 | comment.setMessage(text); |
42 | 46 | ||
47 | + //On le sauvegarde | ||
43 | commentRepository.save(comment); | 48 | commentRepository.save(comment); |
49 | + //Et one le return | ||
44 | return comment; | 50 | return comment; |
45 | } | 51 | } |
46 | 52 | ||
47 | @Override | 53 | @Override |
48 | @Transactional | 54 | @Transactional |
49 | public void delete(Comment comment) { | 55 | public void delete(Comment comment) { |
56 | + | ||
57 | + //Tests sur les paramètres | ||
50 | if(comment==null){ | 58 | if(comment==null){ |
51 | throw new IllegalArgumentException("comment must be not null"); | 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 | if(commentRepository.findByMessage(comment.getMessage()).size()==0){ | 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 | @Override | 71 | @Override |
@@ -63,6 +74,7 @@ public class CommentServiceImpl implements CommentService { | @@ -63,6 +74,7 @@ public class CommentServiceImpl implements CommentService { | ||
63 | if(animal == null){ | 74 | if(animal == null){ |
64 | throw new IllegalArgumentException("animal must be not null"); | 75 | throw new IllegalArgumentException("animal must be not null"); |
65 | } | 76 | } |
77 | + //Appel de la méthode findByAnimal de la classe CommentRepository | ||
66 | return commentRepository.findByAnimal(animal); | 78 | return commentRepository.findByAnimal(animal); |
67 | } | 79 | } |
68 | 80 | ||
@@ -72,6 +84,7 @@ public class CommentServiceImpl implements CommentService { | @@ -72,6 +84,7 @@ public class CommentServiceImpl implements CommentService { | ||
72 | if(reporter == null){ | 84 | if(reporter == null){ |
73 | throw new IllegalArgumentException("reporter must be not null"); | 85 | throw new IllegalArgumentException("reporter must be not null"); |
74 | } | 86 | } |
87 | + //Appel de la méthode findByReporter de la classe CommentRepository | ||
75 | return commentRepository.findByReporter(reporter); | 88 | return commentRepository.findByReporter(reporter); |
76 | } | 89 | } |
77 | 90 |