CommentServiceImpl.java 3.79 KB
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;

/**
 * TODO: complete all these methods
 */
@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) {
        
        //Tests sur les paramètres
        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");
        }
        
        //Création de l'object Comment
        Comment comment = new Comment();
        comment.setAnimal(animal);
        comment.setReporter(owner);
        comment.setMessage(text);
        
        //On le sauvegarde
        commentRepository.save(comment);
        //Et one le return
        return comment;
    }

    @Override
    @Transactional
    public void delete(Comment comment) {
        
        //Tests sur les paramètres
        if(comment==null){
           throw new IllegalArgumentException("comment must be not null");
        }
        //Si pas de messages avec le meme texte alors le commentaire n'est pas en base
        if(commentRepository.findByMessage(comment.getMessage()).size()==0){
            //On lance donc une exception
            throw new IllegalArgumentException("Comment not in database");
        }
       
        //On le supprime si pas d'exception ci dessus
        commentRepository.delete(comment);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Comment> findByAnimal(Animal animal) {
        if(animal == null){
            throw new IllegalArgumentException("animal must be not null");
        }
        //Appel de la méthode findByAnimal de la classe CommentRepository
        return commentRepository.findByAnimal(animal);
    }

    @Override
    @Transactional(readOnly = true)    
    public List<Comment> findByReporter(Owner reporter) {
        if(reporter == null){
            throw new IllegalArgumentException("reporter must be not null");
        }
        //Appel de la méthode findByReporter de la classe CommentRepository
        return commentRepository.findByReporter(reporter); 
    }

    @Override
    @Transactional(readOnly = true)    
    public List<Comment> findByOwner(Owner owner) {
        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;
    }
    
}