package io.teiler.api.service;

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

import io.teiler.server.persistence.repositories.CompensationRepository;
import io.teiler.server.util.exceptions.TransactionNotFoundException;

@Service
public class CompensationUtil {
    
    @Autowired
    private CompensationRepository compensationRepository;
    
    public CompensationUtil() { /* intentionally empty */ }

    /**
     * Checks whether a Compensation exists within a Group.
     * 
     * @param groupId Id of the Group
     * @param compensationId Id of the Compensation
     * @throws TransactionNotFoundException Compensation does not exists within Group
     */
    public void checkCompensationBelongsToThisGroup(String groupId, int compensationId) throws TransactionNotFoundException {
        if (compensationRepository.getByGroupIdAndCompensationId(groupId, compensationId) == null) {
            throw new TransactionNotFoundException();
        }
    }

    /**
     * Checks whether a Compensation exists.
     *
     * @param compensationId Id of the Compensation
     * @throws TransactionNotFoundException Compensation does not exist
     */
    public void checkCompensationExists(int compensationId) throws TransactionNotFoundException {
        if (compensationRepository.getById(compensationId) == null) {
            throw new TransactionNotFoundException();
        }
    }
    
}

@@ -209,16 +209,154 @@
 sitory;%0A
+import io.teiler.server.persistence.repositories.ProfiteerRepository;%0Aimport io.teiler.server.util.exceptions.ProfiteerNotFoundException;%0A
 import i
@@ -539,24 +539,97 @@
 itory;%0A    %0A
+    @Autowired%0A    private ProfiteerRepository profiteerRepository;%0A    %0A
     public C
@@ -1670,10 +1670,608 @@
  %7D%0A    %0A
+    /**%0A     * Checks whether a Profiteer-Person exists within an Compensation.%0A     * %0A     * @param compensationId Id of the Compensation%0A     * @param profiteerPersonId Id of the Profiteer-Person%0A     * @throws ProfiteerNotFoundException Profiteer does not exist%0A     */%0A    public void checkProfiteerExistsInThisCompensation(int compensationId, int profiteerPersonId) throws ProfiteerNotFoundException %7B%0A        if (profiteerRepository.getByTransactionIdAndProfiteerPersonId(compensationId, profiteerPersonId) == null) %7B%0A            throw new ProfiteerNotFoundException();%0A        %7D%0A    %7D%0A    %0A
 %7D%0A

add method to check if a profiteer already exists in a compensation (#164)