refactor(services): Barcodes always accept strings
This commit is contained in:
parent
5dbe7816cd
commit
d8fb9ea2fd
@ -6,7 +6,6 @@ import (
|
||||
"errors"
|
||||
"html/template"
|
||||
"image/png"
|
||||
"strconv"
|
||||
|
||||
"github.com/boombuler/barcode"
|
||||
"github.com/boombuler/barcode/code128"
|
||||
@ -22,22 +21,20 @@ type Templater interface {
|
||||
type DefaultTemplater struct {
|
||||
}
|
||||
|
||||
func idToEan13(id int, prefix string) (string, error) {
|
||||
idStr := strconv.Itoa(id)
|
||||
|
||||
if len(idStr) > 12 {
|
||||
func idToEan13(id string, prefix string) (string, error) {
|
||||
if len(id) > 12 {
|
||||
return "", errors.New("id too long")
|
||||
}
|
||||
|
||||
for len(idStr) < 11 {
|
||||
idStr = "0" + idStr
|
||||
for len(id) < 11 {
|
||||
id = "0" + id
|
||||
}
|
||||
idStr = prefix + idStr
|
||||
id = prefix + id
|
||||
|
||||
return idStr, nil
|
||||
return id, nil
|
||||
}
|
||||
|
||||
func (t *DefaultTemplater) GenerateBarcode(code int, format string, prefix string) (string, error) {
|
||||
func (t *DefaultTemplater) GenerateBarcode(code string, format string, prefix string) (string, error) {
|
||||
var generatedCode barcode.Barcode
|
||||
var err error
|
||||
|
||||
@ -53,13 +50,13 @@ func (t *DefaultTemplater) GenerateBarcode(code int, format string, prefix strin
|
||||
}
|
||||
break
|
||||
case "code128":
|
||||
generatedCode, err = code128.Encode(prefix + strconv.Itoa(code))
|
||||
generatedCode, err = code128.Encode(prefix + code)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
break
|
||||
case "qr":
|
||||
generatedCode, err = qr.Encode(prefix+strconv.Itoa(code), qr.M, qr.Numeric)
|
||||
generatedCode, err = qr.Encode(prefix+code, qr.M, qr.AlphaNumeric)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
@ -56,7 +56,7 @@
|
||||
</div>
|
||||
<div class="column">
|
||||
<img style="vertical-align: revert; margin-top: auto; object-fit: cover; max-height: 2cm;"
|
||||
src="data:image/png;base64,{{ barcode .ID $.BarcodeFormat $.BarcodePrefix }}" />
|
||||
src="data:image/png;base64,{{ barcode (printf "%d" .ID) $.BarcodeFormat $.BarcodePrefix }}" />
|
||||
</div>
|
||||
</div>
|
||||
<div class="columns" style="padding-top: 1rem;">
|
||||
|
@ -55,7 +55,7 @@
|
||||
</div>
|
||||
<div class="column">
|
||||
<img style="vertical-align: revert; margin-top: auto; object-fit: cover; max-height: 2cm;"
|
||||
src="data:image/png;base64,{{ barcode .ID $.BarcodeFormat $.BarcodePrefix }}"/>
|
||||
src="data:image/png;base64,{{ barcode (printf "%d" .ID) $.BarcodeFormat $.BarcodePrefix }}"/>
|
||||
</div>
|
||||
</div>
|
||||
<div class="columns" style="padding-top: 1rem;">
|
||||
|
Loading…
x
Reference in New Issue
Block a user