Use a null as product ID when adding new products to invoices
Otherwise, pgx (rightfully) tries to convert a "" into a integer, as this is the field’s type, cannot, and panics with an error. Added a IntegerOrNull method to FormField because this is exactly the same that happens with the invoiceProductId, and made no sense to have to do the logic twice, or in a function inside form.
This commit is contained in:
parent
46b079cb0b
commit
bbabf5c733
|
@ -70,6 +70,15 @@ func (field *InputField) Integer() int {
|
||||||
return value
|
return value
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (field *InputField) IntegerOrNil() interface{} {
|
||||||
|
if field.Val != "" {
|
||||||
|
if i := field.Integer(); i > 0 {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (field *InputField) Float64() float64 {
|
func (field *InputField) Float64() float64 {
|
||||||
value, err := strconv.ParseFloat(field.Val, 64)
|
value, err := strconv.ParseFloat(field.Val, 64)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -48,15 +48,9 @@ func (src EditedInvoiceProductArray) EncodeBinary(ci *pgtype.ConnInfo, buf []byt
|
||||||
}
|
}
|
||||||
var values [][]interface{}
|
var values [][]interface{}
|
||||||
for _, form := range src {
|
for _, form := range src {
|
||||||
var invoiceProductId interface{} = nil
|
|
||||||
if form.InvoiceProductId.Val != "" {
|
|
||||||
if id := form.InvoiceProductId.Integer(); id > 0 {
|
|
||||||
invoiceProductId = id
|
|
||||||
}
|
|
||||||
}
|
|
||||||
values = append(values, []interface{}{
|
values = append(values, []interface{}{
|
||||||
invoiceProductId,
|
form.InvoiceProductId.IntegerOrNil(),
|
||||||
form.ProductId.Val,
|
form.ProductId.IntegerOrNil(),
|
||||||
form.Name.Val,
|
form.Name.Val,
|
||||||
form.Description.Val,
|
form.Description.Val,
|
||||||
form.Price.Val,
|
form.Price.Val,
|
||||||
|
|
Loading…
Reference in New Issue