adding position and trade endpoints
This commit is contained in:
@@ -30,6 +30,7 @@ func InitDB(db *sql.DB) {
|
||||
CREATE TABLE IF NOT EXISTS position (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
shares INTEGER NOT NULL,
|
||||
weight REAL NOT NULL,
|
||||
CostBases REAL NOT NULL,
|
||||
|
||||
@@ -30,6 +30,29 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
|
||||
return trades, nil
|
||||
}
|
||||
|
||||
func GetPositions(db *sql.DB) ([]model.Position, error) {
|
||||
rows, err := db.Query("SELECT company_id, shares, weight, CostBases, currency_id")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var positions []model.Position
|
||||
for rows.Next() {
|
||||
var t model.Position
|
||||
err := rows.Scan(&t.Company.ID, &t.Shares, &t.Weight, &t.CostBasis, t.Currency)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
positions = append(positions, t)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return positions, nil
|
||||
}
|
||||
|
||||
func InsertTrade(db *sql.DB, trade model.Trade) error {
|
||||
_, err := db.Exec(
|
||||
"INSERT INTO trades (company_id, currency_id, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
|
||||
Reference in New Issue
Block a user