better trade endpoints

This commit is contained in:
samantha42
2026-03-25 22:17:19 +01:00
parent d491b9c14c
commit ff7b41e2a8
7 changed files with 51 additions and 26 deletions

View File

@@ -9,36 +9,30 @@ import (
)
func GetTrades(db *sql.DB) ([]model.Trade, error) {
rows, err := db.Query("SELECT company_id, currency_id, shares, product, type, price, traded_at FROM trades")
rows, err := db.Query("SELECT company_id, currency_code, shares, product, type, price, traded_at FROM trades")
if err != nil {
return nil, err
}
defer rows.Close()
var trades []model.Trade
for rows.Next() {
var TickerInt int
var CurrencyInt int
var TypeInt int
var tickerInt int
var typeInt int
var t model.Trade
err := rows.Scan(&TickerInt, &CurrencyInt, &t.Shares, &t.Product, &TypeInt, &t.Price, &t.Date)
err := rows.Scan(&tickerInt, &t.Currency, &t.Shares, &t.Product, &typeInt, &t.Price, &t.Date)
if err != nil {
return nil, err
}
company, err := GetCompanyByID(db, TickerInt)
company, err := GetCompanyByID(db, tickerInt)
if err != nil {
return nil, err
}
currency, err := GetCurrencyByID(db, CurrencyInt)
if err != nil {
return nil, err
}
t.Currency = currency.Name
t.Ticker = *company
switch TypeInt {
switch typeInt {
case 0:
t.Type = model.TradeType(false)
case 1:
@@ -52,7 +46,6 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
if err = rows.Err(); err != nil {
return nil, err
}
return trades, nil
}