diff --git a/postgres/confirmation_service.go b/postgres/confirmation_service.go index b470f4e..a182d4b 100644 --- a/postgres/confirmation_service.go +++ b/postgres/confirmation_service.go @@ -6,16 +6,53 @@ import ( "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 { logger tacitus.Logger client *client } -func (c *ConfirmationService) Confirmation(product·string)·(*Confirmation,·error) { +func (s *ConfirmationService) Confirmation(product string) (Confirmation, error) { + var conf Confirmation + + res, err := s.client.QueryRow(conf_find_product, product) + if err != nil { + return conf, err + } + + err = row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId) + if err != nil { + return conf, err + } + + return conf, nil } -func (c *ConfirmationService) CreateConfirmation(c·*Confirmation)·error { +func (s *ConfirmationService) CreateConfirmation(c Confirmation) (Confirmation, error) { + var conf Confirmation + + res, err := s.client.QueryRow(conf_insert, c.Product, c.LastTradeId) + if err != nil { + return conf, err + } + + err = row.Scan(&conf.Id, &conf.Product, &conf.LastTradeId) + if err != nil { + return conf, err + } + + return conf, nil } -func (c *ConfirmationService) UpdateConfirmation(c·*Confirmation)·error { +func (s *ConfirmationService) UpdateConfirmation(c Confirmation) (Confirmation, error) { + res, err := s.client.Exec(conf_update, c.LastTradeId, c.Product) + return c, err }