new endpoints

This commit is contained in:
zipfriis
2026-03-25 16:54:46 +01:00
parent 4e5b830e75
commit 52d99c7012
8 changed files with 305 additions and 99 deletions

View File

@@ -16,6 +16,25 @@ func InitDB(db *sql.DB) {
name TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS trades (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_id INTEGER NOT NULL,
currency_id INTEGER NOT NULL,
shares INTEGER NOT NULL,
product INTEGER NOT NULL CHECK(product IN (0, 1, 2, 3)),
type INTEGER NOT NULL CHECK(type IN (0, 1)),
price REAL NOT NULL
traded_at DATETIME NOT NULL
);
CREATE TABLE IF NOT EXISTS position (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_id INTEGER NOT NULL,
shares INTEGER NOT NULL,
weight REAL NOT NULL,
CostBases REAL NOT NULL,
);
CREATE TABLE IF NOT EXISTS companies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,

View File

@@ -0,0 +1,45 @@
package database
import (
"Portifolio/internal/model"
"database/sql"
_ "github.com/mattn/go-sqlite3"
)
func GetTrades(db *sql.DB) ([]model.Trade, error) {
rows, err := db.Query("SELECT id, company_id, currency_id, 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 t model.Trade
err := rows.Scan(&t.Ticker, &t.Currency, &t.Shares, &t.Product, &t.Type, &t.Price, &t.Date)
if err != nil {
return nil, err
}
trades = append(trades, t)
}
if err = rows.Err(); err != nil {
return nil, err
}
return trades, 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 (?, ?, ?, ?, ?, ?, ?)",
trade.Ticker.ID,
trade.Currency.ID,
trade.Shares,
trade.Product,
trade.Type,
trade.Price,
trade.Date,
)
return err
}