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

View File

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