changes to model
This commit is contained in:
@@ -11,9 +11,9 @@ import (
|
||||
func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
|
||||
var c model.Company
|
||||
err := db.QueryRow(
|
||||
`SELECT id, name, shares_outstanding, price, currency_id FROM companies WHERE id = ?`,
|
||||
`SELECT id, symbol, shares_outstanding, price, currency_id FROM companies WHERE id = ?`,
|
||||
id,
|
||||
).Scan(&c.ID, &c.Name, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
|
||||
).Scan(&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID)
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, nil
|
||||
@@ -23,3 +23,49 @@ func GetCompanyByID(db *sql.DB, id int) (*model.Company, error) {
|
||||
}
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
func AddCompany(db *sql.DB, input model.CompanyInput) (int, error) {
|
||||
if input.CurrencyID == 0 {
|
||||
if input.CurrencyCode != "" {
|
||||
currency, err := GetCurrencyByCode(db, input.CurrencyCode)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("could not get currency: %s", err)
|
||||
}
|
||||
input.CurrencyID = currency.ID
|
||||
} else {
|
||||
return 0, fmt.Errorf("no currency reference")
|
||||
}
|
||||
}
|
||||
|
||||
res, err := db.Exec(
|
||||
`INSERT INTO companies (symbol, shares_outstanding, price, currency_id) VALUES (?, ?, ?, ?)`,
|
||||
input.Symbol, input.SharesOutstanding, input.Price, input.CurrencyID,
|
||||
)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("failed to insert: %s", err)
|
||||
}
|
||||
id, err := res.LastInsertId()
|
||||
return int(id), err
|
||||
}
|
||||
|
||||
func GetAllCompanies(db *sql.DB) ([]model.Company, error) {
|
||||
rows, err := db.Query(`
|
||||
SELECT id, symbol, shares_outstanding, price, currency_id FROM companies
|
||||
`)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var companies []model.Company
|
||||
for rows.Next() {
|
||||
var c model.Company
|
||||
if err := rows.Scan(
|
||||
&c.ID, &c.Symbol, &c.SharesOutstanding, &c.Price, &c.CurrencyID,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
companies = append(companies, c)
|
||||
}
|
||||
return companies, rows.Err()
|
||||
}
|
||||
|
||||
@@ -20,14 +20,22 @@ func GetCurrencyByID(db *sql.DB, ID int) (model.Currency, error) {
|
||||
return c, nil
|
||||
}
|
||||
|
||||
/*
|
||||
CREATE TABLE IF NOT EXISTS currencies (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
code TEXT NOT NULL UNIQUE,
|
||||
name TEXT NOT NULL
|
||||
);
|
||||
*/
|
||||
|
||||
func GetCurrencyByCode(db *sql.DB, Code string) (model.Currency, error) {
|
||||
var c model.Currency
|
||||
err := db.QueryRow(
|
||||
`SELECT id, code, name, FROM currencies WHERE code = ?`,
|
||||
`SELECT id, code, name FROM currencies WHERE code = ?`,
|
||||
Code,
|
||||
).Scan(&c.ID, &c.Code, &c.Name)
|
||||
if err == sql.ErrNoRows {
|
||||
return c, fmt.Errorf("company %d not found", Code)
|
||||
return c, fmt.Errorf("company %s not found", Code)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ func InitDB(db *sql.DB) {
|
||||
|
||||
CREATE TABLE IF NOT EXISTS trades (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
symbol TEXT NOT NULL,
|
||||
currency_code TEXT NOT NULL,
|
||||
shares INTEGER NOT NULL,
|
||||
product INTEGER NOT NULL CHECK(product IN (0, 1, 2, 3)),
|
||||
@@ -29,16 +29,20 @@ func InitDB(db *sql.DB) {
|
||||
|
||||
CREATE TABLE IF NOT EXISTS position (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
company_id INTEGER NOT NULL UNIQUE,
|
||||
symbol TEXT NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
currency_code TEXT NOT NULL,
|
||||
shares INTEGER NOT NULL,
|
||||
weight REAL NOT NULL,
|
||||
cost_basis REAL NOT NULL
|
||||
cost_basis REAL NOT NULL,
|
||||
FOREIGN KEY (currency_id) REFERENCES currencies(id),
|
||||
FOREIGN KEY (company_id) REFERENCES companies(id)
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
symbol TEXT NOT NULL UNIQUE,
|
||||
shares_outstanding INTEGER NOT NULL,
|
||||
price REAL NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
@@ -84,68 +88,3 @@ func InitDB(db *sql.DB) {
|
||||
}
|
||||
fmt.Println("Tables ready")
|
||||
}
|
||||
|
||||
func MigrateAddUniqueToRevenueEntries(db *sql.DB) error {
|
||||
steps := []string{
|
||||
// 1. copy existing data into a temp table with the new constraint
|
||||
`CREATE TABLE IF NOT EXISTS revenue_entries_new (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_id INTEGER NOT NULL,
|
||||
category_id INTEGER NOT NULL,
|
||||
period_id INTEGER NOT NULL,
|
||||
value REAL NOT NULL,
|
||||
FOREIGN KEY (company_id) REFERENCES companies(id),
|
||||
FOREIGN KEY (currency_id) REFERENCES currencies(id),
|
||||
FOREIGN KEY (category_id) REFERENCES category(id),
|
||||
FOREIGN KEY (period_id) REFERENCES periods(id),
|
||||
UNIQUE(company_id, category_id, period_id)
|
||||
)`,
|
||||
// 2. copy data over
|
||||
`INSERT OR IGNORE INTO revenue_entries_new
|
||||
SELECT id, company_id, currency_id, category_id, period_id, value
|
||||
FROM revenue_entries`,
|
||||
// 3. drop old table
|
||||
`DROP TABLE revenue_entries`,
|
||||
// 4. rename new table
|
||||
`ALTER TABLE revenue_entries_new RENAME TO revenue_entries`,
|
||||
}
|
||||
|
||||
for _, step := range steps {
|
||||
if _, err := db.Exec(step); err != nil {
|
||||
return fmt.Errorf("migration failed: %w", err)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func MigrateTradeCode(db *sql.DB) error {
|
||||
step :=
|
||||
// 1. copy existing data into a temp table with the new constraint
|
||||
`
|
||||
ALTER TABLE trades RENAME TO trades_old;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS trades (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
company_id INTEGER NOT NULL,
|
||||
currency_code TEXT 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
|
||||
);
|
||||
|
||||
INSERT INTO trades (id, company_id, currency_code, shares, product, type, price, traded_at)
|
||||
SELECT id, company_id, '', shares, product, type, price, traded_at
|
||||
FROM trades_old;
|
||||
|
||||
DROP TABLE trades_old;
|
||||
`
|
||||
|
||||
_, err := db.Exec(step)
|
||||
if err != nil {
|
||||
return fmt.Errorf("migration failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import (
|
||||
)
|
||||
|
||||
func GetTrades(db *sql.DB) ([]model.Trade, error) {
|
||||
rows, err := db.Query("SELECT company_id, currency_code, shares, product, type, price, traded_at FROM trades")
|
||||
rows, err := db.Query("SELECT company_id, symbol, currency_id, currency_code, shares, product, type, price, traded_at FROM trades")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -17,21 +17,14 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
|
||||
|
||||
var trades []model.Trade
|
||||
for rows.Next() {
|
||||
var tickerInt int
|
||||
var typeInt int
|
||||
var t model.Trade
|
||||
|
||||
err := rows.Scan(&tickerInt, &t.Currency, &t.Shares, &t.Product, &typeInt, &t.Price, &t.Date)
|
||||
err := rows.Scan(&t.CompanyID, &t.Symbol, &t.CompanyID, &t.CurrencyCode, &t.Shares, &t.Product, &typeInt, &t.Price, &t.Date)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
company, err := GetCompanyByID(db, tickerInt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.Ticker = *company
|
||||
|
||||
switch typeInt {
|
||||
case 0:
|
||||
t.Type = model.TradeType(false)
|
||||
@@ -50,7 +43,7 @@ func GetTrades(db *sql.DB) ([]model.Trade, error) {
|
||||
}
|
||||
|
||||
func GetPositions(db *sql.DB) ([]model.Position, error) {
|
||||
rows, err := db.Query("SELECT company_id, shares, weight, CostBases, currency_id")
|
||||
rows, err := db.Query("SELECT company_id, symbol, shares, weight, CostBasis, currency_id, currency_code from position")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -59,10 +52,12 @@ func GetPositions(db *sql.DB) ([]model.Position, error) {
|
||||
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)
|
||||
|
||||
err := rows.Scan(&t.CompanyID, &t.Symbol, &t.Shares, &t.Weight, &t.CostBasis, &t.CurrencyID, &t.CurrencyCode)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
positions = append(positions, t)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
@@ -74,9 +69,11 @@ 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, currency_id, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?)",
|
||||
trade.Ticker.ID,
|
||||
trade.Currency,
|
||||
"INSERT INTO trades (company_id, symbol, currency_id, currency_code, shares, product, type, price, traded_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
|
||||
trade.CompanyID,
|
||||
trade.Symbol,
|
||||
trade.CurrencyID,
|
||||
trade.CurrencyCode,
|
||||
trade.Shares,
|
||||
trade.Product,
|
||||
trade.Type,
|
||||
|
||||
Reference in New Issue
Block a user