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:
jordi fita mas 2023-05-22 11:06:06 +02:00
parent 46b079cb0b
commit bbabf5c733
2 changed files with 11 additions and 8 deletions

View File

@ -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 {

View File

@ -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,