add trade and position service

This commit is contained in:
zipfriis
2026-03-27 10:14:43 +01:00
parent 2c24ee48b0
commit d0cb9af746
7 changed files with 35 additions and 22 deletions

View File

@@ -43,9 +43,9 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
}
func GetPositions(db *sql.DB) ([]model.Position, error) {
rows, err := db.Query("SELECT company_id, symbol, shares, weight, CostBasis, currency_id, currency_code from position")
rows, err := db.Query("SELECT company_id, symbol, shares, weight, cost_basis, currency_id, currency_code from position")
if err != nil {
return nil, err
return []model.Position{}, err
}
defer rows.Close()
@@ -55,13 +55,13 @@ func GetPositions(db *sql.DB) ([]model.Position, error) {
err := rows.Scan(&t.CompanyID, &t.Symbol, &t.Shares, &t.Weight, &t.CostBasis, &t.CurrencyID, &t.CurrencyCode)
if err != nil {
return nil, err
return []model.Position{}, err
}
positions = append(positions, t)
}
if err = rows.Err(); err != nil {
return nil, err
return []model.Position{}, err
}
return positions, nil

View File

@@ -81,15 +81,15 @@ func GetTradeListHandler(db *sql.DB) http.HandlerFunc {
func GetPositionListHandler(db *sql.DB) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
tradeList, err := database.GetPositions(db)
posList, err := database.GetPositions(db)
if err != nil {
http.Error(w, "failed to fetch trades", http.StatusInternalServerError)
http.Error(w, fmt.Sprintf("failed to fetch postiton: %s", err), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
if err := json.NewEncoder(w).Encode(tradeList); err != nil {
http.Error(w, "failed to encode trades", http.StatusInternalServerError)
if err := json.NewEncoder(w).Encode(map[string]any{"List": posList}); err != nil {
http.Error(w, fmt.Sprintf("failed to encode positions: %s", err), http.StatusInternalServerError)
return
}
}

View File

@@ -54,7 +54,7 @@ type Trade struct {
func (r *AddTradeRequest) Validate() error {
if r.Symbol == "" {
return errors.New("empty SYmbol string")
return errors.New("empty Symbol string")
}
if r.Shares <= 0 {
return errors.New("shares must be a positive integer")

View File

@@ -16,7 +16,7 @@ func UpdatePositionByTradeList(db *sql.DB) error {
fmt.Printf("Failed to get the trades from db: %s", err)
}
var TradeSum map[string]model.Position
TradeSum := make(map[string]model.Position)
for _, trade := range trades {
if trade.Type == model.Buy {