added date filtering for transactions, refactor code
This commit is contained in:
@@ -2,6 +2,7 @@ package com.faf223.expensetrackerfaf.controller;
|
||||
|
||||
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
||||
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
||||
import com.faf223.expensetrackerfaf.model.Expense;
|
||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||
@@ -97,24 +98,34 @@ public class ExpenseController {
|
||||
|
||||
@GetMapping("/personal-expenses")
|
||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
||||
@RequestParam Optional<Month> month) {
|
||||
@RequestParam Optional<Month> month,
|
||||
@RequestParam Optional<Integer> startYear,
|
||||
@RequestParam Optional<Integer> endYear,
|
||||
@RequestParam Optional<String> lastUnit) {
|
||||
|
||||
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
||||
|
||||
if (authentication != null && authentication.getPrincipal() instanceof UserDetails userDetails) {
|
||||
|
||||
String email = userDetails.getUsername();
|
||||
List<ExpenseDTO> expenses;
|
||||
List<ExpenseDTO> expenses = Collections.emptyList();
|
||||
|
||||
expenses = date.map(localDate -> expenseService.getTransactionsByDate(localDate, email).stream().map(expenseMapper::toDto).toList())
|
||||
.orElseGet(() -> month.map(value -> expenseService.getTransactionsByMonth(value, email).stream().map(expenseMapper::toDto).toList())
|
||||
.orElseGet(() -> expenseService.getTransactionsByEmail(email).stream().map(expenseMapper::toDto).toList()));
|
||||
if(date.isPresent())
|
||||
expenses = expenseService.getTransactionsByDate(date.get(), email).stream().map(expenseMapper::toDto).toList();
|
||||
else if(month.isPresent())
|
||||
expenses = expenseService.getTransactionsByMonth(month.get(), email).stream().map(expenseMapper::toDto).toList();
|
||||
else if(startYear.isPresent() && endYear.isPresent())
|
||||
expenses = expenseService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(expenseMapper::toDto).toList();
|
||||
else if(lastUnit.isPresent()) {
|
||||
|
||||
if(lastUnit.get().equalsIgnoreCase("week"))
|
||||
expenses = expenseService.getLastWeekTransactions(email).stream().map(expenseMapper::toDto).toList();
|
||||
else if(lastUnit.get().equalsIgnoreCase("month"))
|
||||
expenses = expenseService.getLastMonthTransactions(email).stream().map(expenseMapper::toDto).toList();
|
||||
|
||||
if (!expenses.isEmpty()) {
|
||||
return ResponseEntity.ok(expenses);
|
||||
} else {
|
||||
return ResponseEntity.ok(Collections.emptyList());
|
||||
}
|
||||
|
||||
return ResponseEntity.ok(expenses);
|
||||
}
|
||||
|
||||
throw new TransactionsNotFoundException("The expenses have not been found");
|
||||
|
||||
Reference in New Issue
Block a user