package io.teiler.server.persistence.repositories;

import com.querydsl.jpa.impl.JPAQuery;
import io.teiler.server.dto.Debt;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.util.List;

/**
 * Provides database-related operations for debts.
 *
 * @author dthoma
 */
@Repository
public class DebtRepository {

    @Autowired
    private EntityManager entityManager;

    /**
     * Returns a {@link List} of {@link Debt} in the Group with the given Id.
     *
     * @param groupId Id of the Group
     * @return {@link List} of {@link Debt}
     */
    public List<Debt> get(String groupId) {
        Query q = entityManager.createNativeQuery("SELECT a.id, a.version, a.firstname, a.lastname FROM Author a", Debt.class);
        
    }

}

@@ -56,74 +56,57 @@
 ort 
-com.querydsl.jpa.impl.JPAQuery;%0Aimport io.teiler.server.dto.Debt;%0A
+io.teiler.server.persistence.entities.DebtEntity;
 %0Aimp
@@ -544,16 +544,22 @@
 ink Debt
+Entity
 %7D in the
@@ -671,16 +671,22 @@
 ink Debt
+Entity
 %7D%0A     *
@@ -707,16 +707,22 @@
 ist%3CDebt
+Entity
 %3E get(St
@@ -752,16 +752,20 @@
  Query q
+uery
  = entit
@@ -795,77 +795,1414 @@
 ery(
-%22SELECT a.id, a.version, a.firstname, a.lastname FROM Author a%22, Debt
+%0A                %22SELECT%5Cn%22 +%0A                        %22    person.id AS person,%5Cn%22 +%0A                        %22    transactions.credit - profiteers.debt AS balance%5Cn%22 +%0A                        %22FROM person%5Cn%22 +%0A                        %22LEFT JOIN (%5Cn%22 +%0A                        %22    SELECT %5Cn%22 +%0A                        %22        transaction.payer,%5Cn%22 +%0A                        %22        SUM(profiteer.share) AS credit%5Cn%22 +%0A                        %22    FROM transaction%5Cn%22 +%0A                        %22    LEFT JOIN profiteer ON profiteer.transaction=transaction.id%5Cn%22 +%0A                        %22    LEFT JOIN person ON person.id=profiteer.person%5Cn%22 +%0A                        %22    WHERE person.%5C%22group%5C%22='1'%5Cn%22 +%0A                        %22    GROUP BY transaction.payer%5Cn%22 +%0A                        %22) AS transactions ON transactions.payer=person.id%5Cn%22 +%0A                        %22LEFT JOIN (%5Cn%22 +%0A                        %22    SELECT %5Cn%22 +%0A                        %22        profiteer.person,%5Cn%22 +%0A                        %22        SUM(profiteer.share) AS debt%5Cn%22 +%0A                        %22    FROM profiteer%5Cn%22 +%0A                        %22    LEFT JOIN person ON person.id=profiteer.person%5Cn%22 +%0A                        %22    WHERE person.%5C%22group%5C%22='1'%5Cn%22 +%0A                        %22    GROUP BY profiteer.person%5Cn%22 +%0A                        %22) AS profiteers ON profiteers.person=person.id%22,%0A                DebtEntity
 .cla
@@ -2206,24 +2206,25 @@
 class);%0A
+%0A
         
 %0A    %7D%0A%0A
@@ -2215,16 +2215,65 @@
         
+return (List%3CDebtEntity%3E) query.getResultList();%0A
 %0A    %7D%0A%0A

add query