This repository has been archived on 2022-11-30. You can view files and clone it, but cannot push or open issues or pull requests.
tacitus/postgres/trade_service.go
2017-09-08 08:45:22 -07:00

126 lines
3.1 KiB
Go

package postgres
import (
"time"
"tacitus"
)
const (
trade_columns = `trade_id, product, price, size, buy, sell, timestamp`
trade_insert = `INSERT INTO trades (` + trade_columns + `) VALUES ` +
`($1, $2, $3, $4, $5, $6, $7) RETURNING id;`
trade_find = `SELECT id, ` + trade_columns + ` FROM trades WHERE id = $1;`
trade_find_trade_id = `SELECT id, ` + trade_columns + ` FROM trades ` +
`WHERE trade_id = $1 AND product = $2;`
trade_delete = `DELETE FROM trades WHERE id = $1;`
trades_in_date_range = `SELECT id, ` + trade_columns + ` FROM ` +
`trades WHERE product = $1 AND timestamp >= $2 AND timestamp < $3;`
trade_first = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
`product = $1 ORDER BY trade_id ASC LIMIT 1;`
trade_last = `SELECT id, ` + trade_columns + ` FROM trades WHERE ` +
`product = $1 ORDER BY trade_id DESC LIMIT 1;`
)
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
}
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
}