package xyz.danielcortes.repository;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import xyz.danielcortes.framework.PersistenceManager;
import xyz.danielcortes.models.Autor;

public class AutorRepository {
  private EntityManager em;

  public AutorRepository() { this.em = PersistenceManager.getEntityManager(); }

  public List<Autor> getAll() {
    TypedQuery<Autor> query = em.createQuery("SELECT a FROM Autor a", Autor.class);
    return query.getResultList();
  }

  public void save(Autor autor) {
    em.getTransaction().begin();
    em.persist(autor);
    em.getTransaction().commit();
  }

  public void update(Autor autor) {
    em.getTransaction().begin();
    em.merge(autor);
    em.getTransaction().commit();
  }

  public void delete(Autor autor) {
    em.getTransaction().begin();
    em.remove(autor);
    em.getTransaction().commit();
  }

}
