ProductBarcode: return an array of BarcodeProduct instead of one

This commit is contained in:
Crumb Owl
2025-07-02 11:54:24 +02:00
parent 09f29d82f4
commit a3c13a8a74
8 changed files with 197 additions and 36 deletions

View File

@@ -77,28 +77,6 @@ func (ctrl *V1Controller) HandleGenerateQRCode() errchain.HandlerFunc {
} }
} }
type BarcodeProduct struct {
SearchEngineName string `json:"search_engine_name"`
//Name string `json:"name" validate:"required,min=1,max=255"`
//Description string `json:"description" validate:"max=1000"`
// Identifications
ModelNumber string `json:"modelNumber"`
Manufacturer string `json:"manufacturer"`
// Extras
Country string `json:"notes"`
Barcode string `json:"barcode"`
// TODO: add image attachement
// TODO: add asin?
ImageURL string `json:"imageURL"`
ImageBase64 string `json:"imageBase64"`
Item repo.ItemCreate `json:"item"`
}
/* /*
ImportRef string `json:"-"` ImportRef string `json:"-"`
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"` ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
@@ -191,7 +169,7 @@ type BARCODESPIDER_COMResponse struct {
// @Tags Items // @Tags Items
// @Produce json // @Produce json
// @Param data query string false "barcode to be searched" // @Param data query string false "barcode to be searched"
// @Success 200 {object} repo.ItemCreate // @Success 200 {object} []repo.BarcodeProduct
// @Router /v1/getproductfromean [GET] // @Router /v1/getproductfromean [GET]
// @Security Bearer // @Security Bearer
func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) errchain.HandlerFunc { func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) errchain.HandlerFunc {
@@ -217,13 +195,13 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
log.Info().Msg("========================" + q.EAN) log.Info().Msg("========================" + q.EAN)
// Search on UPCITEMDB // Search on UPCITEMDB
var products []BarcodeProduct var products []repo.BarcodeProduct
// www.ean-search.org/: not free // www.ean-search.org/: not free
// Example code: dewalt 5035048748428 // Example code: dewalt 5035048748428
upcitemdb := func(iEan string) ([]BarcodeProduct, error) { upcitemdb := func(iEan string) ([]repo.BarcodeProduct, error) {
resp, err := http.Get("https://api.upcitemdb.com/prod/trial/lookup?upc=" + iEan) resp, err := http.Get("https://api.upcitemdb.com/prod/trial/lookup?upc=" + iEan)
if err != nil { if err != nil {
return nil, err return nil, err
@@ -243,10 +221,10 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
log.Error().Msg("Can not unmarshal JSON") log.Error().Msg("Can not unmarshal JSON")
} }
var res []BarcodeProduct var res []repo.BarcodeProduct
for _, it := range result.Items { for _, it := range result.Items {
var p BarcodeProduct var p repo.BarcodeProduct
p.SearchEngineName = "upcitemdb.com" p.SearchEngineName = "upcitemdb.com"
p.Barcode = iEan p.Barcode = iEan
@@ -270,7 +248,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
} }
// Barcode spider implementation // Barcode spider implementation
barcodespider := func(tokenAPI string, iEan string) ([]BarcodeProduct, error) { barcodespider := func(tokenAPI string, iEan string) ([]repo.BarcodeProduct, error) {
if len(tokenAPI) == 0 { if len(tokenAPI) == 0 {
return nil, errors.New("no api token configured for barcodespider") return nil, errors.New("no api token configured for barcodespider")
@@ -307,7 +285,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
} }
// TODO: check 200 code on HTTP repsonse. // TODO: check 200 code on HTTP repsonse.
var p BarcodeProduct var p repo.BarcodeProduct
p.Barcode = iEan p.Barcode = iEan
p.SearchEngineName = "barcodespider.com" p.SearchEngineName = "barcodespider.com"
p.Item.Name = result.ItemAttributes.Title p.Item.Name = result.ItemAttributes.Title
@@ -316,7 +294,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
p.ModelNumber = result.ItemAttributes.Model p.ModelNumber = result.ItemAttributes.Model
p.ImageURL = result.ItemAttributes.Image p.ImageURL = result.ItemAttributes.Image
var res []BarcodeProduct var res []repo.BarcodeProduct
res = append(res, p) res = append(res, p)
return res, nil return res, nil
@@ -383,7 +361,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
if len(products) != 0 { if len(products) != 0 {
// Return only the first result for now. Enhance this with a dedicated dialog // Return only the first result for now. Enhance this with a dedicated dialog
// displaying all the references found? // displaying all the references found?
return json.NewEncoder(w).Encode(products[0]) return json.NewEncoder(w).Encode(products)
} }
return nil return nil

View File

@@ -221,7 +221,10 @@ const docTemplate = `{
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/repo.ItemCreate" "type": "array",
"items": {
"$ref": "#/definitions/repo.BarcodeProduct"
}
} }
} }
} }
@@ -3095,6 +3098,37 @@ const docTemplate = `{
"TypeTime" "TypeTime"
] ]
}, },
"repo.BarcodeProduct": {
"type": "object",
"properties": {
"barcode": {
"type": "string"
},
"imageBase64": {
"type": "string"
},
"imageURL": {
"type": "string"
},
"item": {
"$ref": "#/definitions/repo.ItemCreate"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"description": "Identifications",
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"search_engine_name": {
"type": "string"
}
}
},
"repo.Group": { "repo.Group": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -219,7 +219,10 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/repo.ItemCreate" "type": "array",
"items": {
"$ref": "#/definitions/repo.BarcodeProduct"
}
} }
} }
} }
@@ -3093,6 +3096,37 @@
"TypeTime" "TypeTime"
] ]
}, },
"repo.BarcodeProduct": {
"type": "object",
"properties": {
"barcode": {
"type": "string"
},
"imageBase64": {
"type": "string"
},
"imageURL": {
"type": "string"
},
"item": {
"$ref": "#/definitions/repo.ItemCreate"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"description": "Identifications",
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"search_engine_name": {
"type": "string"
}
}
},
"repo.Group": { "repo.Group": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -646,6 +646,27 @@ definitions:
- TypeNumber - TypeNumber
- TypeBoolean - TypeBoolean
- TypeTime - TypeTime
repo.BarcodeProduct:
properties:
barcode:
type: string
imageBase64:
type: string
imageURL:
type: string
item:
$ref: '#/definitions/repo.ItemCreate'
manufacturer:
type: string
modelNumber:
description: Identifications
type: string
notes:
description: Extras
type: string
search_engine_name:
type: string
type: object
repo.Group: repo.Group:
properties: properties:
createdAt: createdAt:
@@ -1564,7 +1585,9 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/repo.ItemCreate' items:
$ref: '#/definitions/repo.BarcodeProduct'
type: array
security: security:
- Bearer: [] - Bearer: []
summary: Search EAN from Barcode summary: Search EAN from Barcode

View File

@@ -0,0 +1,18 @@
package repo
type BarcodeProduct struct {
SearchEngineName string `json:"search_engine_name"`
// Identifications
ModelNumber string `json:"modelNumber"`
Manufacturer string `json:"manufacturer"`
// Extras
Country string `json:"notes"`
Barcode string `json:"barcode"`
ImageURL string `json:"imageURL"`
ImageBase64 string `json:"imageBase64"`
Item ItemCreate `json:"item"`
}

View File

@@ -219,7 +219,10 @@
"200": { "200": {
"description": "OK", "description": "OK",
"schema": { "schema": {
"$ref": "#/definitions/repo.ItemCreate" "type": "array",
"items": {
"$ref": "#/definitions/repo.BarcodeProduct"
}
} }
} }
} }
@@ -3093,6 +3096,37 @@
"TypeTime" "TypeTime"
] ]
}, },
"repo.BarcodeProduct": {
"type": "object",
"properties": {
"barcode": {
"type": "string"
},
"imageBase64": {
"type": "string"
},
"imageURL": {
"type": "string"
},
"item": {
"$ref": "#/definitions/repo.ItemCreate"
},
"manufacturer": {
"type": "string"
},
"modelNumber": {
"description": "Identifications",
"type": "string"
},
"notes": {
"description": "Extras",
"type": "string"
},
"search_engine_name": {
"type": "string"
}
}
},
"repo.Group": { "repo.Group": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@@ -646,6 +646,27 @@ definitions:
- TypeNumber - TypeNumber
- TypeBoolean - TypeBoolean
- TypeTime - TypeTime
repo.BarcodeProduct:
properties:
barcode:
type: string
imageBase64:
type: string
imageURL:
type: string
item:
$ref: '#/definitions/repo.ItemCreate'
manufacturer:
type: string
modelNumber:
description: Identifications
type: string
notes:
description: Extras
type: string
search_engine_name:
type: string
type: object
repo.Group: repo.Group:
properties: properties:
createdAt: createdAt:
@@ -1564,7 +1585,9 @@ paths:
"200": "200":
description: OK description: OK
schema: schema:
$ref: '#/definitions/repo.ItemCreate' items:
$ref: '#/definitions/repo.BarcodeProduct'
type: array
security: security:
- Bearer: [] - Bearer: []
summary: Search EAN from Barcode summary: Search EAN from Barcode

View File

@@ -451,6 +451,23 @@ export interface EntUserEdges {
notifiers: EntNotifier[]; notifiers: EntNotifier[];
} }
export interface BarcodeProduct {
barcode: string;
imageBase64: string;
/**
* TODO: add image attachement
* TODO: add asin?
*/
imageURL: string;
item: ItemCreate;
manufacturer: string;
/** Identifications */
modelNumber: string;
/** Extras */
notes: string;
search_engine_name: string;
}
export interface Group { export interface Group {
createdAt: Date | string; createdAt: Date | string;
currency: string; currency: string;