38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package application
|
|
|
|
import (
|
|
"errors"
|
|
"vegan-barcode/internal/database"
|
|
"vegan-barcode/internal/models"
|
|
)
|
|
|
|
// GetClaims doesn't automatically create a new product because maybe the user mistyped.
|
|
// Only create a new product when they want to add claims.
|
|
func (a *Application) GetClaims(system string, barcode string) (*models.ProductClaims, error) {
|
|
product, err := a.db.FindProductByBarcode(system, barcode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if product == nil {
|
|
return nil, errors.New("product not found")
|
|
}
|
|
|
|
claims, err := a.db.FindClaimsByProductID(product.Id)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &models.ProductClaims{Id: product.Id, Claims: claims}, nil
|
|
}
|
|
|
|
// CreateUserClaim gets the product ID or creates a new entry if not found,
|
|
// then calls InsertUserClaim to create the claim object and put it in the database.
|
|
func (a *Application) CreateUserClaim(system string, barcode string, form models.UserClaimForm) (*database.UserClaim, error) {
|
|
product, err := a.db.FindOrCreateProduct(system, barcode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return a.db.InsertUserClaim(product.Id, form)
|
|
}
|