-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTransactionService.java
More file actions
141 lines (116 loc) · 5.78 KB
/
TransactionService.java
File metadata and controls
141 lines (116 loc) · 5.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package fun.trackmoney.transaction.service;
import fun.trackmoney.account.entity.AccountEntity;
import fun.trackmoney.account.mapper.AccountMapper;
import fun.trackmoney.account.service.AccountService;
import fun.trackmoney.category.entity.CategoryEntity;
import fun.trackmoney.category.service.CategoryService;
import fun.trackmoney.enums.TransactionType;
import fun.trackmoney.transaction.dto.BillResponseDTO;
import fun.trackmoney.transaction.dto.CreateTransactionDTO;
import fun.trackmoney.transaction.dto.TransactionResponseDTO;
import fun.trackmoney.transaction.dto.TransactionUpdateDTO;
import fun.trackmoney.transaction.entity.TransactionEntity;
import fun.trackmoney.transaction.exception.TransactionNotFoundException;
import fun.trackmoney.transaction.mapper.TransactionMapper;
import fun.trackmoney.transaction.repository.TransactionRepository;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
import java.util.Objects;
@Service
public class TransactionService {
private final TransactionRepository transactionRepository;
private final TransactionMapper transactionMapper;
private final AccountService accountService;
private final AccountMapper accountMapper;
private final CategoryService categoryService;
public TransactionService(TransactionRepository transactionRepository,
TransactionMapper transactionMapper,
AccountService accountService,
AccountMapper accountMapper,
CategoryService categoryService) {
this.transactionRepository = transactionRepository;
this.transactionMapper = transactionMapper;
this.accountService = accountService;
this.accountMapper = accountMapper;
this.categoryService = categoryService;
}
public TransactionResponseDTO createTransaction(CreateTransactionDTO transactionDTO) {
AccountEntity account = accountMapper.accountResponseToEntity(
accountService.findAccountById(transactionDTO.accountId()));
CategoryEntity category = categoryService.findById(transactionDTO.categoryId());
TransactionEntity transactionEntity = transactionMapper.createTransactionToEntity(transactionDTO);
transactionEntity.setAccount(account);
transactionEntity.setCategory(category);
var response = transactionMapper.toResponseDTO(transactionRepository.save(transactionEntity));
accountService.updateAccountBalance(transactionDTO.amount(), account.getAccountId(),
TransactionType.INCOME.equals(transactionDTO.transactionType()));
return response;
}
public List<TransactionResponseDTO> findAllTransaction() {
return transactionMapper.toResponseDTOList(transactionRepository.findAll());
}
public TransactionResponseDTO findById(Integer id) {
return transactionMapper.toResponseDTO(transactionRepository.findById(id)
.orElseThrow(() -> new TransactionNotFoundException("Transaction not found.")));
}
public TransactionResponseDTO update(Integer id, TransactionUpdateDTO dto) {
TransactionEntity transaction = transactionRepository.findById(id)
.orElseThrow(() -> new TransactionNotFoundException("Transaction not found."));
transaction.setAmount(dto.amount());
transaction.setDescription(dto.description());
transaction.setCategory(categoryService.findById(dto.categoryId()));
transaction.setAccount(accountMapper.accountResponseToEntity(accountService.findAccountById(dto.accountId())));
return transactionMapper.toResponseDTO(transactionRepository.save(transaction));
}
public void delete(Integer id) {
transactionRepository.deleteById(id);
}
public BigDecimal getIncome(Integer accountId) {
return transactionRepository.findAllByAccountId(accountId)
.stream()
.filter(t -> TransactionType.INCOME.equals(t.getTransactionType()))
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
public BigDecimal getExpense(Integer accountId) {
return transactionRepository.findAllByAccountId(accountId)
.stream()
.filter(t -> TransactionType.EXPENSE.equals(t.getTransactionType()))
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
public BillResponseDTO getBill(Integer id) {
var result = transactionRepository.findAllByAccountId(id);
BigDecimal totalBillsBeforeToday = result.stream()
.filter(transaction ->
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isBefore(LocalDate.now())
)
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalUpComing = result.stream()
.filter(transaction ->
transaction.getCategory().getName().equalsIgnoreCase("bill") &&
transaction.getTransactionDate().toLocalDateTime().toLocalDate().isAfter(LocalDate.now())
)
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
BigDecimal totalBueSoon = result.stream()
.filter(transaction -> {
LocalDate date = transaction.getTransactionDate().toLocalDateTime().toLocalDate();
return transaction.getCategory().getName().equalsIgnoreCase("bill")
&& !date.isBefore(LocalDate.now())
&& !date.isAfter(LocalDate.now().plusDays(7));
})
.map(TransactionEntity::getAmount)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
return new BillResponseDTO(totalBillsBeforeToday, totalUpComing, totalBueSoon);
}
}