47 lines
1 KiB
Go
47 lines
1 KiB
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
"github.com/vingarcia/ksql"
|
|
)
|
|
|
|
func (d *Database) FindProductByBarcode(system string, barcode string) (*Product, error) {
|
|
|
|
ctx := context.Background()
|
|
ctx = ksql.InjectLogger(ctx, ksql.Logger)
|
|
|
|
var product Product
|
|
err := d.db.QueryOne(ctx, &product, "FROM products WHERE system = $1 AND barcode = $2", system, barcode)
|
|
|
|
// No error because product may just not exist.
|
|
if err == ksql.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &product, nil
|
|
}
|
|
|
|
// Doesnt handle checking if product exists.
|
|
func (d *Database) CreateProduct(system string, barcode string) (*Product, error) {
|
|
ctx := context.Background()
|
|
var product = Product{
|
|
System: system,
|
|
Barcode: barcode,
|
|
Created_at: time.Now(),
|
|
}
|
|
|
|
log.Debugf("made new product: %v", product)
|
|
|
|
if err := d.db.Insert(ctx, ProductsTable, &product); err != nil {
|
|
return nil, fmt.Errorf("failed to insert new product: %w", err)
|
|
}
|
|
|
|
return &product, nil
|
|
}
|