diff --git a/src/main/java/fr/plil/sio/examen/api/Comment.java b/src/main/java/fr/plil/sio/examen/api/Comment.java index 020e3a9..dc35803 100644 --- a/src/main/java/fr/plil/sio/examen/api/Comment.java +++ b/src/main/java/fr/plil/sio/examen/api/Comment.java @@ -39,4 +39,36 @@ public class Comment { @Column(nullable = false) private String message; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public Owner getReporter(){ + return this.reporter; + } + + public void setReporter(Owner owner){ + this.reporter = owner; + } + + public Animal getAnimal(){ + return this.animal; + } + + public void setAnimal(Animal animal){ + this.animal = animal; + } + + public String getMessage(){ + return this.message; + } + + public void setMessage(String message){ + this.message = message; + } } diff --git a/src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java b/src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java index 2258ee5..0ca8116 100644 --- a/src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java +++ b/src/main/java/fr/plil/sio/examen/repositories/CommentRepository.java @@ -11,7 +11,7 @@ import org.springframework.data.jpa.repository.JpaRepository; */ public interface CommentRepository extends JpaRepository{ - List findByOwner(Owner owner); + List findByReporter(Owner reporter); List findByAnimal(Animal animal); diff --git a/src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java b/src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java index 3d66cdd..2902c54 100644 --- a/src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java +++ b/src/main/java/fr/plil/sio/examen/services/AnimalServiceImpl.java @@ -33,7 +33,14 @@ public class AnimalServiceImpl implements AnimalService { @Override @Transactional public void delete(Animal animal) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if(animal==null){ + throw new IllegalArgumentException("animal must be not null"); + } + if(animalRepository.findByName(animal.getName()).size()==0){ + throw new IllegalArgumentException("animal not in database"); + } + + animalRepository.delete(animal); } } diff --git a/src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java b/src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java index cc60c48..9ae9435 100644 --- a/src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java +++ b/src/main/java/fr/plil/sio/examen/services/CommentServiceImpl.java @@ -3,7 +3,11 @@ 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; +import fr.plil.sio.examen.repositories.AnimalRepository; +import fr.plil.sio.examen.repositories.CommentRepository; +import java.util.ArrayList; import java.util.List; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -13,34 +17,84 @@ import org.springframework.transaction.annotation.Transactional; @Service public class CommentServiceImpl implements CommentService { + @Autowired + private CommentRepository commentRepository; + + @Autowired + private AnimalRepository animalRepository; + @Override @Transactional public Comment add(Owner owner, Animal animal, String text) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + 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; } @Override @Transactional public void delete(Comment comment) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + 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); } @Override @Transactional(readOnly = true) public List findByAnimal(Animal animal) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if(animal == null){ + throw new IllegalArgumentException("animal must be not null"); + } + return commentRepository.findByAnimal(animal); } @Override @Transactional(readOnly = true) public List findByReporter(Owner reporter) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + if(reporter == null){ + throw new IllegalArgumentException("reporter must be not null"); + } + return commentRepository.findByReporter(reporter); } @Override @Transactional(readOnly = true) public List findByOwner(Owner owner) { - throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates. + 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 listAnimals = animalRepository.findByOwner(owner); + + //Création d'une liste de commentaire vide pour les y ajouter + List listComments = new ArrayList(); + + //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; } } -- libgit2 0.21.2