Commit 5fd5c1856aafb612aa0bf32585c9d473d923c0f0

Authored by rvangrev
1 parent 069a0012

Correction des Méthodes findByXX : OK

Implémentation de la classe CommentServiceImpl : OK
Implémentation de la méthiode delete de la classe AnimalService(IMPL) : OK
src/main/java/fr/plil/sio/examen/api/Comment.java
@@ -39,4 +39,36 @@ public class Comment { @@ -39,4 +39,36 @@ public class Comment {
39 39
40 @Column(nullable = false) 40 @Column(nullable = false)
41 private String message; 41 private String message;
  42 +
  43 + public Long getId() {
  44 + return id;
  45 + }
  46 +
  47 + public void setId(Long id) {
  48 + this.id = id;
  49 + }
  50 +
  51 + public Owner getReporter(){
  52 + return this.reporter;
  53 + }
  54 +
  55 + public void setReporter(Owner owner){
  56 + this.reporter = owner;
  57 + }
  58 +
  59 + public Animal getAnimal(){
  60 + return this.animal;
  61 + }
  62 +
  63 + public void setAnimal(Animal animal){
  64 + this.animal = animal;
  65 + }
  66 +
  67 + public String getMessage(){
  68 + return this.message;
  69 + }
  70 +
  71 + public void setMessage(String message){
  72 + this.message = message;
  73 + }
42 } 74 }
src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java
@@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.JpaRepository; @@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.JpaRepository;
11 */ 11 */
12 public interface CommentRepository extends JpaRepository<Comment, Long>{ 12 public interface CommentRepository extends JpaRepository<Comment, Long>{
13 13
14 - List<Comment> findByOwner(Owner owner); 14 + List<Comment> findByReporter(Owner reporter);
15 15
16 List<Comment> findByAnimal(Animal animal); 16 List<Comment> findByAnimal(Animal animal);
17 17
src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java
@@ -33,7 +33,14 @@ public class AnimalServiceImpl implements AnimalService { @@ -33,7 +33,14 @@ public class AnimalServiceImpl implements AnimalService {
33 @Override 33 @Override
34 @Transactional 34 @Transactional
35 public void delete(Animal animal) { 35 public void delete(Animal animal) {
36 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 36 + if(animal==null){
  37 + throw new IllegalArgumentException("animal must be not null");
  38 + }
  39 + if(animalRepository.findByName(animal.getName()).size()==0){
  40 + throw new IllegalArgumentException("animal not in database");
  41 + }
  42 +
  43 + animalRepository.delete(animal);
37 } 44 }
38 45
39 } 46 }
src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java
@@ -3,7 +3,11 @@ package fr.plil.sio.examen.services; @@ -3,7 +3,11 @@ package fr.plil.sio.examen.services;
3 import fr.plil.sio.examen.api.Animal; 3 import fr.plil.sio.examen.api.Animal;
4 import fr.plil.sio.examen.api.Comment; 4 import fr.plil.sio.examen.api.Comment;
5 import fr.plil.sio.examen.api.Owner; 5 import fr.plil.sio.examen.api.Owner;
  6 +import fr.plil.sio.examen.repositories.AnimalRepository;
  7 +import fr.plil.sio.examen.repositories.CommentRepository;
  8 +import java.util.ArrayList;
6 import java.util.List; 9 import java.util.List;
  10 +import org.springframework.beans.factory.annotation.Autowired;
7 import org.springframework.stereotype.Service; 11 import org.springframework.stereotype.Service;
8 import org.springframework.transaction.annotation.Transactional; 12 import org.springframework.transaction.annotation.Transactional;
9 13
@@ -13,34 +17,84 @@ import org.springframework.transaction.annotation.Transactional; @@ -13,34 +17,84 @@ import org.springframework.transaction.annotation.Transactional;
13 @Service 17 @Service
14 public class CommentServiceImpl implements CommentService { 18 public class CommentServiceImpl implements CommentService {
15 19
  20 + @Autowired
  21 + private CommentRepository commentRepository;
  22 +
  23 + @Autowired
  24 + private AnimalRepository animalRepository;
  25 +
16 @Override 26 @Override
17 @Transactional 27 @Transactional
18 public Comment add(Owner owner, Animal animal, String text) { 28 public Comment add(Owner owner, Animal animal, String text) {
19 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 29 + if(animal== null) {
  30 + throw new IllegalArgumentException("name must be not null");
  31 + }
  32 + if(owner == null) {
  33 + throw new IllegalArgumentException("owner must be not null");
  34 + }
  35 + if(text == null){
  36 + throw new IllegalArgumentException("text must be not null");
  37 + }
  38 + Comment comment = new Comment();
  39 + comment.setAnimal(animal);
  40 + comment.setReporter(owner);
  41 + comment.setMessage(text);
  42 +
  43 + commentRepository.save(comment);
  44 + return comment;
20 } 45 }
21 46
22 @Override 47 @Override
23 @Transactional 48 @Transactional
24 public void delete(Comment comment) { 49 public void delete(Comment comment) {
25 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 50 + if(comment==null){
  51 + throw new IllegalArgumentException("comment must be not null");
  52 + }
  53 + if(commentRepository.findByMessage(comment.getMessage()).size()==0){
  54 + throw new IllegalArgumentException("Comment not in database");
  55 + }
  56 +
  57 + commentRepository.delete(comment);
26 } 58 }
27 59
28 @Override 60 @Override
29 @Transactional(readOnly = true) 61 @Transactional(readOnly = true)
30 public List<Comment> findByAnimal(Animal animal) { 62 public List<Comment> findByAnimal(Animal animal) {
31 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 63 + if(animal == null){
  64 + throw new IllegalArgumentException("animal must be not null");
  65 + }
  66 + return commentRepository.findByAnimal(animal);
32 } 67 }
33 68
34 @Override 69 @Override
35 @Transactional(readOnly = true) 70 @Transactional(readOnly = true)
36 public List<Comment> findByReporter(Owner reporter) { 71 public List<Comment> findByReporter(Owner reporter) {
37 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 72 + if(reporter == null){
  73 + throw new IllegalArgumentException("reporter must be not null");
  74 + }
  75 + return commentRepository.findByReporter(reporter);
38 } 76 }
39 77
40 @Override 78 @Override
41 @Transactional(readOnly = true) 79 @Transactional(readOnly = true)
42 public List<Comment> findByOwner(Owner owner) { 80 public List<Comment> findByOwner(Owner owner) {
43 - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. 81 + if(owner == null){
  82 + throw new IllegalArgumentException("owner must be not null");
  83 + }
  84 + //On récupère la liste de tous les animaux de l'owner
  85 + List<Animal> listAnimals = animalRepository.findByOwner(owner);
  86 +
  87 + //Création d'une liste de commentaire vide pour les y ajouter
  88 + List<Comment> listComments = new ArrayList<Comment>();
  89 +
  90 + //Parcours de la liste des animaux de l'owner
  91 + for(Animal animal : listAnimals){
  92 + //Ajout à la liste des commentaires sur l'animal en cours
  93 + listComments.addAll(commentRepository.findByAnimal(animal));
  94 + }
  95 +
  96 + //On retourne la liste de commentaires
  97 + return listComments;
44 } 98 }
45 99
46 } 100 }