Fixed error when inserting an empty item

This commit is contained in:
konrad 2018-01-16 14:11:46 +01:00 committed by kolaente
parent 547b44d608
commit 97ebfc0207
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
1 changed files with 9 additions and 5 deletions

View File

@ -1,17 +1,21 @@
package models
import "fmt"
// AddOrUpdateItem adds or updates a item from a item struct
func AddOrUpdateItem(item Item) (newItem Item, err error) {
// save the quantity for later use
qty := item.Quantity
if item.ID == 0 {
if item.Title != "" { // Only insert it if the title is not empty
_, err = x.Insert(&item)
if item.Title == "" { // Only insert it if the title is not empty
return Item{}, fmt.Errorf("You need at least a title to create an item")
}
if err != nil {
return Item{}, err
}
_, err = x.Insert(&item)
if err != nil {
return Item{}, err
}
} else {
_, err = x.ID(item.ID).Update(&item)