add trade and position service
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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")
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user