Blame view

src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java 3.02 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;

575ff2e8   msahmane   Examen terminé.
6
7
  import fr.plil.sio.examen.repositories.CommentRepository;

  import java.util.LinkedList;

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

575ff2e8   msahmane   Examen terminé.
9
  import org.springframework.beans.factory.annotation.Autowired;

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

  import org.springframework.transaction.annotation.Transactional;

  

  /**

   * TODO: complete all these methods

   */

  @Service

  public class CommentServiceImpl implements CommentService {

  

575ff2e8   msahmane   Examen terminé.
19
20
21
      @Autowired

      private CommentRepository commentRepository;

      

589e1ad3   jcartign   First version
22
23
24
      @Override

      @Transactional

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

575ff2e8   msahmane   Examen terminé.
25
26
27
28
29
30
31
32
33
34
35
36
37
38
          if(owner == null)

              throw new IllegalArgumentException("Owner cannot be null");

          if(animal == null)

              throw new IllegalArgumentException("Animal Cannot be null");

          if(text == null)

              throw new IllegalArgumentException("Text cannot be null");

          

          Comment comment = new Comment();

          comment.setReporter(owner);

          comment.setAnimal(animal);

          comment.setMessage(text);

          commentRepository.save(comment);

          

          return comment;

589e1ad3   jcartign   First version
39
40
41
42
43
      }

  

      @Override

      @Transactional

      public void delete(Comment comment) {

575ff2e8   msahmane   Examen terminé.
44
45
46
47
48
49
          if(comment == null)

              throw new IllegalArgumentException("comment cannot be null");

          if(comment.getId() == null)

              throw new IllegalArgumentException("Comment doesn't exists");

          

          commentRepository.delete(comment);

589e1ad3   jcartign   First version
50
51
52
53
54
      }

  

      @Override

      @Transactional(readOnly = true)

      public List<Comment> findByAnimal(Animal animal) {

575ff2e8   msahmane   Examen terminé.
55
56
57
58
59
60
          if(animal == null)

              throw new IllegalArgumentException("Animal cannot be null");

          if(animal.getId() == null)

              throw new IllegalArgumentException("Animal doesn't exists");

          

          return commentRepository.findByAnimal(animal);

589e1ad3   jcartign   First version
61
62
63
64
65
      }

  

      @Override

      @Transactional(readOnly = true)    

      public List<Comment> findByReporter(Owner reporter) {

575ff2e8   msahmane   Examen terminé.
66
67
68
69
70
71
          if(reporter == null)

              throw new IllegalArgumentException("reporter cannot be null");

          if(reporter.getId() == null)

              throw new IllegalArgumentException("reporter doesn't exists");

          

          return commentRepository.findByReporter(reporter);

589e1ad3   jcartign   First version
72
73
74
75
76
      }

  

      @Override

      @Transactional(readOnly = true)    

      public List<Comment> findByOwner(Owner owner) {

575ff2e8   msahmane   Examen terminé.
77
78
79
80
81
82
83
84
85
86
87
88
89
90
          if(owner == null)

              throw new IllegalArgumentException("owner cannot be null");

          if(owner.getId() == null)

              throw new IllegalArgumentException("owner doesn't exists");

          

          List<Animal> animalsOfOwner = owner.getAnimals();

          List<Comment> comments = new LinkedList<>();

          

          for(Animal a : animalsOfOwner){                            

              comments.addAll(a.getComments());

          }

          

          return comments;

          

589e1ad3   jcartign   First version
91
92
93
      }

      

  }