mirror of
https://github.com/hay-kot/homebox.git
synced 2025-08-01 15:20:29 +00:00
Simple validation for negative values of integer fields
This commit is contained in:
parent
704b5d27d8
commit
0d4dc2cc12
1 changed files with 32 additions and 0 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue