refactor code, add more flexibility for filtering transactions by periods of time
This commit is contained in:
@@ -1,8 +1,7 @@
|
|||||||
package com.faf223.expensetrackerfaf.config;
|
package com.faf223.expensetrackerfaf.config;
|
||||||
|
|
||||||
import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
|
||||||
import com.faf223.expensetrackerfaf.repository.UserRepository;
|
|
||||||
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
import com.faf223.expensetrackerfaf.security.PersonDetails;
|
||||||
|
import com.faf223.expensetrackerfaf.service.CredentialService;
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
@@ -19,12 +18,11 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class ApplicationConfig {
|
public class ApplicationConfig {
|
||||||
|
|
||||||
private final UserRepository userRepository;
|
private final CredentialService credentialService;
|
||||||
private final CredentialRepository credentialRepository;
|
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public UserDetailsService userDetailsService() {
|
public UserDetailsService userDetailsService() {
|
||||||
return username -> new PersonDetails(credentialRepository.findByEmail(username).orElseThrow((() -> new UsernameNotFoundException("User not found"))));
|
return username -> new PersonDetails(credentialService.findByEmail(username).orElseThrow((() -> new UsernameNotFoundException("User not found"))));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package com.faf223.expensetrackerfaf.controller;
|
|||||||
|
|
||||||
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
import com.faf223.expensetrackerfaf.dto.ExpenseCreationDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
import com.faf223.expensetrackerfaf.dto.ExpenseDTO;
|
||||||
import com.faf223.expensetrackerfaf.dto.IncomeDTO;
|
|
||||||
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
import com.faf223.expensetrackerfaf.dto.mappers.ExpenseMapper;
|
||||||
import com.faf223.expensetrackerfaf.model.Expense;
|
import com.faf223.expensetrackerfaf.model.Expense;
|
||||||
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
import com.faf223.expensetrackerfaf.model.ExpenseCategory;
|
||||||
@@ -23,6 +22,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -97,8 +97,9 @@ public class ExpenseController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/personal-expenses")
|
@GetMapping("/personal-expenses")
|
||||||
|
@Transactional(readOnly = true)
|
||||||
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
public ResponseEntity<List<ExpenseDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
||||||
@RequestParam Optional<Month> month,
|
@RequestParam Optional<Integer> month,
|
||||||
@RequestParam Optional<Integer> startYear,
|
@RequestParam Optional<Integer> startYear,
|
||||||
@RequestParam Optional<Integer> endYear,
|
@RequestParam Optional<Integer> endYear,
|
||||||
@RequestParam Optional<String> lastUnit) {
|
@RequestParam Optional<String> lastUnit) {
|
||||||
@@ -113,7 +114,7 @@ public class ExpenseController {
|
|||||||
if(date.isPresent())
|
if(date.isPresent())
|
||||||
expenses = expenseService.getTransactionsByDate(date.get(), email).stream().map(expenseMapper::toDto).toList();
|
expenses = expenseService.getTransactionsByDate(date.get(), email).stream().map(expenseMapper::toDto).toList();
|
||||||
else if(month.isPresent())
|
else if(month.isPresent())
|
||||||
expenses = expenseService.getTransactionsByMonth(month.get(), email).stream().map(expenseMapper::toDto).toList();
|
expenses = expenseService.getTransactionsByMonth(Month.of(month.get()), email).stream().map(expenseMapper::toDto).toList();
|
||||||
else if(startYear.isPresent() && endYear.isPresent())
|
else if(startYear.isPresent() && endYear.isPresent())
|
||||||
expenses = expenseService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(expenseMapper::toDto).toList();
|
expenses = expenseService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(expenseMapper::toDto).toList();
|
||||||
else if(lastUnit.isPresent()) {
|
else if(lastUnit.isPresent()) {
|
||||||
|
|||||||
@@ -22,6 +22,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
|
|||||||
import org.springframework.security.core.Authentication;
|
import org.springframework.security.core.Authentication;
|
||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
import org.springframework.validation.BindingResult;
|
import org.springframework.validation.BindingResult;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
@@ -96,8 +97,9 @@ public class IncomeController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/personal-incomes")
|
@GetMapping("/personal-incomes")
|
||||||
public ResponseEntity<List<IncomeDTO>> getExpensesByUser(@RequestParam Optional<LocalDate> date,
|
@Transactional(readOnly = true)
|
||||||
@RequestParam Optional<Month> month,
|
public ResponseEntity<List<IncomeDTO>> getIncomesByUser(@RequestParam Optional<LocalDate> date,
|
||||||
|
@RequestParam Optional<Integer> month,
|
||||||
@RequestParam Optional<Integer> startYear,
|
@RequestParam Optional<Integer> startYear,
|
||||||
@RequestParam Optional<Integer> endYear,
|
@RequestParam Optional<Integer> endYear,
|
||||||
@RequestParam Optional<String> lastUnit) {
|
@RequestParam Optional<String> lastUnit) {
|
||||||
@@ -112,7 +114,7 @@ public class IncomeController {
|
|||||||
if(date.isPresent())
|
if(date.isPresent())
|
||||||
incomes = incomeService.getTransactionsByDate(date.get(), email).stream().map(incomeMapper::toDto).toList();
|
incomes = incomeService.getTransactionsByDate(date.get(), email).stream().map(incomeMapper::toDto).toList();
|
||||||
else if(month.isPresent())
|
else if(month.isPresent())
|
||||||
incomes = incomeService.getTransactionsByMonth(month.get(), email).stream().map(incomeMapper::toDto).toList();
|
incomes = incomeService.getTransactionsByMonth(Month.of(month.get()), email).stream().map(incomeMapper::toDto).toList();
|
||||||
else if(startYear.isPresent() && endYear.isPresent())
|
else if(startYear.isPresent() && endYear.isPresent())
|
||||||
incomes = incomeService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(incomeMapper::toDto).toList();
|
incomes = incomeService.getYearIntervalTransactions(email, startYear.get(), endYear.get()).stream().map(incomeMapper::toDto).toList();
|
||||||
else if(lastUnit.isPresent()) {
|
else if(lastUnit.isPresent()) {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import com.faf223.expensetrackerfaf.model.User;
|
|||||||
import org.springframework.data.jpa.repository.JpaRepository;
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
import org.springframework.data.jpa.repository.query.Procedure;
|
import org.springframework.data.jpa.repository.query.Procedure;
|
||||||
import org.springframework.stereotype.Repository;
|
import org.springframework.stereotype.Repository;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -16,15 +17,19 @@ public interface IncomeRepository extends JpaRepository<Income, Long> {
|
|||||||
|
|
||||||
List<Income> findByDateBetween(LocalDate start, LocalDate end);
|
List<Income> findByDateBetween(LocalDate start, LocalDate end);
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
@Procedure(procedureName = "get_incomes_by_month")
|
@Procedure(procedureName = "get_incomes_by_month")
|
||||||
List<Income> filterByMonth(int month);
|
List<Income> filterByMonth(int month);
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
@Procedure(procedureName = "get_last_week_incomes")
|
@Procedure(procedureName = "get_last_week_incomes")
|
||||||
List<Income> findLastWeek();
|
List<Income> findLastWeek();
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
@Procedure(procedureName = "get_last_month_incomes")
|
@Procedure(procedureName = "get_last_month_incomes")
|
||||||
List<Income> findLastMonth();
|
List<Income> findLastMonth();
|
||||||
|
|
||||||
|
@Transactional(readOnly = true)
|
||||||
@Procedure(procedureName = "get_incomes_by_year_interval")
|
@Procedure(procedureName = "get_incomes_by_year_interval")
|
||||||
List<Income> filterByYearInterval(int start, int end);
|
List<Income> filterByYearInterval(int start, int end);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,14 +5,16 @@ import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
|||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
public class CredentialService {
|
public class CredentialService {
|
||||||
|
|
||||||
private final CredentialRepository credentialRepository;
|
private final CredentialRepository credentialRepository;
|
||||||
|
|
||||||
public Credential findByEmail(String email) {
|
public Optional<Credential> findByEmail(String email) {
|
||||||
return credentialRepository.findByEmail(email).orElse(null);
|
return credentialRepository.findByEmail(email);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import org.springframework.security.core.Authentication;
|
|||||||
import org.springframework.security.core.context.SecurityContextHolder;
|
import org.springframework.security.core.context.SecurityContextHolder;
|
||||||
import org.springframework.security.core.userdetails.UserDetails;
|
import org.springframework.security.core.userdetails.UserDetails;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.Month;
|
import java.time.Month;
|
||||||
|
|||||||
@@ -56,15 +56,7 @@ public class IncomeService implements ITransactionService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Income> getTransactionsByDate(LocalDate date, String email) {
|
public List<Income> getTransactionsByDate(LocalDate date, String email) {
|
||||||
return getTransactionsByDate(date)
|
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByDate(date), email);
|
||||||
.stream()
|
|
||||||
.filter(transaction -> {
|
|
||||||
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
|
||||||
if(credential.isEmpty())
|
|
||||||
throw new UserNotFoundException("The user has not been found");
|
|
||||||
return credential.get().getUser().equals(transaction.getUser());
|
|
||||||
})
|
|
||||||
.toList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -74,15 +66,7 @@ public class IncomeService implements ITransactionService {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<Income> getTransactionsByMonth(Month month, String email) {
|
public List<Income> getTransactionsByMonth(Month month, String email) {
|
||||||
return getTransactionsByMonth(month)
|
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByMonth(month), email);
|
||||||
.stream()
|
|
||||||
.filter(transaction -> {
|
|
||||||
Optional<Credential> credential = credentialRepository.findByEmail(email);
|
|
||||||
if(credential.isEmpty())
|
|
||||||
throw new UserNotFoundException("The user has not been found");
|
|
||||||
return credential.get().getUser().equals(transaction.getUser());
|
|
||||||
})
|
|
||||||
.toList();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ import lombok.RequiredArgsConstructor;
|
|||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@RequiredArgsConstructor
|
@RequiredArgsConstructor
|
||||||
@@ -19,10 +20,10 @@ public class TransactionFilter {
|
|||||||
return transactions
|
return transactions
|
||||||
.stream()
|
.stream()
|
||||||
.filter(transaction -> {
|
.filter(transaction -> {
|
||||||
Credential credential = credentialService.findByEmail(email);
|
Optional<Credential> credential = credentialService.findByEmail(email);
|
||||||
if(credential == null)
|
if(credential.isEmpty())
|
||||||
throw new UserNotFoundException("The user has not been found");
|
throw new UserNotFoundException("The user has not been found");
|
||||||
return credential.getUser().equals(transaction.getUser());
|
return credential.get().getUser().equals(transaction.getUser());
|
||||||
})
|
})
|
||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user