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.Editorial;

public class EditorialRepository {
  private EntityManager em;

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

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

  public void save(Editorial editorial) {
    em.getTransaction().begin();
    em.persist(editorial);
    em.getTransaction().commit();
  }

  public void update(Editorial editorial) {
    em.getTransaction().begin();
    em.merge(editorial);
    em.getTransaction().commit();
  }
  public void delete(Editorial editorial) {
    em.getTransaction().begin();
    em.remove(editorial);
    em.getTransaction().commit();
  }
}
