#22 Add transaction upload
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package de.financer.controller;
|
||||
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.dto.CreateUploadedTransactionsRequestDto;
|
||||
import de.financer.dto.ExpensePeriodTotal;
|
||||
import de.financer.dto.SaveTransactionRequestDto;
|
||||
import de.financer.dto.SearchTransactionsResponseDto;
|
||||
@@ -12,6 +13,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@@ -164,4 +166,19 @@ public class TransactionController {
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
@PostMapping(value = "/transactions/upload")
|
||||
public ResponseEntity createTransaction(@RequestBody Collection<CreateUploadedTransactionsRequestDto> requestDtos) {
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("POST /transactions/upload got parameters: %s", requestDtos));
|
||||
}
|
||||
|
||||
final ResponseReason responseReason = this.transactionService.createTransactions(requestDtos);
|
||||
|
||||
if (LOGGER.isDebugEnabled()) {
|
||||
LOGGER.debug(String.format("POST /transactions/upload returns with %s", responseReason.name()));
|
||||
}
|
||||
|
||||
return responseReason.toResponseEntity();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +60,10 @@ public class RecurringTransactionService {
|
||||
return this.recurringTransactionRepository.findAllActive(LocalDate.now());
|
||||
}
|
||||
|
||||
protected Optional<RecurringTransaction> findById(Long id) {
|
||||
return this.recurringTransactionRepository.findById(id);
|
||||
}
|
||||
|
||||
public Iterable<RecurringTransaction> getAllForAccount(String accountKey) {
|
||||
final Account account = this.accountService.getAccountByKey(accountKey);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@ package de.financer.service;
|
||||
import de.financer.ResponseReason;
|
||||
import de.financer.config.FinancerConfig;
|
||||
import de.financer.dba.TransactionRepository;
|
||||
import de.financer.dto.CreateUploadedTransactionsRequestDto;
|
||||
import de.financer.dto.ExpensePeriodTotal;
|
||||
import de.financer.dto.Order;
|
||||
import de.financer.dto.SearchTransactionsResponseDto;
|
||||
@@ -44,6 +45,9 @@ public class TransactionService {
|
||||
@Autowired
|
||||
private TransactionRepository transactionRepository;
|
||||
|
||||
@Autowired
|
||||
private RecurringTransactionService recurringTransactionService;
|
||||
|
||||
@Autowired
|
||||
private FinancerConfig financerConfig;
|
||||
|
||||
@@ -411,4 +415,54 @@ public class TransactionService {
|
||||
throw new FinancerServiceException(ResponseReason.FQL_MALFORMED);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* This method creates multiple transactions in one batch based on the given parameters. In case of invalid
|
||||
* parameters the creation aborts and the first issue found is returned.
|
||||
*
|
||||
* @param requestDtos parameters to create the transactions for
|
||||
* @return {@link ResponseReason#CREATED CREATED} in case of success, another instance of {@link ResponseReason}
|
||||
* otherwise. Never <code>null</code>
|
||||
*/
|
||||
@Transactional(propagation = Propagation.REQUIRED)
|
||||
public ResponseReason createTransactions(Collection<CreateUploadedTransactionsRequestDto> requestDtos) {
|
||||
requestDtos.stream().forEach(dto -> {
|
||||
final ResponseReason responseReason;
|
||||
|
||||
if(StringUtils.isNotEmpty(dto.getRecurringTransactionId())) {
|
||||
final RecurringTransaction recurringTransaction =
|
||||
this.recurringTransactionService.findById(Long.valueOf(dto.getRecurringTransactionId()))
|
||||
.orElseThrow(() -> new FinancerServiceException(
|
||||
ResponseReason.RECURRING_TRANSACTION_NOT_FOUND));
|
||||
|
||||
responseReason = this.createTransaction(dto.getFromAccountKey(),
|
||||
dto.getToAccountKey(),
|
||||
Long.valueOf(dto.getAmount()),
|
||||
dto.getDate(),
|
||||
dto.getDescription(),
|
||||
recurringTransaction,
|
||||
dto.getTaxRelevant(),
|
||||
dto.getFileName(),
|
||||
dto.getFileContent()
|
||||
);
|
||||
}
|
||||
else {
|
||||
responseReason = this.createTransaction(dto.getFromAccountKey(),
|
||||
dto.getToAccountKey(),
|
||||
Long.valueOf(dto.getAmount()),
|
||||
dto.getDate(),
|
||||
dto.getDescription(),
|
||||
dto.getTaxRelevant(),
|
||||
dto.getFileName(),
|
||||
dto.getFileContent()
|
||||
);
|
||||
}
|
||||
|
||||
if (responseReason != ResponseReason.CREATED) {
|
||||
throw new FinancerServiceException(responseReason);
|
||||
}
|
||||
});
|
||||
|
||||
return ResponseReason.CREATED;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user