func (c *DeleteItemCommand) Run() error { item := models.Item{} item.Find(item.Id) if item.Id == 0 { return errors.New("Inavlid id") } err := item.Delete().Error if err != nil { return err } color.Print("@gItem deleted\n") return nil }
// Main command method func (c *EditItemCommand) Run() error { var item models.Item item.Find(c.Id) if item.Id == 0 { return errors.New("Invalid Item Id") } if c.Amount != 0 { item.Amount = c.Amount } if c.ItemType != "" { item.Type = c.ItemType } if c.Comment != "" { item.Comment = c.Comment } if c.CategoryTitle != "" { category := &models.Category{} err := category.FindCategoryOrCreate(map[string]interface{}{ "title": c.CategoryTitle, "type": item.Type, }).Error if err != nil { return err } item.CategoryId = category.Id } err := item.Save().Error if err != nil { return err } color.Print("@gItem edited\n") return nil }