63 lines
1.7 KiB
Go
63 lines
1.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"git.kevincotugno.com/kcotugno/tacitus"
|
|
)
|
|
|
|
const (
|
|
conf_columns = `product, type, last_id`
|
|
conf_insert = `INSERT INTO confirmations (` + conf_columns +
|
|
`) VALUES ($1, $2, $3) RETURNING id, ` + conf_columns + `;`
|
|
conf_find_product = `SELECT id, ` + conf_columns + ` FROM confirmations ` +
|
|
`WHERE product = $1 AND type = $2;`
|
|
conf_update = `UPDATE confirmations SET product = $1, type = $2, last_id = $3 ` +
|
|
`WHERE id = $4;`
|
|
)
|
|
|
|
type ConfirmationService struct {
|
|
client *Client
|
|
}
|
|
|
|
func (s *ConfirmationService) Confirmation(product, c_type string) (tacitus.Confirmation, error) {
|
|
var conf tacitus.Confirmation
|
|
|
|
s.client.logQuery(conf_find_product, product, c_type)
|
|
|
|
row := s.client.db.QueryRow(conf_find_product, product, c_type)
|
|
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
|
|
if err != nil {
|
|
s.client.logError(conf_find_product, err, product, c_type)
|
|
|
|
return conf, err
|
|
}
|
|
|
|
return conf, nil
|
|
}
|
|
|
|
func (s *ConfirmationService) CreateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
|
var conf tacitus.Confirmation
|
|
|
|
s.client.logQuery(conf_insert, c.Product, c.Type, c.LastId)
|
|
|
|
row := s.client.db.QueryRow(conf_insert, c.Product, c.Type, c.LastId)
|
|
err := row.Scan(&conf.Id, &conf.Product, &conf.Type, &conf.LastId)
|
|
if err != nil {
|
|
s.client.logError(conf_insert, err, c.Product, c.Type, c.LastId)
|
|
|
|
return conf, err
|
|
}
|
|
|
|
return conf, nil
|
|
}
|
|
|
|
func (s *ConfirmationService) UpdateConfirmation(c tacitus.Confirmation) (tacitus.Confirmation, error) {
|
|
s.client.logQuery(conf_update, c.Product, c.Type, c.LastId, c.Id)
|
|
|
|
_, err := s.client.db.Exec(conf_update, c.Product, c.Type, c.LastId, c.Id)
|
|
if err != nil {
|
|
s.client.logError(conf_update, err, c.Product, c.Type, c.LastId, c.Id)
|
|
}
|
|
|
|
return c, err
|
|
}
|