Refactor investment-related controllers and services; enhance historical rates functionality in investments.html
This commit is contained in:
parent
f5b3e49ca6
commit
052b3a5fed
Binary file not shown.
|
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 105 KiB |
|
|
@ -16,7 +16,7 @@ import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
|
||||||
import com.example.FinanceTracker.Components.BootstrapColours;
|
import com.example.FinanceTracker.Components.BootstrapColours;
|
||||||
import com.example.FinanceTracker.Components.PdfManager;
|
//import com.example.FinanceTracker.Components.PdfManager;
|
||||||
import com.example.FinanceTracker.DTOs.GroupedTransaction;
|
import com.example.FinanceTracker.DTOs.GroupedTransaction;
|
||||||
import com.example.FinanceTracker.DTOs.Home.OverviewCard;
|
import com.example.FinanceTracker.DTOs.Home.OverviewCard;
|
||||||
import com.example.FinanceTracker.Models.Transaction;
|
import com.example.FinanceTracker.Models.Transaction;
|
||||||
|
|
@ -26,8 +26,8 @@ import com.example.FinanceTracker.Services.TransactionService;
|
||||||
@Controller
|
@Controller
|
||||||
public class HomeController {
|
public class HomeController {
|
||||||
|
|
||||||
@Autowired
|
// @Autowired
|
||||||
private PdfManager pdfManager;
|
// private PdfManager pdfManager;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private TransactionService transactionsService;
|
private TransactionService transactionsService;
|
||||||
|
|
|
||||||
|
|
@ -2,20 +2,19 @@ package com.example.FinanceTracker.Controllers;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.time.LocalDateTime;
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.ui.Model;
|
import org.springframework.ui.Model;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.PathVariable;
|
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
|
||||||
import com.example.FinanceTracker.DTOs.Investments.CurrRate;
|
import com.example.FinanceTracker.DTOs.Investments.CurrRate;
|
||||||
import com.example.FinanceTracker.DTOs.Investments.Currency;
|
import com.example.FinanceTracker.DTOs.Investments.Currency;
|
||||||
import com.example.FinanceTracker.DTOs.Investments.ExchangeRate;
|
import com.example.FinanceTracker.DTOs.Investments.ExchangeRate;
|
||||||
import com.example.FinanceTracker.Services.InvestmentService;
|
import com.example.FinanceTracker.Services.InvestmentService;
|
||||||
|
import com.example.FinanceTracker.Services.TransactionService;
|
||||||
|
|
||||||
@Controller
|
@Controller
|
||||||
public class InvestmentsController {
|
public class InvestmentsController {
|
||||||
|
|
@ -23,9 +22,15 @@ public class InvestmentsController {
|
||||||
@Autowired
|
@Autowired
|
||||||
private InvestmentService investmentService;
|
private InvestmentService investmentService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private TransactionService transactionService;
|
||||||
|
|
||||||
@GetMapping("/investments")
|
@GetMapping("/investments")
|
||||||
public String investments(Model model) {
|
public String investments(Model model) {
|
||||||
|
|
||||||
|
// Heading
|
||||||
|
model.addAttribute("latestBalance", "Total Invested: " +transactionService.getTotalInvested() + "*");
|
||||||
|
|
||||||
// Create Currency Pills
|
// Create Currency Pills
|
||||||
List<CurrRate> allRates = investmentService.getCurrRates();
|
List<CurrRate> allRates = investmentService.getCurrRates();
|
||||||
model.addAttribute("currRates", allRates);
|
model.addAttribute("currRates", allRates);
|
||||||
|
|
@ -54,12 +59,21 @@ public class InvestmentsController {
|
||||||
return "investments";
|
return "investments";
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping("/historicalRatesWeekly")
|
@GetMapping("/historicalRates")
|
||||||
public String showHistoricalRatesWeek(Model model,
|
public String getHistoricalRates(
|
||||||
@RequestParam("historicalRatesWeekly") boolean historicalRatesWeekly){
|
@RequestParam("duration") String durationCode,
|
||||||
|
@RequestParam("currencyChange") String currencyChange,
|
||||||
|
Model model) {
|
||||||
|
|
||||||
// historical rates chart
|
// historical rates chart
|
||||||
List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates("USD", 3).reversed();
|
int duration = 1;
|
||||||
|
if (durationCode.equals("MONTH"))
|
||||||
|
duration = 2;
|
||||||
|
if (durationCode.equals("YEAR"))
|
||||||
|
duration = 3;
|
||||||
|
|
||||||
|
List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates(currencyChange, duration)
|
||||||
|
.reversed();
|
||||||
|
|
||||||
// Get all the rates from the historical Exchange Rates
|
// Get all the rates from the historical Exchange Rates
|
||||||
ArrayList<BigDecimal> historicalRates = new ArrayList<>();
|
ArrayList<BigDecimal> historicalRates = new ArrayList<>();
|
||||||
|
|
@ -75,29 +89,4 @@ public class InvestmentsController {
|
||||||
return "investments :: HistoricalRatesChart";
|
return "investments :: HistoricalRatesChart";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@GetMapping("/changeHistoricalCurr")
|
|
||||||
public String changeHistoricalCurr(Model model,
|
|
||||||
@RequestParam("currencyChange") String currencyChange){
|
|
||||||
|
|
||||||
// historical rates chart
|
|
||||||
List<ExchangeRate> historicalExchangeRates = investmentService.getHistoricalRates(currencyChange, 3).reversed();
|
|
||||||
|
|
||||||
// Get all the rates from the historical Exchange Rates
|
|
||||||
ArrayList<BigDecimal> historicalRates = new ArrayList<>();
|
|
||||||
ArrayList<LocalDate> historicalRatesLabels = new ArrayList<>();
|
|
||||||
for (ExchangeRate exchangeRate : historicalExchangeRates) {
|
|
||||||
historicalRates.add(exchangeRate.getRate());
|
|
||||||
historicalRatesLabels.add(exchangeRate.getCreatedAt().toLocalDate());
|
|
||||||
}
|
|
||||||
|
|
||||||
model.addAttribute("historicalRates", historicalRates);
|
|
||||||
model.addAttribute("labels", historicalRatesLabels);
|
|
||||||
|
|
||||||
return "investments :: HistoricalRatesChart";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ public class Transaction {
|
||||||
private String description;
|
private String description;
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
private int category;
|
private String category;
|
||||||
|
|
||||||
@Column
|
@Column
|
||||||
private BigDecimal moneyIn;
|
private BigDecimal moneyIn;
|
||||||
|
|
|
||||||
|
|
@ -186,4 +186,12 @@ public interface TransactionRepo extends JpaRepository<Transaction, UUID> {
|
||||||
""", nativeQuery = true)
|
""", nativeQuery = true)
|
||||||
double getSavingRate();
|
double getSavingRate();
|
||||||
|
|
||||||
|
@Query(value = """
|
||||||
|
SELECT SUM(money_out) * -1
|
||||||
|
FROM transactions t
|
||||||
|
JOIN categories c ON t.category = c.id
|
||||||
|
WHERE c.category_name = 'Investments'
|
||||||
|
""", nativeQuery = true)
|
||||||
|
double getTotalInvested();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,5 @@
|
||||||
package com.example.FinanceTracker.Services;
|
package com.example.FinanceTracker.Services;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
|
@ -56,10 +55,14 @@ public class InvestmentService {
|
||||||
.body(new ParameterizedTypeReference<List<ExchangeRate>>() {
|
.body(new ParameterizedTypeReference<List<ExchangeRate>>() {
|
||||||
});
|
});
|
||||||
|
|
||||||
if (exchangeRate.get(0).getRate().compareTo(exchangeRate.get(1).getRate()) > 0) {
|
int changeFactor = exchangeRate.get(0).getRate().compareTo(exchangeRate.get(1).getRate());
|
||||||
|
|
||||||
|
if (changeFactor > 0) {
|
||||||
return "UP";
|
return "UP";
|
||||||
} else {
|
} else if (changeFactor < 0) {
|
||||||
return "DOWN";
|
return "DOWN";
|
||||||
|
}else {
|
||||||
|
return "NEUTRAL";
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -51,7 +51,7 @@ public class TransactionService {
|
||||||
return transactionRepo.searchTransactions(pageable, searchValue);
|
return transactionRepo.searchTransactions(pageable, searchValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getLatestBalance(){
|
public double getLatestBalance() {
|
||||||
return transactionRepo.getLatestBalance();
|
return transactionRepo.getLatestBalance();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -113,7 +113,7 @@ public class TransactionService {
|
||||||
return transactionRepo.getTotalFeesByYear(year);
|
return transactionRepo.getTotalFeesByYear(year);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getTotalFees(){
|
public double getTotalFees() {
|
||||||
return transactionRepo.getTotalFees();
|
return transactionRepo.getTotalFees();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -121,21 +121,24 @@ public class TransactionService {
|
||||||
// Other
|
// Other
|
||||||
// =============================================================
|
// =============================================================
|
||||||
|
|
||||||
|
|
||||||
public List<Object[]> getTotals() {
|
public List<Object[]> getTotals() {
|
||||||
return transactionRepo.getTotals();
|
return transactionRepo.getTotals();
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<QuartlyTotals> getQuarterlyTotals(int year){
|
public List<QuartlyTotals> getQuarterlyTotals(int year) {
|
||||||
return transactionRepo.getQuarterlyTotals(year);
|
return transactionRepo.getQuarterlyTotals(year);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getNetCashflow(){
|
public double getNetCashflow() {
|
||||||
return transactionRepo.getNetCashflow();
|
return transactionRepo.getNetCashflow();
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getSavingRate(){
|
public double getSavingRate() {
|
||||||
return transactionRepo.getSavingRate();
|
return transactionRepo.getSavingRate();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public double getTotalInvested() {
|
||||||
|
return transactionRepo.getTotalInvested();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,21 +1,20 @@
|
||||||
<span
|
<span th:fragment="curr-pill (currRate)" class="badge d-inline-flex align-items-center gap-1 p-2"
|
||||||
th:fragment="curr-pill (currRate)"
|
style="min-width: 100px;" th:classappend="
|
||||||
class="badge d-inline-flex align-items-center gap-1 p-2"
|
${currRate.change} == 'UP'
|
||||||
style="min-width: 100px;"
|
? ' border border-success text-success'
|
||||||
th:classappend="
|
: (${currRate.change} == 'DOWN'
|
||||||
${currRate.change} == 'UP'
|
? ' border border-danger text-danger'
|
||||||
? ' border border-success text-success'
|
: ' border border-warning text-warning')
|
||||||
: ' border border-danger text-danger'
|
|
||||||
">
|
">
|
||||||
|
|
||||||
<span
|
<span class="material-icons bg-gray" th:text="
|
||||||
class="material-icons bg-gray"
|
${currRate.change} == 'UP'
|
||||||
th:text="${currRate.change} == 'UP' ? 'trending_up' : 'trending_down'">
|
? 'trending_up'
|
||||||
|
: (${currRate.change} == 'DOWN'
|
||||||
|
? 'trending_down'
|
||||||
|
: 'trending_flat')
|
||||||
|
">
|
||||||
</span>
|
</span>
|
||||||
<span class="fw-semibold text-dark" th:text="${currRate.ISO}"></span>
|
<span class="fw-semibold text-dark" th:text="${currRate.ISO}"></span>
|
||||||
<span th:text="${currRate.value}"></span>
|
<span th:text="${currRate.value}"></span>
|
||||||
</span>
|
</span>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -134,37 +134,43 @@
|
||||||
<div class="col-lg-4 ">
|
<div class="col-lg-4 ">
|
||||||
<span class="fw-bold text-primary mb-1">Historical Rates</span>
|
<span class="fw-bold text-primary mb-1">Historical Rates</span>
|
||||||
</div>
|
</div>
|
||||||
<div class="col-lg-4 mw-50">
|
<form id="historicalRatesControls" >
|
||||||
<div class="btn-group d-flex justify-content-center mb-1" role="group"
|
|
||||||
aria-label="Time range">
|
<!-- Duration Radios -->
|
||||||
<input th:hx-get="@{/historicalRatesWeekly}" hx-trigger="click once"
|
<div class="btn-group d-flex justify-content-center mb-1" role="group">
|
||||||
hx-swap="outerHTML" hx-target="#HistoricalRatesChartContainer"
|
<input type="radio" name="duration" value="WEEK" class="btn-check"
|
||||||
type="radio" class="btn-check" name="historicalRatesWeekly"
|
id="option1" checked hx-get="/historicalRates" hx-trigger="change"
|
||||||
id="option1" autocomplete="off" checked>
|
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
|
||||||
|
hx-include="#historicalRatesControls">
|
||||||
<label class="btn btn-secondary" for="option1">Week</label>
|
<label class="btn btn-secondary" for="option1">Week</label>
|
||||||
|
|
||||||
<input type="radio" class="btn-check" name="options" id="option2"
|
<input type="radio" name="duration" value="MONTH" class="btn-check"
|
||||||
autocomplete="off">
|
id="option2" hx-get="/historicalRates" hx-trigger="change"
|
||||||
|
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
|
||||||
|
hx-include="#historicalRatesControls">
|
||||||
<label class="btn btn-secondary" for="option2">Month</label>
|
<label class="btn btn-secondary" for="option2">Month</label>
|
||||||
|
|
||||||
<input type="radio" class="btn-check" name="options" id="option3"
|
<input type="radio" name="duration" value="YEAR" class="btn-check"
|
||||||
autocomplete="off">
|
id="option3" hx-get="/historicalRates" hx-trigger="change"
|
||||||
|
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
|
||||||
|
hx-include="#historicalRatesControls">
|
||||||
<label class="btn btn-secondary" for="option3">Year</label>
|
<label class="btn btn-secondary" for="option3">Year</label>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-lg-4 d-flex justify-content-end">
|
<!-- Currency Select -->
|
||||||
<select class="form-select" name="currencyChange" id="" style="max-width: 200px;"
|
<select class="form-select" name="currencyChange" style="max-width: 200px;"
|
||||||
th:hx-get="@{/changeHistoricalCurr}"
|
hx-get="/historicalRates" hx-trigger="change"
|
||||||
hx-trigger="change"
|
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
|
||||||
hx-swap="outerHTML"
|
hx-include="#historicalRatesControls">
|
||||||
hx-target="#HistoricalRatesChartContainer">
|
|
||||||
<option th:each="currency : ${allCurrencies}"
|
<option th:each="currency : ${allCurrencies}"
|
||||||
th:value="${currency.code}"
|
th:value="${currency.code}"
|
||||||
th:text="${currency.code + ' - ' + currency.currencyName}">Test
|
th:text="${currency.code + ' - ' + currency.currencyName}">
|
||||||
</option>
|
</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
|
||||||
|
</form>
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="card-body">
|
<div class="card-body">
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue