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

63 lines
1.7 KiB
Go
Raw Normal View History

2017-09-23 10:19:43 -07:00
package postgres
import (
"git.kevincotugno.com/kcotugno/tacitus"
2017-09-23 10:19:43 -07:00
)
2017-09-23 10:59:13 -07:00
const (
2017-10-10 17:22:00 -07:00
conf_columns = `product, type, last_id`
2017-09-23 10:59:13 -07:00
conf_insert = `INSERT INTO confirmations (` + conf_columns +
2017-10-10 17:22:00 -07:00
`) VALUES ($1, $2, $3) RETURNING id, ` + conf_columns + `;`
2017-10-05 21:53:02 -07:00
conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations ` +
2017-10-10 17:22:00 -07:00
`WHERE product = $1 AND type = $2;`
conf_update = `UPDATE confirmations SET product = $1, type = $2, last_id = $3 ` +
`WHERE id = $4;`
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-10-10 17:22:00 -07:00
func (s *ConfirmationService) Confirmation(product, c_type string) (tacitus.Confirmation, error) {
2017-09-24 17:30:01 -07:00
var conf tacitus.Confirmation
2017-09-23 10:59:13 -07:00
2017-10-10 17:22:00 -07:00
s.client.logQuery(conf_find_product, product, c_type)
2017-09-23 11:45:16 -07:00
2017-10-10 17:22:00 -07:00
row := s.client.db.QueryRow(conf_find_product, product, c_type)
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
2017-09-23 10:59:13 -07:00
if err != nil {
2017-10-10 17:22:00 -07:00
s.client.logError(conf_find_product, err, product, c_type)
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-10-10 17:22:00 -07:00
s.client.logQuery(conf_insert, c.Product, c.Type, c.LastId)
2017-09-23 11:45:16 -07:00
2017-10-10 17:22:00 -07:00
row := s.client.db.QueryRow(conf_insert, c.Product, c.Type, c.LastId)
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
2017-09-23 10:59:13 -07:00
if err != nil {
2017-10-10 17:22:00 -07:00
s.client.logError(conf_insert, err, c.Product, c.Type, c.LastId)
2017-09-23 11:45:16 -07:00
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-10 17:22:00 -07:00
s.client.logQuery(conf_update, c.Product, c.Type, c.LastId, c.Id)
2017-09-23 11:45:16 -07:00
2017-10-10 17:22:00 -07:00
_, err := s.client.db.Exec(conf_update, c.Product, c.Type, c.LastId, c.Id)
2017-09-23 11:45:16 -07:00
if err != nil {
2017-10-10 17:22:00 -07:00
s.client.logError(conf_update, err, c.Product, c.Type, c.LastId, c.Id)
2017-09-23 11:45:16 -07:00
}
2017-09-23 10:59:13 -07:00
return c, err
2017-09-23 10:19:43 -07:00
}