package de.fau.amos.virtualledger.server.persistence;

import de.fau.amos.virtualledger.dtos.Contact;
import de.fau.amos.virtualledger.server.model.ContactsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.PersistenceContext;
import javax.persistence.EntityExistsException;
import javax.persistence.Query;
import java.lang.invoke.MethodHandles;
import java.util.List;

@Component
public class ContactsRepository {
    @SuppressWarnings("unused")
    private static final Logger LOGGER = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

    @PersistenceContext
    private EntityManager entityManager;


    public void createContact(final Contact contact, final String userEmail) {

        final int userId;
        try {
            final Query query = entityManager.createQuery("Select u.id FROM User u WHERE u.email = :email");
            query.setParameter("email", userEmail);
            final List resultList = query.getResultList();
            if (resultList.isEmpty()) {
                throw new IllegalArgumentException("User does not exist");
            }
            userId = (int) resultList.get(0);

            final ContactsEntity contactsEntity = new ContactsEntity();
            contactsEntity.setUserId(userId);
            contactsEntity.setEmail(contact.getEmail());
            contactsEntity.setFirstname(contact.getFirstName());
            contactsEntity.setLastname(contact.getLastName());

            final EntityTransaction entityTransaction = entityManager.getTransaction();
            try {
                entityTransaction.begin();
                entityManager.persist(contactsEntity);
                entityTransaction.commit();
            } catch (final EntityExistsException entityExistsException) {
                LOGGER.warn("Entity already exists: " + contactsEntity);
                entityTransaction.rollback();
                throw entityExistsException;
            }
        } catch (final Exception ex) {
            LOGGER.warn("", ex);
            throw ex;
        } finally {
            entityManager.close();
        }

    }

    public List<ContactsEntity> getContactsByEmail(final String email) {
        try {
            final Query query = entityManager
                    .createQuery("Select contacts FROM ContactsEntity contacts JOIN User user ON contacts.userId = user.id WHERE user.email = :email");
            query.setParameter("email", email);
            return query.getResultList();
        } finally {
            entityManager.close();
        }
    }
}

@@ -333,150 +333,26 @@
 nce.
-EntityTransaction;%0Aimport javax.persistence.PersistenceContext;%0Aimport javax.persistence.EntityExistsException;%0Aimport javax.persistence.Query
+PersistenceContext
 ;%0Aim
@@ -388,16 +388,44 @@
 andles;%0A
+import java.util.ArrayList;%0A
 import j
@@ -777,1850 +777,131 @@
     
-    final int userId;%0A        try %7B%0A            final Query query = entityManager.createQuery(%22Select u.id FROM User u WHERE u.email = :email%22);%0A            query.setParameter(%22email%22, userEmail);%0A            final List resultList = query.getResultList();%0A            if (resultList.isEmpty()) %7B%0A                throw new IllegalArgumentException(%22User does not exist%22);%0A            %7D%0A            userId = (int) resultList.get(0);%0A%0A            final ContactsEntity contactsEntity = new ContactsEntity();%0A            contactsEntity.setUserId(userId);%0A            contactsEntity.setEmail(contact.getEmail());%0A            contactsEntity.setFirstname(contact.getFirstName());%0A            contactsEntity.setLastname(contact.getLastName());%0A%0A            final EntityTransaction entityTransaction = entityManager.getTransaction();%0A            try %7B%0A                entityTransaction.begin();%0A                entityManager.persist(contactsEntity);%0A                entityTransaction.commit();%0A            %7D catch (final EntityExistsException entityExistsException) %7B%0A                LOGGER.warn(%22Entity already exists: %22 + contactsEntity);%0A                entityTransaction.rollback();%0A                throw entityExistsException;%0A            %7D%0A        %7D catch (final Exception ex) %7B%0A            LOGGER.warn(%22%22, ex);%0A            throw ex;%0A        %7D finally %7B%0A            entityManager.close();%0A        %7D%0A%0A    %7D%0A%0A    public List%3CContactsEntity%3E getContactsByEmail(final String email) %7B%0A        try %7B%0A            final Query query = entityManager%0A                    .createQuery(%22Select contacts FROM ContactsEntity contacts JOIN User user ON contacts.userId = user.id WHERE user.email = :email%22);%0A            query.setParameter(%22email%22, email);%0A            return query.getResultList();%0A        %7D finally %7B%0A            entityManager.close();%0A        %7D
+%7D%0A%0A    public List%3CContactsEntity%3E getContactsByEmail(final String email) %7B%0A        return new ArrayList%3CContactsEntity%3E();
 %0A   

create and get contacts have dummy implementation because of changes in the database