update of posotion by trade his

This commit is contained in:
samantha42
2026-03-26 11:42:38 +01:00
parent 3f878c1dc0
commit 2c24ee48b0
7 changed files with 156 additions and 30 deletions

View File

@@ -8,6 +8,22 @@ import (
_ "github.com/mattn/go-sqlite3"
)
func GetCompanyBySymbol(db *sql.DB, symbol string) (*model.Company, error) {
var c model.Company
err := db.QueryRow(
`SELECT id, symbol, shares_outstanding, price, currency_id FROM companies WHERE symbol = ?`,
symbol,
).Scan(&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
if err == sql.ErrNoRows {
return nil, nil
}
if err != nil {
return nil, fmt.Errorf("query company: %w", err)
}
return &c, nil
}
func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
var c model.Company
err := db.QueryRow(

View File

@@ -9,7 +9,7 @@ import (
)
func GetTrades(db *sql.DB) ([]model.Trade, error) {
rows, err := db.Query("SELECT company_id, symbol, currency_id, currency_code, shares, product, type, price, traded_at FROM trades")
rows, err := db.Query("SELECT symbol, currency_code, shares, product, type, price, traded_at FROM trades")
if err != nil {
return nil, err
}
@@ -20,7 +20,7 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
var typeInt int
var t model.Trade
err := rows.Scan(&t.CompanyID, &t.Symbol, &t.CompanyID, &t.CurrencyCode, &t.Shares, &t.Product, &typeInt, &t.Price, &t.Date)
err := rows.Scan(&t.Symbol, &t.CurrencyCode, &t.Shares, &t.Product, &typeInt, &t.Price, &t.Date)
if err != nil {
return nil, err
}
@@ -69,10 +69,8 @@ func GetPositions(db *sql.DB) ([]model.Position, error) {
func InsertTrade(db *sql.DB, trade model.Trade) error {
_, err := db.Exec(
"INSERT INTO trades (company_id, symbol, currency_id, currency_code, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
trade.CompanyID,
"INSERT INTO trades (symbol, currency_code, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
trade.Symbol,
trade.CurrencyID,
trade.CurrencyCode,
trade.Shares,
trade.Product,
@@ -82,3 +80,56 @@ func InsertTrade(db *sql.DB, trade model.Trade) error {
)
return err
}
func UpdatePositions(db *sql.DB, positions []model.Position) error {
// Complete overwrite of the db positions
_, err := db.Exec("DELETE FROM position")
if err != nil {
return fmt.Errorf("failed to clear positions: %s", err)
}
for _, p := range positions {
// Resolve company_id if missing
if p.CompanyID == 0 {
company, err := GetCompanyBySymbol(db, p.Symbol)
if err != nil {
return fmt.Errorf("could not find company %s: %s", p.Symbol, err)
}
p.CompanyID = company.ID
}
// Resolve currency_id if missing
if p.CurrencyID == 0 {
currency, err := GetCurrencyByCode(db, p.CurrencyCode)
if err != nil {
return fmt.Errorf("could not find currency %s: %s", p.CurrencyCode, err)
}
p.CurrencyID = currency.ID
}
_, err := db.Exec(`
INSERT INTO position (company_id, symbol, currency_id, currency_code, shares, weight, cost_basis)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(company_id) DO UPDATE SET
symbol = excluded.symbol,
currency_id = excluded.currency_id,
currency_code = excluded.currency_code,
shares = excluded.shares,
weight = excluded.weight,
cost_basis = excluded.cost_basis
`,
p.CompanyID,
p.Symbol,
p.CurrencyID,
p.CurrencyCode,
p.Shares,
p.Weight,
p.CostBasis,
)
if err != nil {
return fmt.Errorf("failed to upsert position %s: %s", p.Symbol, err)
}
}
return nil
}