update of posotion by trade his
This commit is contained in:
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"Portifolio/internal/database"
|
||||
"Portifolio/internal/model"
|
||||
"Portifolio/internal/service"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -32,15 +33,8 @@ func AddTradeHandler(db *sql.DB) http.HandlerFunc {
|
||||
return
|
||||
}
|
||||
|
||||
// check if company is in the db.
|
||||
company, err := database.GetCompanyByID(db, req.TickerId)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to find currency: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
trade := model.Trade{
|
||||
Symbol: company.Symbol,
|
||||
Symbol: req.Symbol,
|
||||
Shares: req.Shares,
|
||||
Product: model.TradeProduct(req.Product),
|
||||
Type: model.TradeType(req.Type),
|
||||
@@ -49,7 +43,23 @@ func AddTradeHandler(db *sql.DB) http.HandlerFunc {
|
||||
Date: req.Date,
|
||||
}
|
||||
|
||||
database.InsertTrade(db, trade)
|
||||
err = database.InsertTrade(db, trade)
|
||||
if err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to insert trade into db: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
err = service.UpdatePositionByTradeList(db)
|
||||
update := true
|
||||
if err != nil {
|
||||
update = false
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if err := json.NewEncoder(w).Encode(map[string]any{"success": true, "position update": update}); err != nil {
|
||||
http.Error(w, fmt.Sprintf("failed to encode trades: %s", err), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user