Simple validation for negative values of integer fields

This commit is contained in:
J Sen Ooi 2023-12-17 19:48:57 +08:00
parent 704b5d27d8
commit 0d4dc2cc12

View file

@ -185,8 +185,12 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data io.Re
finished := 0
var errorMessage string
for i := range sheet.Rows {
row := sheet.Rows[i]
var hasNegativeValues bool
createRequired := true
@ -202,6 +206,25 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data io.Re
createRequired = false
}
}
// Ooi J Sen
// Check integer fields for negative values
if row.Quantity < 0 {
errorMessage += fmt.Sprintf("Negative quantity at row %d\n", i+1)
hasNegativeValues = true
}
if row.PurchasePrice < 0 {
errorMessage += fmt.Sprintf("Negative purchase price at row %d\n", i+1)
hasNegativeValues = true
}
if row.SoldPrice < 0 {
errorMessage += fmt.Sprintf("Negative sold price at row %d\n", i+1)
hasNegativeValues = true
}
if (hasNegativeValues) {
continue
}
// ========================================
// Pre-Create Labels as necessary
@ -350,6 +373,15 @@ func (svc *ItemService) CsvImport(ctx context.Context, GID uuid.UUID, data io.Re
finished++
}
// Ooi J Sen
// Display error messages in console
if errorMessage != "" {
// Log or handle the error message here
fmt.Println("Error Messages:")
fmt.Println(errorMessage)
return 0, fmt.Errorf("Errors detected in CSV:\n%s", errorMessage)
}
return finished, nil
}