added first implementation of position list

This commit is contained in:
Matthias Hinrichs
2025-07-05 05:10:42 +02:00
parent aef9342cc5
commit 3a1ddd88d9
9 changed files with 1348 additions and 452 deletions
+38 -1
View File
@@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"portfolio-tracker/internal/model"
"portfolio-tracker/internal/service"
"portfolio-tracker/internal/web/templates"
"strconv"
"strings"
@@ -53,10 +54,30 @@ func PortfolioDetailHandler(w http.ResponseWriter, r *http.Request) {
return
}
// Calculate positions
fmt.Printf("DEBUG: Starting position calculation for portfolio %d with %d activities\n", portfolio.ID, len(portfolio.Activities))
positionCalculator := service.NewPositionCalculator()
positions, positionSummary, err := positionCalculator.CalculatePositions(portfolio)
if err != nil {
fmt.Printf("Error calculating positions: %v\n", err)
// Continue with empty positions rather than failing
positions = []model.Position{}
positionSummary = model.PositionSummary{
Currency: portfolio.BaseCurrency,
LastUpdated: time.Now(),
}
}
fmt.Printf("DEBUG: Position calculation completed. Found %d positions\n", len(positions))
for i, pos := range positions {
fmt.Printf("DEBUG: Position %d: %s - Shares: %.4f, Open: %v\n", i+1, pos.Stock, pos.Shares, pos.IsOpen())
}
fmt.Printf("DEBUG: Position summary - Total Value: %.2f, Total Cost: %.2f\n", positionSummary.TotalValue, positionSummary.TotalCostBasis)
// Get all user portfolios for navigation
portfolios := getUserPortfolios(username)
component := templates.PortfolioDetail(auth, username, portfolio, portfolios)
component := templates.PortfolioDetail(auth, username, portfolio, portfolios, positions, positionSummary)
templ.Handler(component).ServeHTTP(w, r)
}
@@ -196,6 +217,14 @@ func PortfolioTransactionHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("Stock %s currency: %s\n", stock, stockCurrency)
// Handle GBp (pence) to GBP (pounds) conversion
// If the stock is quoted in pence, convert price to pounds
if stockCurrency == "GBp" {
price = price / 100.0
stockCurrency = "GBP"
fmt.Printf("Converted GBp price %.2f to GBP price %.2f\n", price*100, price)
}
// Create activity
activity := model.Activity{
PortfolioID: uint(portfolioID),
@@ -317,6 +346,14 @@ func EditTransactionHandler(w http.ResponseWriter, r *http.Request) {
stockCurrency = portfolio.BaseCurrency
}
// Handle GBp (pence) to GBP (pounds) conversion
// If the stock is quoted in pence, convert price to pounds
if stockCurrency == "GBp" {
price = price / 100.0
stockCurrency = "GBP"
fmt.Printf("Converted GBp price %.2f to GBP price %.2f\n", price*100, price)
}
// Update activity fields
activity.Type = model.ActivityType(transactionType)
activity.Stock = stock