Blame view

src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java 3.26 KB
589e1ad3   jcartign   First version
1
2
3
4
5
  package fr.plil.sio.examen.services;

  

  import fr.plil.sio.examen.api.Animal;

  import fr.plil.sio.examen.api.Comment;

  import fr.plil.sio.examen.api.Owner;

5fd5c185   rvangrev   Correction des Mé...
6
7
8
  import fr.plil.sio.examen.repositories.AnimalRepository;

  import fr.plil.sio.examen.repositories.CommentRepository;

  import java.util.ArrayList;

589e1ad3   jcartign   First version
9
  import java.util.List;

5fd5c185   rvangrev   Correction des Mé...
10
  import org.springframework.beans.factory.annotation.Autowired;

589e1ad3   jcartign   First version
11
12
13
14
15
16
17
18
19
  import org.springframework.stereotype.Service;

  import org.springframework.transaction.annotation.Transactional;

  

  /**

   * TODO: complete all these methods

   */

  @Service

  public class CommentServiceImpl implements CommentService {

  

5fd5c185   rvangrev   Correction des Mé...
20
21
22
23
24
25
      @Autowired

      private CommentRepository commentRepository;

      

      @Autowired

      private AnimalRepository animalRepository;

      

589e1ad3   jcartign   First version
26
27
28
      @Override

      @Transactional

      public Comment add(Owner owner, Animal animal, String text) {

5fd5c185   rvangrev   Correction des Mé...
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
           if(animal== null) {

              throw new IllegalArgumentException("name must be not null");

          }

          if(owner == null) {

              throw new IllegalArgumentException("owner must be not null");

          }

          if(text == null){

              throw new IllegalArgumentException("text must be not null");

          }

          Comment comment = new Comment();

          comment.setAnimal(animal);

          comment.setReporter(owner);

          comment.setMessage(text);

          

          commentRepository.save(comment);

          return comment;

589e1ad3   jcartign   First version
45
46
47
48
49
      }

  

      @Override

      @Transactional

      public void delete(Comment comment) {

5fd5c185   rvangrev   Correction des Mé...
50
51
52
53
54
55
56
57
          if(comment==null){

             throw new IllegalArgumentException("comment must be not null");

          }

          if(commentRepository.findByMessage(comment.getMessage()).size()==0){

             throw new IllegalArgumentException("Comment not in database");

          }

         

         commentRepository.delete(comment);

589e1ad3   jcartign   First version
58
59
60
61
62
      }

  

      @Override

      @Transactional(readOnly = true)

      public List<Comment> findByAnimal(Animal animal) {

5fd5c185   rvangrev   Correction des Mé...
63
64
65
66
          if(animal == null){

              throw new IllegalArgumentException("animal must be not null");

          }

          return commentRepository.findByAnimal(animal);

589e1ad3   jcartign   First version
67
68
69
70
71
      }

  

      @Override

      @Transactional(readOnly = true)    

      public List<Comment> findByReporter(Owner reporter) {

5fd5c185   rvangrev   Correction des Mé...
72
73
74
75
          if(reporter == null){

              throw new IllegalArgumentException("reporter must be not null");

          }

          return commentRepository.findByReporter(reporter); 

589e1ad3   jcartign   First version
76
77
78
79
80
      }

  

      @Override

      @Transactional(readOnly = true)    

      public List<Comment> findByOwner(Owner owner) {

5fd5c185   rvangrev   Correction des Mé...
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
          if(owner == null){

              throw new IllegalArgumentException("owner must be not null");

          }

          //On récupère la liste de tous les animaux de l'owner

          List<Animal> listAnimals = animalRepository.findByOwner(owner);

          

          //Création d'une liste de commentaire vide pour les y ajouter

          List<Comment> listComments = new ArrayList<Comment>();

          

          //Parcours de la liste des animaux de l'owner

          for(Animal animal : listAnimals){

              //Ajout à la liste des commentaires sur l'animal en cours

              listComments.addAll(commentRepository.findByAnimal(animal));

          }

          

          //On retourne la liste de commentaires

          return listComments;

589e1ad3   jcartign   First version
98
99
100
      }

      

  }