refactor code, add more flexibility for filtering transactions by periods of time
This commit is contained in:
@@ -5,14 +5,16 @@ import com.faf223.expensetrackerfaf.repository.CredentialRepository;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
@Service
|
||||
@RequiredArgsConstructor
|
||||
public class CredentialService {
|
||||
|
||||
private final CredentialRepository credentialRepository;
|
||||
|
||||
public Credential findByEmail(String email) {
|
||||
return credentialRepository.findByEmail(email).orElse(null);
|
||||
public Optional<Credential> findByEmail(String email) {
|
||||
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.userdetails.UserDetails;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.time.LocalDate;
|
||||
import java.time.Month;
|
||||
|
||||
@@ -56,15 +56,7 @@ public class IncomeService implements ITransactionService {
|
||||
|
||||
@Override
|
||||
public List<Income> getTransactionsByDate(LocalDate date, String email) {
|
||||
return getTransactionsByDate(date)
|
||||
.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();
|
||||
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByDate(date), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -74,15 +66,7 @@ public class IncomeService implements ITransactionService {
|
||||
|
||||
@Override
|
||||
public List<Income> getTransactionsByMonth(Month month, String email) {
|
||||
return getTransactionsByMonth(month)
|
||||
.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();
|
||||
return (List<Income>) transactionFilter.filterByEmail(getTransactionsByMonth(month), email);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user