Exemplo n.º 1
0
func (c *Category) FindCategoryOrCreate(params map[string]interface{}) *gorm.DB {
	sess := db.Connection().First(c, params)
	if c.Id == 0 {
		if title, ok := params["title"]; ok {
			c.Title = title.(string)
			c.Slug = title.(string)
			c.UserId = db.USERID
		}
		if ctype, ok := params["type"]; ok {
			c.Type = ctype.(string)
		}
		sess = c.Save()
	}
	return sess
}
Exemplo n.º 2
0
func (c *ListItemCommand) Run() error {

	// find category by title
	category := &models.Category{}
	if c.CategoryTitle != "" {
		err := category.FindByParams(map[string]interface{}{"title": c.CategoryTitle}).Error
		if err == nil {
			return errors.New("Undefine Category")
		}
	}

	// find all items
	items := []models.Item{}
	params := map[string]interface{}{"type": c.ItemType, "user_id": db.USERID}
	if category.Id != 0 {
		params["category_id"] = category.Id
	}
	db.Connection().Find(&items, params)

	// prepare table view
	data := [][]string{}
	for _, item := range items {
		data = append(data, []string{
			strconv.Itoa(item.Id), strconv.FormatFloat(item.Amount, 'f', 2, 64),
			item.GetCategoryTitle(), item.Comment,
		})
	}

	table := tablewriter.NewWriter(os.Stdout)
	table.SetHeader([]string{"Id", "Amount", "Category", "Comments"})
	table.SetAlignment(tablewriter.ALIGN_LEFT)
	for _, v := range data {
		table.Append(v)
	}

	// render table views
	color.Println("@gSpent items:")
	table.Render()
	return nil
}
Exemplo n.º 3
0
func SumAmountByType(userId int, itemType string) (float64, error) {
	var amount float64
	row := db.Connection().Table("item").Select("SUM(amount) as sumamount").Where("user_id=? AND type=?", userId, itemType).Row()
	row.Scan(&amount)
	return amount, nil
}
Exemplo n.º 4
0
func (i *Item) FindByParams(params map[string]interface{}) *gorm.DB {
	sess := db.Connection().First(i, params)
	return sess
}
Exemplo n.º 5
0
func (i *Item) Delete() *gorm.DB {
	return db.Connection().Delete(i)
}
Exemplo n.º 6
0
func (i *Item) Find(id int) *gorm.DB {
	return db.Connection().First(i, id)
}
Exemplo n.º 7
0
func (i *Item) Save() *gorm.DB {
	return db.Connection().Save(i)
}
Exemplo n.º 8
0
func (u *User) Find(id int) {
	db.Connection().First(u, id)
}
Exemplo n.º 9
0
func (u *User) Save() {
	db.Connection().Save(u)
}
Exemplo n.º 10
0
func (c *Category) FindByParams(params map[string]interface{}) *gorm.DB {
	sess := db.Connection().First(c, params)
	return sess
}
Exemplo n.º 11
0
func (c *Category) Delete() *gorm.DB {
	return db.Connection().Delete(c)
}
Exemplo n.º 12
0
func (c *Category) Find(id int) *gorm.DB {
	return db.Connection().First(c, id)
}
Exemplo n.º 13
0
func (c *Category) Save() *gorm.DB {
	return db.Connection().Save(c)
}