package tutorial.core.services.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tutorial.core.models.entities.EmailDomain;
import tutorial.core.repositories.EmailDomainRepo;
import tutorial.core.services.EmailDomainService;
import tutorial.core.services.exceptions.ActiveIngredientExistsException;
import tutorial.core.services.util.EmailDomainList;
import tutorial.rest.resources.EmailDomainResource;

/**
 * 
 * @author Xavier
 */
@Service
@Transactional
public class EmailDomainServiceImpl implements EmailDomainService {

    @Autowired
    private EmailDomainRepo emailDomainRepo;

    @Override
    public EmailDomain findEmailDomain(Long id) {
        return emailDomainRepo.findEmailDomain(id);
    }

    @Override
    public EmailDomain createEmailDomain(EmailDomainResource data) {
        EmailDomain emailDomainSameName = emailDomainRepo.findEmailDomainByName(data.getEmailDomainText());

        if(emailDomainSameName != null)
        {
            throw new ActiveIngredientExistsException();
        }
        
        EmailDomain emailDomain = emailDomainRepo.createEmailDomain(data.toEmailDomain());
        return emailDomain;
    }

    @Override
    public EmailDomainList findAllEmailDomains() {
        return new EmailDomainList(emailDomainRepo.findAllEmailDomains());
    }

    @Override
    public void deleteAllEmailDomains() {
        emailDomainRepo.deleteAllEmailDomains();
    }
}
