From da895c78e3dc68e1435bbbbb5a3a5e9b12e6951b Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Sat, 23 Sep 2017 10:59:13 -0700 Subject: [PATCH] Implement confirmation service --- postgres/confirmation_service.go | 43 +++++++++++++++++++++++++++++--- 1 file changed, 40 insertions(+), 3 deletions(-) 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 }