Merge pull request 'Feat: Openfoodfacts integration' (#50) from openfoodfacts into master

Reviewed-on: #50
This commit is contained in:
Theo Technicguy 2023-08-24 18:06:59 +02:00 committed by Naja2Git
commit d62253cc9f
Signed by: Naja2Git
GPG Key ID: C4408DCE12738204
5 changed files with 73 additions and 3 deletions

1
go.mod
View File

@ -7,6 +7,7 @@ require github.com/mattn/go-sqlite3 v1.14.17
require (
github.com/gorilla/mux v1.8.0
github.com/justinas/alice v1.2.0
github.com/openfoodfacts/openfoodfacts-go v1.0.0
github.com/rs/zerolog v1.30.0
github.com/stretchr/testify v1.8.4
)

2
go.sum
View File

@ -13,6 +13,8 @@ github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/openfoodfacts/openfoodfacts-go v1.0.0 h1:fBVpQ1Jl2WAWcZbUtH9ffaQovyaBdc5O3/EX/dlzq5g=
github.com/openfoodfacts/openfoodfacts-go v1.0.0/go.mod h1:LxA3Y1wfVgR7QC9WgnlH69KjsmIqM5w6W5m8AwONnYQ=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=

62
logic/openfoodfacts.go Normal file
View File

@ -0,0 +1,62 @@
/**
* file: logic/openfoodfacts.go
* author: theo technicguy
* license: apache 2.0
*
* This file implements the connection logic to the
* openfoodfacts service
*/
package logic
import (
"fmt"
"time"
"git.licolas.net/hoffman/server/types"
"github.com/openfoodfacts/openfoodfacts-go"
)
var (
offAPI *openfoodfacts.Client
)
func setupOFFAPI() {
offapi := openfoodfacts.NewClient("world", "", "")
offAPI = &offapi
offAPI.Timeout(5 * time.Second)
}
func getProductInformation(ean uint) (*types.Product, error) {
if offAPI == nil {
setupOFFAPI()
}
off, err := offAPI.Product(fmt.Sprintf("%d", ean))
if err != nil {
return nil, err
}
return &types.Product{
Id: 0,
EAN: ean,
Name: off.ProductName,
}, nil
}
func fillBlankFields(product *types.Product) error {
if product.EAN == 0 {
return types.ErrInvalidSearch
}
off, err := getProductInformation(product.EAN)
if err != nil {
return err
}
if product.Name == "" {
product.Name = off.Name
}
return nil
}

View File

@ -30,6 +30,11 @@ func (l *logic) checkProductExists(id uint) (*types.Product, error) {
func (l *logic) CreateProduct(product *types.Product) (err error) {
logger.Trace().Any("product", product).Msg("creating product")
if err := fillBlankFields(product); err != nil {
logger.Warn().Any("product", product).Err(err).Msg("error updating data")
}
return l.dsrc.InsertProduct(product)
}

View File

@ -82,14 +82,14 @@ func (ps *ProductSearch) BuildTagSearch() string {
func (ps *ProductSearch) BuildSearch() string {
sql := `SELECT DISTINCT p.*
FROM Product p
JOIN ProductTagging ptg ON p.ProductID = ptg.ProductID
JOIN ProductTag pt ON ptg.TagID = pt.TagID
LEFT OUTER JOIN ProductTagging ptg ON p.ProductID = ptg.ProductID
LEFT OUTER JOIN ProductTag pt ON ptg.TagID = pt.TagID
`
filters := []string{}
if ps.Ean != "" {
filters = append(filters, "p.ean LIKE '%%"+ps.Ean+"%%'")
filters = append(filters, fmt.Sprintf("p.ean LIKE '%%%s%%'", ps.Ean))
}
if len(ps.Names) > 0 {
filters = append(filters, ps.BuildNamesSearch())