40 lines
816 B
Go
40 lines
816 B
Go
package database
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"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)
|
|
if err == ksql.ErrRecordNotFound {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &product, nil
|
|
}
|
|
|
|
func (d *Database) CreateProduct(system string, barcode string) (*Product, error) {
|
|
ctx := context.Background()
|
|
var product = Product{
|
|
System: system,
|
|
Barcode: barcode,
|
|
Created_at: time.Now(),
|
|
}
|
|
|
|
if err := d.db.Insert(ctx, ProductsTable, &product); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &product, nil
|
|
}
|