2017-09-07 23:04:47 -07:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"tacitus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
trade_insert = `INSERT INTO trades (trade_id, product, price, size, buy, ` +
|
|
|
|
`sell, timestamp) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING id;`
|
|
|
|
trade_find = `SELECT id, trade_id, product, price, size, buy, sell, ` +
|
|
|
|
`timestamp FROM trades WHERE id = $1;`
|
|
|
|
trade_find_trade_id = `SELECT id, trade_id, product, price, size, buy, ` +
|
|
|
|
`sell, timestamp FROM trades WHERE trade_id = $1 AND product = $2;`
|
|
|
|
trade_delete = `DELETE FROM trades WHERE id = $1;`
|
|
|
|
trades_in_date_range = `SELECT id, trade_id, product, price, size, buy, ` +
|
|
|
|
`sell, timestamp FROM trades WHERE product = $1 AND timestamp >= $2 ` +
|
|
|
|
`AND timestamp < $3;`
|
2017-09-08 07:35:47 -07:00
|
|
|
trade_first = `SELECT id, trade_id, product, price, size, buy, sell, timestamp ` +
|
|
|
|
`FROM trades WHERE product = $1 ORDER BY trade_id ASC LIMIT 1;`
|
|
|
|
trade_last = `SELECT id, trade_id, product, price, size, buy, sell, timestamp ` +
|
|
|
|
`FROM trades WHERE product = $1 ORDER BY trade_id DESC LIMIT 1;`
|
2017-09-07 23:04:47 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type TradeService struct {
|
|
|
|
client *Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) Trade(id int) (*tacitus.Trade, error) {
|
|
|
|
var tr tacitus.Trade
|
|
|
|
|
|
|
|
row := t.client.db.QueryRow(trade_find, id)
|
|
|
|
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
|
|
|
&tr.Sell, &tr.Timestamp); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) TradeByTradeId(id int, prod string) (*tacitus.Trade, error) {
|
|
|
|
var tr tacitus.Trade
|
|
|
|
|
|
|
|
row := t.client.db.QueryRow(trade_find_trade_id, id, prod)
|
|
|
|
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
|
|
|
&tr.Sell, &tr.Timestamp); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) CreateTrade(trade *tacitus.Trade) error {
|
|
|
|
var id int
|
|
|
|
res := t.client.db.QueryRow(trade_insert, trade.TradeId, trade.Product,
|
|
|
|
trade.Price, trade.Size, trade.Buy, trade.Sell, trade.Timestamp)
|
|
|
|
if err := res.Scan(&id); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
trade.Id = id
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) DeleteTrade(id int) {
|
|
|
|
t.client.db.Exec(trade_delete, id)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) TradesInDateRange(product string, start,
|
|
|
|
end time.Time) ([]tacitus.Trade, error) {
|
|
|
|
|
|
|
|
trades := make([]tacitus.Trade, 0)
|
|
|
|
|
|
|
|
rows, err := t.client.db.Query(trades_in_date_range, product, start, end)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var trade tacitus.Trade
|
|
|
|
if err = rows.Scan(&trade.Id, &trade.TradeId, &trade.Product, &trade.Price,
|
|
|
|
&trade.Size, &trade.Buy, &trade.Sell, &trade.Timestamp); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
trades = append(trades, trade)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rows.Err() != nil {
|
|
|
|
return nil, rows.Err()
|
|
|
|
}
|
|
|
|
|
|
|
|
return trades, nil
|
|
|
|
}
|
2017-09-08 07:35:47 -07:00
|
|
|
|
|
|
|
func (t *TradeService) FirstTrade(product string) (*tacitus.Trade, error) {
|
|
|
|
var tr tacitus.Trade
|
|
|
|
|
|
|
|
row := t.client.db.QueryRow(trade_first, product)
|
|
|
|
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
|
|
|
&tr.Sell, &tr.Timestamp); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tr, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *TradeService) LastTrade(product string) (*tacitus.Trade, error) {
|
|
|
|
var tr tacitus.Trade
|
|
|
|
|
|
|
|
row := t.client.db.QueryRow(trade_last, product)
|
|
|
|
if err := row.Scan(&tr.Id, &tr.TradeId, &tr.Product, &tr.Price, &tr.Size, &tr.Buy,
|
|
|
|
&tr.Sell, &tr.Timestamp); err != nil {
|
|
|
|
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tr, nil
|
|
|
|
}
|