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
|
||||
}
|
||||
|
||||
func (field *InputField) IntegerOrNil() interface{} {
|
||||
if field.Val != "" {
|
||||
if i := field.Integer(); i > 0 {
|
||||
return i
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (field *InputField) Float64() float64 {
|
||||
value, err := strconv.ParseFloat(field.Val, 64)
|
||||
if err != nil {
|
||||
|
|
|
@ -48,15 +48,9 @@ func (src EditedInvoiceProductArray) EncodeBinary(ci *pgtype.ConnInfo, buf []byt
|
|||
}
|
||||
var values [][]interface{}
|
||||
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{}{
|
||||
invoiceProductId,
|
||||
form.ProductId.Val,
|
||||
form.InvoiceProductId.IntegerOrNil(),
|
||||
form.ProductId.IntegerOrNil(),
|
||||
form.Name.Val,
|
||||
form.Description.Val,
|
||||
form.Price.Val,
|
||||
|
|
Loading…
Reference in New Issue