2017-09-23 10:19:43 -07:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/kcotugno/tacitus"
|
|
|
|
)
|
|
|
|
|
2017-09-23 10:59:13 -07:00
|
|
|
const (
|
|
|
|
conf_columns = `product, last_trade_id`
|
|
|
|
conf_insert = `INSERT INTO confirmations (` + conf_columns +
|
|
|
|
`) VALUES ($1, $2) RETURNING id, ` + conf_columns + `;`
|
2017-10-05 21:53:02 -07:00
|
|
|
conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations ` +
|
2017-09-23 11:48:24 -07:00
|
|
|
`WHERE product = $1;`
|
2017-10-05 21:53:02 -07:00
|
|
|
conf_update = `UPDATE confirmations SET last_trade_id = $1 WHERE product = $2;`
|
2017-09-23 10:59:13 -07:00
|
|
|
)
|
|
|
|
|
2017-09-23 10:19:43 -07:00
|
|
|
type ConfirmationService struct {
|
2017-09-24 17:30:01 -07:00
|
|
|
client *Client
|
2017-09-23 10:19:43 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
func (s *ConfirmationService) Confirmation(product string) (tacitus.Confirmation, error) {
|
|
|
|
var conf tacitus.Confirmation
|
2017-09-23 10:59:13 -07:00
|
|
|
|
2017-09-23 11:45:16 -07:00
|
|
|
s.client.logQuery(conf_find_product, product)
|
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
row := s.client.db.QueryRow(conf_find_product, product)
|
|
|
|
err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
|
2017-09-23 10:59:13 -07:00
|
|
|
if err != nil {
|
2017-09-23 11:45:16 -07:00
|
|
|
s.client.logError(conf_find_product, err, product)
|
2017-09-23 10:59:13 -07:00
|
|
|
|
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
2017-09-23 10:19:43 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
func (s *ConfirmationService) CreateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
|
|
|
var conf tacitus.Confirmation
|
2017-09-23 10:59:13 -07:00
|
|
|
|
2017-09-23 11:45:16 -07:00
|
|
|
s.client.logQuery(conf_insert, c.Product, c.LastTradeId)
|
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
row := s.client.db.QueryRow(conf_insert, c.Product, c.LastTradeId)
|
|
|
|
err := row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
|
2017-09-23 10:59:13 -07:00
|
|
|
if err != nil {
|
2017-09-23 11:45:16 -07:00
|
|
|
s.client.logError(conf_insert, err, c.Product, c.LastTradeId)
|
|
|
|
|
2017-09-23 10:59:13 -07:00
|
|
|
return conf, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return conf, nil
|
2017-09-23 10:19:43 -07:00
|
|
|
}
|
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
func (s *ConfirmationService) UpdateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
2017-10-05 21:53:02 -07:00
|
|
|
s.client.logQuery(conf_update, c.LastTradeId, c.Product)
|
2017-09-23 11:45:16 -07:00
|
|
|
|
2017-09-24 17:30:01 -07:00
|
|
|
_, err := s.client.db.Exec(conf_update, c.LastTradeId, c.Product)
|
2017-09-23 11:45:16 -07:00
|
|
|
if err != nil {
|
|
|
|
s.client.logError(conf_update, err, c.LastTradeId, c.Product)
|
|
|
|
}
|
|
|
|
|
2017-09-23 10:59:13 -07:00
|
|
|
return c, err
|
2017-09-23 10:19:43 -07:00
|
|
|
}
|