// // Item.Build() // func (item *Item) Build(values url.Values) { if len(values["item[date]"]) != 0 { if value := values.Get("item[date]"); value == "" { item.Date = time.Time{} } else { item.Date = date.New(value).Time() } } if len(values["item[category_id]"]) != 0 { if id, err := strconv.Atoi(values.Get("item[category_id]")); err == nil { item.CategoryID = id } else { item.CategoryID = 0 } } if len(values["item[sum]"]) != 0 { if sum, err := strconv.ParseFloat(values.Get("item[sum]"), 64); err == nil { item.Sum = sum } else { item.Sum = 0.0 } } if len(values["item[description]"]) != 0 { item.Description = values.Get("item[description]") } }
func TestMarshalJSON(t *testing.T) { assert := assert.New(t) category := Category{ ID: 36, Name: "Food", } item := Item{ ID: 57, Date: date.New("1982-05-17").Time(), Sum: 10.52, Description: "Red Dry Wine", Category: category, } m, _ := json.Marshal(item) var d ItemDecorator json.Unmarshal(m, &d) assert.Equal(57, d.ID) assert.Equal("1982-05-17", d.Date) assert.Equal(10.52, d.Sum) assert.Equal("Red Dry Wine", d.Description) assert.Equal(36, d.Category.ID) assert.Equal("Food", d.Category.Name) }
// // ItemCollection.Search // func (i *ItemCollection) Search(values url.Values) { d := date.New(values.Get("year"), values.Get("month")) DB. Where("date BETWEEN ? AND ?", d.BeginningOfMonth().String(), d.EndOfMonth().String()). Order("date"). Preload("Category"). Find(&i) }
// // ConsolidateCollection.Search // func (c *ConsolidateCollection) Search(values url.Values) { d := date.New(values.Get("year"), values.Get("month")) DB. Table("items"). Where("date BETWEEN ? AND ?", d.BeginningOfMonth().String(), d.EndOfMonth().String()). Select("SUM(sum) AS sum, category_id"). Group("category_id"). Scan(c) }