func GetDailyReports(date string) (reports []*models.DailyReport, e error) { if _, e = time.Parse("2006-01-02", date); e != nil { return } var rows *sql.Rows rows, e = _db.Query(`select date, user_id, work, plan, remark, modified_at from daily_reports where date=? order by date`, date) if e != nil { log.Println(e) return } defer rows.Close() var modifiedAt float64 for rows.Next() { r := new(models.DailyReport) e = rows.Scan(&r.Date, &r.UserId, &r.Work, &r.Plan, &r.Remark, &modifiedAt) if e != nil { log.Println(e) return } r.ModifiedAt = int64(modifiedAt) reports = append(reports, r) } return }
func AddDailyReport(d *models.DailyReport) error { if _, e := time.Parse("2006-01-02", d.Date); e != nil { log.Println("AddDailyReport", e) return e } d.ModifiedAt = time.Now().Unix() _, e := _db.Exec(`insert or replace into daily_reports(date, user_id, work, plan, remark, modified_at) values(?,?,?,?,?,?)`, d.Date, d.UserId, d.Work, d.Plan, d.Remark, d.ModifiedAt) if e != nil { log.Println(e) return e } return nil }
func TestAddDailyReport(t *testing.T) { d := new(models.DailyReport) d.Date = "2015-06-30" d.Work = "work" d.Plan = "my plan" d.Remark = "hehe" d.UserId = userId if AddDailyReport(d) == nil { t.Log("add daily report") } else { t.Error("") } if AddDailyReport(d) != nil { t.Log("test duplicated") } else { t.Error("should not allow duplicated report") } }