mirror of
https://github.com/sysadminsmedia/homebox.git
synced 2025-12-21 13:23:14 +01:00
ProductBarcode: return an array of BarcodeProduct instead of one
This commit is contained in:
@@ -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:"-"`
|
||||
ParentID uuid.UUID `json:"parentId" extensions:"x-nullable"`
|
||||
@@ -191,7 +169,7 @@ type BARCODESPIDER_COMResponse struct {
|
||||
// @Tags Items
|
||||
// @Produce json
|
||||
// @Param data query string false "barcode to be searched"
|
||||
// @Success 200 {object} repo.ItemCreate
|
||||
// @Success 200 {object} []repo.BarcodeProduct
|
||||
// @Router /v1/getproductfromean [GET]
|
||||
// @Security Bearer
|
||||
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)
|
||||
|
||||
// Search on UPCITEMDB
|
||||
var products []BarcodeProduct
|
||||
var products []repo.BarcodeProduct
|
||||
|
||||
// www.ean-search.org/: not free
|
||||
|
||||
// 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)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -243,10 +221,10 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
|
||||
log.Error().Msg("Can not unmarshal JSON")
|
||||
}
|
||||
|
||||
var res []BarcodeProduct
|
||||
var res []repo.BarcodeProduct
|
||||
|
||||
for _, it := range result.Items {
|
||||
var p BarcodeProduct
|
||||
var p repo.BarcodeProduct
|
||||
p.SearchEngineName = "upcitemdb.com"
|
||||
p.Barcode = iEan
|
||||
|
||||
@@ -270,7 +248,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
|
||||
}
|
||||
|
||||
// Barcode spider implementation
|
||||
barcodespider := func(tokenAPI string, iEan string) ([]BarcodeProduct, error) {
|
||||
barcodespider := func(tokenAPI string, iEan string) ([]repo.BarcodeProduct, error) {
|
||||
|
||||
if len(tokenAPI) == 0 {
|
||||
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.
|
||||
var p BarcodeProduct
|
||||
var p repo.BarcodeProduct
|
||||
p.Barcode = iEan
|
||||
p.SearchEngineName = "barcodespider.com"
|
||||
p.Item.Name = result.ItemAttributes.Title
|
||||
@@ -316,7 +294,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
|
||||
p.ModelNumber = result.ItemAttributes.Model
|
||||
p.ImageURL = result.ItemAttributes.Image
|
||||
|
||||
var res []BarcodeProduct
|
||||
var res []repo.BarcodeProduct
|
||||
res = append(res, p)
|
||||
|
||||
return res, nil
|
||||
@@ -383,7 +361,7 @@ func (ctrl *V1Controller) HandleProductSearchEAN(conf config.BarcodeAPIConf) err
|
||||
if len(products) != 0 {
|
||||
// Return only the first result for now. Enhance this with a dedicated dialog
|
||||
// displaying all the references found?
|
||||
return json.NewEncoder(w).Encode(products[0])
|
||||
return json.NewEncoder(w).Encode(products)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
@@ -221,7 +221,10 @@ const docTemplate = `{
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/repo.ItemCreate"
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/repo.BarcodeProduct"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3095,6 +3098,37 @@ const docTemplate = `{
|
||||
"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": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -219,7 +219,10 @@
|
||||
"200": {
|
||||
"description": "OK",
|
||||
"schema": {
|
||||
"$ref": "#/definitions/repo.ItemCreate"
|
||||
"type": "array",
|
||||
"items": {
|
||||
"$ref": "#/definitions/repo.BarcodeProduct"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3093,6 +3096,37 @@
|
||||
"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": {
|
||||
"type": "object",
|
||||
"properties": {
|
||||
|
||||
@@ -646,6 +646,27 @@ definitions:
|
||||
- TypeNumber
|
||||
- TypeBoolean
|
||||
- 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:
|
||||
properties:
|
||||
createdAt:
|
||||
@@ -1564,7 +1585,9 @@ paths:
|
||||
"200":
|
||||
description: OK
|
||||
schema:
|
||||
$ref: '#/definitions/repo.ItemCreate'
|
||||
items:
|
||||
$ref: '#/definitions/repo.BarcodeProduct'
|
||||
type: array
|
||||
security:
|
||||
- Bearer: []
|
||||
summary: Search EAN from Barcode
|
||||
|
||||
18
backend/internal/data/repo/repo_product_search.go
Normal file
18
backend/internal/data/repo/repo_product_search.go
Normal 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"`
|
||||
}
|
||||
Reference in New Issue
Block a user