Trade service clean up

This commit is contained in:
Kevin Cotugno 2017-09-23 16:52:47 -07:00
parent 0787c29ebd
commit 144ba33b48
2 changed files with 57 additions and 57 deletions

View File

@ -10,7 +10,7 @@ import (
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;`
`($1, $2, $3, $4, $5, $6, $7) RETURNING id,` + trade_columns + `;`
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;`
@ -31,70 +31,70 @@ type TradeService struct {
client *Client
}
func (t *TradeService) Trade(id int) (*tacitus.Trade, error) {
var tr tacitus.Trade
func (s *TradeService) Trade(id int) (tacitus.Trade, error) {
var t tacitus.Trade
t.client.logQuery(trade_find, id)
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 {
t.client.logError(trade_find, err, id)
s.client.logQuery(trade_find, id)
row := s.client.db.QueryRow(trade_find, id)
err := row.Scan(&t.Id, &t.TradeId, &t.Product, &t.Price, &t.Size, &t.Buy,
&t.Sell, &t.Timestamp)
if err != nil {
s.client.logError(trade_find, err, id)
return nil, err
return t, err
}
return &tr, nil
return t, nil
}
func (t *TradeService) TradeByTradeId(id int, prod string) (*tacitus.Trade, error) {
var tr tacitus.Trade
func (s *TradeService) TradeByTradeId(id int, prod string) (tacitus.Trade, error) {
var t tacitus.Trade
t.client.logQuery(trade_find_trade_id, id, prod)
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 {
s.client.logQuery(trade_find_trade_id, id, prod)
row := s.client.db.QueryRow(trade_find_trade_id, id, prod)
err := row.Scan(&t.Id, &t.TradeId, &t.Product, &t.Price,
&t.Size, &t.Buy, &t.Sell, &t.Timestamp)
if err != nil {
s.client.logError(trade_find_trade_id, err, id, prod)
t.client.logError(trade_find_trade_id, err, id, prod)
return nil, err
return t, err
}
return &tr, nil
return t, nil
}
func (t *TradeService) CreateTrade(trade *tacitus.Trade) error {
var id int
func (s *TradeService) CreateTrade(trade tacitus.Trade) (tacitus.Trade, error) {
var t tacitus.Trade
params := []interface{}{trade.TradeId, trade.Product, trade.Price,
trade.Size, trade.Buy, trade.Sell, trade.Timestamp}
t.client.logQuery(trade_insert, params...)
res := t.client.db.QueryRow(trade_insert, params...)
if err := res.Scan(&id); err != nil {
t.client.logError(trade_insert, err, params...)
return err
s.client.logQuery(trade_insert, params...)
res := s.client.db.QueryRow(trade_insert, params...)
if err := res.Scan(&t.Id); err != nil {
s.client.logError(trade_insert, err, params...)
return t, err
}
trade.Id = id
return nil
return t, nil
}
func (t *TradeService) DeleteTrade(id int) {
t.client.logQuery(trade_delete, id)
func (s *TradeService) DeleteTrade(id int) {
s.client.logQuery(trade_delete, id)
_, err := t.client.db.Exec(trade_delete, id)
_, err := s.client.db.Exec(trade_delete, id)
if err != nil {
t.client.logError(trade_delete, err, id)
s.client.logError(trade_delete, err, id)
}
}
func (t *TradeService) TradesInDateRange(product string, start,
func (s *TradeService) TradesInDateRange(product string, start,
end time.Time) ([]tacitus.Trade, error) {
t.client.logQuery(trade_in_date_range, product, start, end)
rows, err := t.client.db.Query(trade_in_date_range, product, start, end)
s.client.logQuery(trade_in_date_range, product, start, end)
rows, err := s.client.db.Query(trade_in_date_range, product, start, end)
if err != nil {
t.client.logError(trade_in_date_range, err, product, start, end)
s.client.logError(trade_in_date_range, err, product, start, end)
return nil, err
}
@ -102,11 +102,11 @@ func (t *TradeService) TradesInDateRange(product string, start,
return deserializeTrades(rows)
}
func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) {
t.client.logQuery(trade_first, product, limit)
rows, err := t.client.db.Query(trade_first, product, limit)
func (s *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_first, product, limit)
rows, err := s.client.db.Query(trade_first, product, limit)
if err != nil {
t.client.logError(trade_first, err, product, limit)
s.client.logError(trade_first, err, product, limit)
return nil, err
}
@ -114,11 +114,11 @@ func (t *TradeService) FirstTrades(product string, limit int) ([]tacitus.Trade,
return deserializeTrades(rows)
}
func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) {
t.client.logQuery(trade_last, product, limit)
rows, err := t.client.db.Query(trade_last, product, limit)
func (s *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_last, product, limit)
rows, err := s.client.db.Query(trade_last, product, limit)
if err != nil {
t.client.logError(trade_last, err, product, limit)
s.client.logError(trade_last, err, product, limit)
return nil, err
}
@ -126,11 +126,11 @@ func (t *TradeService) LastTrades(product string, limit int) ([]tacitus.Trade, e
return deserializeTrades(rows)
}
func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) {
t.client.logQuery(trade_after, product, id, limit)
rows, err := t.client.db.Query(trade_after, product, id, limit)
func (s *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_after, product, id, limit)
rows, err := s.client.db.Query(trade_after, product, id, limit)
if err != nil {
t.client.logError(trade_after, err, product, id, limit)
s.client.logError(trade_after, err, product, id, limit)
return nil, err
}
@ -139,11 +139,11 @@ func (t *TradeService) TradesAfter(product string, id, limit int) ([]tacitus.Tra
}
func (t *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) {
t.client.logQuery(trade_before, product, id, limit)
rows, err := t.client.db.Query(trade_before, product, id, limit)
func (s *TradeService) TradesBefore(product string, id, limit int) ([]tacitus.Trade, error) {
s.client.logQuery(trade_before, product, id, limit)
rows, err := s.client.db.Query(trade_before, product, id, limit)
if err != nil {
t.client.logError(trade_before, err, product, id, limit)
s.client.logError(trade_before, err, product, id, limit)
return nil, err
}

View File

@ -21,9 +21,9 @@ type Trade struct {
}
type TradeService interface {
Trade(id int) (*Trade, error)
TradeByTradeId(id int, prod string) (*Trade, error)
CreateTrade(t *Trade) error
Trade(id int) (Trade, error)
TradeByTradeId(id int, prod string) (Trade, error)
CreateTrade(t Trade) (Trade, error)
DeleteTrade(id int)
TradesInDateRange(product string, start, end time.Time) ([]Trade, error)
FirstTrades(product string, limit int) ([]Trade, error)