app is now using historical exchange rates for transactions

This commit is contained in:
Matthias Hinrichs
2025-07-05 03:44:53 +02:00
parent a96bbe4d2a
commit aef9342cc5
11 changed files with 926 additions and 237 deletions
+47 -5
View File
@@ -6,6 +6,7 @@ import (
"net/http"
"portfolio-tracker/internal/model"
"portfolio-tracker/internal/util"
"time"
)
func JsonEndpoint(w http.ResponseWriter, r *http.Request) {
@@ -110,7 +111,26 @@ func ClearCurrencyCacheHandler(w http.ResponseWriter, r *http.Request) {
response := map[string]string{
"status": "success",
"message": "Currency cache cleared successfully",
"message": "Currency cache (including historical rates) cleared successfully",
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
// CleanExpiredCacheHandler removes expired cache entries
func CleanExpiredCacheHandler(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
http.Error(w, "Nur POST erlaubt", http.StatusMethodNotAllowed)
return
}
removedCount := util.CleanExpiredCache()
response := map[string]interface{}{
"status": "success",
"message": "Expired cache entries cleaned",
"removed_count": removedCount,
}
w.Header().Set("Content-Type", "application/json")
@@ -124,12 +144,34 @@ func CurrencyCacheInfoHandler(w http.ResponseWriter, r *http.Request) {
return
}
cacheInfo := util.GetCacheInfo()
detailedCacheInfo := util.GetDetailedCacheInfo()
// Separate historical and current rates
historicalRates := make(map[string]interface{})
currentRates := make(map[string]interface{})
for key, cached := range detailedCacheInfo {
rateInfo := map[string]interface{}{
"rate": cached.Rate,
"timestamp": cached.Timestamp,
"date": cached.Date,
"age_hours": time.Since(cached.Timestamp).Hours(),
}
if cached.Date == cached.Timestamp.Format("2006-01-02") {
currentRates[key] = rateInfo
} else {
historicalRates[key] = rateInfo
}
}
response := map[string]interface{}{
"status": "success",
"cache_size": len(cacheInfo),
"entries": cacheInfo,
"status": "success",
"total_entries": len(detailedCacheInfo),
"historical_rates": historicalRates,
"current_rates": currentRates,
"historical_count": len(historicalRates),
"current_count": len(currentRates),
}
w.Header().Set("Content-Type", "application/json")