set and get now work trough endpoint api
This commit is contained in:
@@ -19,3 +19,15 @@ func GetCurrencyByID(db *sql.DB, ID int) (model.Currency, error) {
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
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 = ?`,
|
||||
Code,
|
||||
).Scan(&c.ID, &c.Code, &c.Name)
|
||||
if err == sql.ErrNoRows {
|
||||
return c, fmt.Errorf("company %d not found", Code)
|
||||
}
|
||||
return c, nil
|
||||
}
|
||||
|
||||
@@ -23,17 +23,17 @@ func InitDB(db *sql.DB) {
|
||||
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
|
||||
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,
|
||||
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,
|
||||
shares INTEGER NOT NULL,
|
||||
weight REAL NOT NULL,
|
||||
cost_basis REAL NOT NULL
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
|
||||
@@ -3,24 +3,50 @@ package database
|
||||
import (
|
||||
"Portifolio/internal/model"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
|
||||
_ "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")
|
||||
rows, err := db.Query("SELECT 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 TickerInt int
|
||||
var CurrencyInt int
|
||||
var TypeInt int
|
||||
|
||||
var t model.Trade
|
||||
err := rows.Scan(&t.Ticker, &t.Currency, &t.Shares, &t.Product, &t.Type, &t.Price, &t.Date)
|
||||
err := rows.Scan(&TickerInt, &CurrencyInt, &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
|
||||
}
|
||||
currency, err := GetCurrencyByID(db, CurrencyInt)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
t.Currency = currency
|
||||
t.Ticker = *company
|
||||
switch TypeInt {
|
||||
case 0:
|
||||
t.Type = model.TradeType(false)
|
||||
case 1:
|
||||
t.Type = model.TradeType(true)
|
||||
default:
|
||||
return nil, fmt.Errorf("failed to convert given Type int to bool of trade type.")
|
||||
}
|
||||
|
||||
trades = append(trades, t)
|
||||
}
|
||||
if err = rows.Err(); err != nil {
|
||||
|
||||
Reference in New Issue
Block a user