Refactor investment-related controllers and services; enhance historical rates functionality in investments.html

This commit is contained in:
Kiyan 2026-02-18 20:38:52 +02:00
parent f5b3e49ca6
commit 052b3a5fed
9 changed files with 89 additions and 81 deletions

Binary file not shown.

Before

Width:  |  Height:  |  Size: 51 KiB

After

Width:  |  Height:  |  Size: 105 KiB

View File

@ -16,7 +16,7 @@ import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
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.Home.OverviewCard;
import com.example.FinanceTracker.Models.Transaction;
@ -26,8 +26,8 @@ import com.example.FinanceTracker.Services.TransactionService;
@Controller
public class HomeController {
@Autowired
private PdfManager pdfManager;
// @Autowired
// private PdfManager pdfManager;
@Autowired
private TransactionService transactionsService;

View File

@ -2,20 +2,19 @@ package com.example.FinanceTracker.Controllers;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import com.example.FinanceTracker.DTOs.Investments.CurrRate;
import com.example.FinanceTracker.DTOs.Investments.Currency;
import com.example.FinanceTracker.DTOs.Investments.ExchangeRate;
import com.example.FinanceTracker.Services.InvestmentService;
import com.example.FinanceTracker.Services.TransactionService;
@Controller
public class InvestmentsController {
@ -23,9 +22,15 @@ public class InvestmentsController {
@Autowired
private InvestmentService investmentService;
@Autowired
private TransactionService transactionService;
@GetMapping("/investments")
public String investments(Model model) {
// Heading
model.addAttribute("latestBalance", "Total Invested: " +transactionService.getTotalInvested() + "*");
// Create Currency Pills
List<CurrRate> allRates = investmentService.getCurrRates();
model.addAttribute("currRates", allRates);
@ -54,12 +59,21 @@ public class InvestmentsController {
return "investments";
}
@GetMapping("/historicalRatesWeekly")
public String showHistoricalRatesWeek(Model model,
@RequestParam("historicalRatesWeekly") boolean historicalRatesWeekly){
@GetMapping("/historicalRates")
public String getHistoricalRates(
@RequestParam("duration") String durationCode,
@RequestParam("currencyChange") String currencyChange,
Model model) {
// 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
ArrayList<BigDecimal> historicalRates = new ArrayList<>();
@ -75,29 +89,4 @@ public class InvestmentsController {
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";
}
}

View File

@ -32,7 +32,7 @@ public class Transaction {
private String description;
@Column
private int category;
private String category;
@Column
private BigDecimal moneyIn;

View File

@ -186,4 +186,12 @@ public interface TransactionRepo extends JpaRepository<Transaction, UUID> {
""", nativeQuery = true)
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();
}

View File

@ -1,6 +1,5 @@
package com.example.FinanceTracker.Services;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;
@ -56,10 +55,14 @@ public class InvestmentService {
.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";
} else {
} else if (changeFactor < 0) {
return "DOWN";
}else {
return "NEUTRAL";
}
}

View File

@ -121,7 +121,6 @@ public class TransactionService {
// Other
// =============================================================
public List<Object[]> getTotals() {
return transactionRepo.getTotals();
}
@ -138,4 +137,8 @@ public class TransactionService {
return transactionRepo.getSavingRate();
}
public double getTotalInvested() {
return transactionRepo.getTotalInvested();
}
}

View File

@ -1,21 +1,20 @@
<span
th:fragment="curr-pill (currRate)"
class="badge d-inline-flex align-items-center gap-1 p-2"
style="min-width: 100px;"
th:classappend="
<span th:fragment="curr-pill (currRate)" class="badge d-inline-flex align-items-center gap-1 p-2"
style="min-width: 100px;" th:classappend="
${currRate.change} == 'UP'
? ' border border-success text-success'
: ' border border-danger text-danger'
: (${currRate.change} == 'DOWN'
? ' border border-danger text-danger'
: ' border border-warning text-warning')
">
<span
class="material-icons bg-gray"
th:text="${currRate.change} == 'UP' ? 'trending_up' : 'trending_down'">
<span class="material-icons bg-gray" th:text="
${currRate.change} == 'UP'
? 'trending_up'
: (${currRate.change} == 'DOWN'
? 'trending_down'
: 'trending_flat')
">
</span>
<span class="fw-semibold text-dark" th:text="${currRate.ISO}"></span>
<span th:text="${currRate.value}"></span>
</span>

View File

@ -134,37 +134,43 @@
<div class="col-lg-4 ">
<span class="fw-bold text-primary mb-1">Historical Rates</span>
</div>
<div class="col-lg-4 mw-50">
<div class="btn-group d-flex justify-content-center mb-1" role="group"
aria-label="Time range">
<input th:hx-get="@{/historicalRatesWeekly}" hx-trigger="click once"
hx-swap="outerHTML" hx-target="#HistoricalRatesChartContainer"
type="radio" class="btn-check" name="historicalRatesWeekly"
id="option1" autocomplete="off" checked>
<form id="historicalRatesControls" >
<!-- Duration Radios -->
<div class="btn-group d-flex justify-content-center mb-1" role="group">
<input type="radio" name="duration" value="WEEK" class="btn-check"
id="option1" checked hx-get="/historicalRates" hx-trigger="change"
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
hx-include="#historicalRatesControls">
<label class="btn btn-secondary" for="option1">Week</label>
<input type="radio" class="btn-check" name="options" id="option2"
autocomplete="off">
<input type="radio" name="duration" value="MONTH" class="btn-check"
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>
<input type="radio" class="btn-check" name="options" id="option3"
autocomplete="off">
<input type="radio" name="duration" value="YEAR" class="btn-check"
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>
</div>
</div>
<div class="col-lg-4 d-flex justify-content-end">
<select class="form-select" name="currencyChange" id="" style="max-width: 200px;"
th:hx-get="@{/changeHistoricalCurr}"
hx-trigger="change"
hx-swap="outerHTML"
hx-target="#HistoricalRatesChartContainer">
<!-- Currency Select -->
<select class="form-select" name="currencyChange" style="max-width: 200px;"
hx-get="/historicalRates" hx-trigger="change"
hx-target="#HistoricalRatesChartContainer" hx-swap="outerHTML"
hx-include="#historicalRatesControls">
<option th:each="currency : ${allCurrencies}"
th:value="${currency.code}"
th:text="${currency.code + ' - ' + currency.currencyName}">Test
th:text="${currency.code + ' - ' + currency.currencyName}">
</option>
</select>
</div>
</form>
</div>
</div>
<div class="card-body">