41 lines
956 B
Go
41 lines
956 B
Go
package application
|
|
|
|
import (
|
|
"errors"
|
|
"vegan-barcode/internal/database"
|
|
"vegan-barcode/internal/models"
|
|
)
|
|
|
|
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
|
|
}
|
|
|
|
func (a *Application) CreateClaim(system string, barcode string, form models.UserClaimForm) (*database.UserClaim, error) {
|
|
product, err := a.db.FindProductByBarcode(system, barcode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
// no product, create new
|
|
if product == nil {
|
|
product, err = a.db.CreateProduct(system, barcode)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
return a.db.CreateUserClaim(product.Id, form)
|
|
}
|