package io.teiler.server.persistence.repositories;

import com.querydsl.jpa.impl.JPAQuery;
import io.teiler.server.dto.Person;
import io.teiler.server.persistence.entities.GroupEntity;
import io.teiler.server.persistence.entities.PersonEntity;
import io.teiler.server.persistence.entities.QPersonEntity;
import java.util.List;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

/**
 * Provides database-related operations for Groups.
 * 
 * @author lroellin
 */
@Repository
public class PersonRepository {

    @Autowired
    private EntityManager entityManager;

    @Autowired
    private GroupRepository groupRepository;

    /**
     * Creates a new Person and returns it.
     * 
     * @param groupId Id of the Group
     * @param person Name of the Group
     * @return {@link GroupEntity}
     */
    @Transactional
    public PersonEntity create(String groupId, Person person) {
        GroupEntity groupEntity = groupRepository.getGroupById(groupId);
        PersonEntity personEntity = new PersonEntity(person);
        groupEntity.addPerson(personEntity);
        entityManager.persist(personEntity);
        entityManager.persist(groupEntity);
        return personEntity;
    }

    /**
     * Returns a {@link PersonEntity} with the given name and Group-Id.
     * 
     * @param groupId Id of the Group
     * @param name Name of the Person
     * @return {@link PersonEntity}
     */
    public PersonEntity getByName(String groupId, String name) {
        return new JPAQuery<PersonEntity>(entityManager).from(QPersonEntity.personEntity)
            .where(QPersonEntity.personEntity.groupId.eq(groupId)
            .and(QPersonEntity.personEntity.name.eq(name)))
            .fetchOne();
    }

    /**
     * Returns a {@link List} of {@link PersonEntity} in the Group with the given Id.
     * 
     * @param groupId Id of the Group
     * @param limit Maximum amount of people to fetch
     * @return {@link List} of {@link PersonEntity}
     */
    public List<PersonEntity> getPeople(String groupId, long limit) {
        return new JPAQuery<PersonEntity>(entityManager).from(QPersonEntity.personEntity)
            .where(QPersonEntity.personEntity.groupId.eq(groupId))
            .limit(limit)
            .orderBy(QPersonEntity.personEntity.id.asc())
            .fetch();
    }

    /**
     * Returns a {@link PersonEntity} with the given Group- and Person-Id.
     * 
     * @param groupId Id of the Group
     * @param personId Id of the Person
     * @return {@link PersonEntity}
     */
    public PersonEntity getByGroupAndPersonId(String groupId, int personId) {
        return new JPAQuery<PersonEntity>(entityManager).from(QPersonEntity.personEntity)
            .where(QPersonEntity.personEntity.groupId.eq(groupId)
            .and(QPersonEntity.personEntity.id.eq(personId)))
            .fetchOne();
    }

    /**
     * Returns the {@link PersonEntity} with the given Id.
     * 
     * @param personId Id of the Person
     * @return {@link PersonEntity}
     */
    public PersonEntity getById(int personId) {
        return new JPAQuery<PersonEntity>(entityManager).from(QPersonEntity.personEntity)
            .where(QPersonEntity.personEntity.id.eq(personId))
            .fetchOne();
    }

    /**
     * Updates a already persisted Person with the given values.
     * 
     * @param personId Id of the Person
     * @param changedPerson {@link Person} containing the new values
     * @return {@link PersonEntity} containing the new values
     */
    @Transactional
    public PersonEntity editPerson(int personId, Person changedPerson) {
        PersonEntity person = getById(personId);
        person.setName(changedPerson.getName());
        entityManager.persist(person);
        return person;
    }

    /**
     * Deletes the Person with the given Id.
     * 
     * @param personId Id of the Person
     */
    @Transactional
    public void deletePerson(int personId) {
        PersonEntity person = getById(personId);
        entityManager.remove(person);
    }
    
}

@@ -45,16 +45,235 @@
 ories;%0A%0A
+import java.util.List;%0A%0Aimport javax.persistence.EntityManager;%0Aimport javax.transaction.Transactional;%0A%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.stereotype.Repository;%0A%0A
 import c
@@ -303,16 +303,17 @@
 AQuery;%0A
+%0A
 import i
@@ -520,224 +520,8 @@
 ity;
-%0Aimport java.util.List;%0Aimport javax.persistence.EntityManager;%0Aimport javax.transaction.Transactional;%0Aimport org.springframework.beans.factory.annotation.Autowired;%0Aimport org.springframework.stereotype.Repository;
 %0A%0A/*
@@ -598,16 +598,36 @@
 roellin%0A
+ * @author pbaechli%0A
  */%0A@Rep
@@ -3766,33 +3766,41 @@
    PersonEntity 
-p
+existingP
 erson = getById(
@@ -3822,49 +3822,204 @@
     
-person.setName(changedPerson.getName
+%0A        PersonEntity updatedPerson = new PersonEntity(changedPerson);%0A        updatedPerson.setId(existingPerson.getId());%0A        updatedPerson.setGroupId(existingPerson.getGroupId
 ());%0A
+        %0A
     
@@ -4032,33 +4032,38 @@
 tityManager.
-persist(p
+merge(updatedP
 erson);%0A    
@@ -4062,32 +4062,48 @@
 n);%0A        
-return p
+%0A        return updatedP
 erson;%0A    %7D
@@ -4095,32 +4095,36 @@
 edPerson;%0A    %7D%0A
+    
 %0A    /**%0A     * 

fix edit method in person repository