package dev.fyloz.trial.colorrecipesexplorer.service;

import dev.fyloz.trial.colorrecipesexplorer.exception.model.EntityNotFoundException;
import dev.fyloz.trial.colorrecipesexplorer.exception.model.ModelException;
import dev.fyloz.trial.colorrecipesexplorer.model.NamedModel;
import dev.fyloz.trial.colorrecipesexplorer.repository.NamedJpaRepository;

abstract public class AbstractJavaNamedService<E extends NamedModel, R extends NamedJpaRepository<E>> extends AbstractJavaService<E, R> {

    public AbstractJavaNamedService(Class<E> type) {
        super(type);
    }

    public boolean existsByName(String name) {
        return repository.existsByName(name);
    }

    public E getByName(String name) {
        E found = repository.findByName(name);
        if (found == null) throw new EntityNotFoundException(type, ModelException.IdentifierType.NAME, name);

        return found;
    }

    public void deleteByName(String name) {
        repository.deleteByName(name);
    }

}
