Blame view

src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java 1.59 KB
589e1ad3   jcartign   First version
1
2
3
  package fr.plil.sio.examen.services;

  

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

575ff2e8   msahmane   Examen terminé.
4
  import fr.plil.sio.examen.api.Comment;

589e1ad3   jcartign   First version
5
6
  import fr.plil.sio.examen.api.Owner;

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

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

589e1ad3   jcartign   First version
8
9
10
11
12
13
14
15
16
  import org.springframework.beans.factory.annotation.Autowired;

  import org.springframework.stereotype.Service;

  import org.springframework.transaction.annotation.Transactional;

  

  @Service

  public class AnimalServiceImpl implements AnimalService {

  

      @Autowired

      private AnimalRepository animalRepository;

575ff2e8   msahmane   Examen terminé.
17
18
19
      

      @Autowired

      private CommentRepository commentRepository;

589e1ad3   jcartign   First version
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
  

      @Override

      @Transactional

      public Animal create(String name, Owner owner) {

          if(name == null) {

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

          }

          if(owner == null) {

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

          }

          Animal animal = new Animal();

          animal.setName(name);

          animal.setOwner(owner);

          animalRepository.save(animal);

          return animal;

      }

  

  

      @Override

      @Transactional    

      public void delete(Animal animal) {

575ff2e8   msahmane   Examen terminé.
41
42
43
44
45
46
47
48
49
50
          if(animal == null)

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

          

          if(animal.getId() == null)

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

          

          for(Comment c : animal.getComments())

              commentRepository.delete(c);

          

          animalRepository.delete(animal);

589e1ad3   jcartign   First version
51
52
53
      }

      

  }