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/confirmation_service.go

71 lines
1.7 KiB
Go

package postgres
import (
"database/sql"
"github.com/kcotugno/tacitus"
)
const (
conf_columns = `product, last_trade_id`
conf_insert = `INSERT INTO confirmations (` + conf_columns +
`) VALUES ($1, $2) RETURNING id, ` + conf_columns + `;`
conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations` +
`WHERE product = $1`
conf_update = `UPDATE confirmation SET last_trade_id = $1 WHERE product = $2`
)
type ConfirmationService struct {
client *client
}
func (s *ConfirmationService) Confirmation(product string) (Confirmation, error) {
var conf Confirmation
s.client.logQuery(conf_find_product, product)
res, err := s.client.QueryRow(conf_find_product, product)
if err != nil {
s.client.logError(conf_find_product, err, product)
return conf, err
}
err = row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
if err != nil {
return conf, err
}
return conf, nil
}
func (s *ConfirmationService) CreateConfirmation(c Confirmation) (Confirmation, error) {
var conf Confirmation
s.client.logQuery(conf_insert, c.Product, c.LastTradeId)
res, err := s.client.QueryRow(conf_insert, c.Product, c.LastTradeId)
if err != nil {
s.client.logError(conf_insert, err, c.Product, c.LastTradeId)
return conf, err
}
err = row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId)
if err != nil {
return conf, err
}
return conf, nil
}
func (s *ConfirmationService) UpdateConfirmation(c Confirmation) (Confirmation, error) {
s.client.logQuery(conf_insert, c.LastTradeId, c.Product)
res, err := s.client.Exec(conf_update, c.LastTradeId, c.Product)
if err != nil {
s.client.logError(conf_update, err, c.LastTradeId, c.Product)
}
return c, err
}