func ResponseError(c wine.Context, err ErrType) { msg := "" switch err { case ErrParm: msg = "parameter error" case ErrServer: msg = "server error" default: break } c.JSON(gox.M{"error": err, "msg": msg}) }
func GetDailyReports(c wine.Context) { date := c.Params().GetStr("date") users, e := dao.GetAllUsers() if e != nil { ResponseError(c, ErrServer) return } reports := make([]*models.DailyReport, len(users)) for i, u := range users { r, _ := dao.GetUserDailyReport(u.Id, date) if r == nil { r = new(models.DailyReport) r.ModifiedAt = time.Now().Unix() } r.User = u r.UserId = u.Id reports[i] = r } ResponseData(c, gox.M{"reports": reports}) }
func GetUserDailyReports(c wine.Context) { start := c.Params().GetStr("start") end := c.Params().GetStr("end") var st time.Time var et time.Time var e error if st, e = time.Parse("2006-01-02", start); e != nil { ResponseError(c, ErrParm) return } if et, e = time.Parse("2006-01-02", end); e != nil { ResponseError(c, ErrParm) return } userId := c.Params().GetInt64("user_id") user, _ := dao.GetUser(userId) if user == nil { ResponseError(c, ErrParm) return } var reports []*models.DailyReport stt := st.Unix() ett := et.Unix() for t := stt; t <= ett; t += 24 * 3600 { d := time.Unix(t, 0).Format("2006-01-02") r, _ := dao.GetUserDailyReport(userId, d) if r == nil { r = new(models.DailyReport) r.ModifiedAt = time.Now().Unix() } r.User = user r.UserId = userId r.Date = d reports = append(reports, r) } ResponseData(c, gox.M{"reports": reports, "user": user}) }
func HandleIndex(c wine.Context) { date := c.Params().GetStr("date") _, e := time.Parse("2006-01-02", date) if e != nil { date = time.Now().In(time.FixedZone("", 8*3600)).Format("2006-01-02") } t, _ := time.Parse("2006-01-02", date) previousDate := t.Add(-24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02") nextDate := t.Add(24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02") params := gox.M{} params["previous_date"] = previousDate params["next_date"] = nextDate params["date"] = date params["user_start_date"] = t.Add(-10 * 24 * 3600 * time.Second).In(time.FixedZone("", 8*3600)).Format("2006-01-02") params["user_end_date"] = date params["title"] = date + "(" + DaysOfWeek[t.Weekday()] + ")" //log.Println(params) resp, _ := http.Get("http://localhost:8001/daily-reports/" + date) data, _ := ioutil.ReadAll(resp.Body) defer resp.Body.Close() var respMap map[string]interface{} if err := json.Unmarshal(data, &respMap); err == nil { if data, ok := respMap["data"].(map[string]interface{}); ok { if reports, ok := data["reports"].([]interface{}); ok { params["reports"] = reports c.TemplateHTML("index.tmpl", params) return } } } log.Println(respMap) c.TemplateHTML("error.tmpl", nil) return }
func HandleUserDailyReports(c wine.Context) { start := c.Params().GetStr("start") end := c.Params().GetStr("end") var st time.Time var et time.Time var e error if st, e = time.Parse("2006-01-02", start); e != nil { c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"}) return } if et, e = time.Parse("2006-01-02", end); e != nil { c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"}) return } userId, _ := strconv.ParseInt(c.Params().GetStr("user_id"), 10, 64) if userId <= 0 { c.TemplateHTML("error.tmpl", gox.M{"msg": "invalid date"}) return } resp, e := http.Get("http://localhost:8001/user/" + c.Params().GetStr("user_id") + "/daily-reports/" + start + "/" + end) if e != nil { c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"}) return } data, _ := ioutil.ReadAll(resp.Body) defer resp.Body.Close() params := gox.M{} previousStart := st.AddDate(0, 0, -10).Format("2006-01-02") nextEnd := et.AddDate(0, 0, 10).Format("2006-01-02") params["previous_start"] = previousStart params["next_end"] = nextEnd params["start"] = start params["end"] = end var respMap map[string]interface{} if err := json.Unmarshal(data, &respMap); err == nil { if data, ok := respMap["data"].(map[string]interface{}); ok { if reports, ok := data["reports"].([]interface{}); ok { for _, r := range reports { m := r.(map[string]interface{}) t, _ := time.Parse("2006-01-02", m["date"].(string)) m["date"] = m["date"].(string) + "(" + DaysOfWeek[t.Weekday()] + ")" } if user, ok := data["user"].(map[string]interface{}); ok { params["reports"] = reports params["user"] = user c.TemplateHTML("user_daily_reports.tmpl", params) return } } } } log.Println(respMap) c.TemplateHTML("error.tmpl", gox.M{"msg": "server error"}) }
func HandlePostDailyReport(c wine.Context) { var req PostDailyReportRequest req.UserId = c.Params().GetInt64("user_id") req.Date = c.Params().GetStr("date") req.Work = c.Params().GetStr("work") req.Plan = c.Params().GetStr("plan") req.Remark = c.Params().GetStr("remark") if req.UserId < 0 || len(req.Date) == 0 { c.JSON(gox.M{"error": 1}) return } values := url.Values{} values.Set("user_id", strconv.FormatInt(req.UserId, 10)) values.Set("date", req.Date) values.Set("work", req.Work) values.Set("plan", req.Plan) values.Set("remark", req.Remark) resp, e := http.DefaultClient.PostForm("http://localhost:8001/daily-report", values) if e != nil { c.JSON(gox.M{"error": 2, "msg": "server error"}) return } data, _ := ioutil.ReadAll(resp.Body) defer resp.Body.Close() var respMap map[string]interface{} if err := json.Unmarshal(data, &respMap); err == nil { if err, ok := respMap["error"].(float64); ok && int(err) == 0 { c.JSON(gox.M{"error": 0}) } else { log.Println(respMap) c.JSON(gox.M{"error": 1, "msg": "failed to add report"}) } } else { log.Println(respMap) c.JSON(gox.M{"error": 2, "msg": "server error"}) } }
func ResponseData(c wine.Context, data interface{}) { c.JSON(gox.M{"error": ErrNone, "msg": "", "data": data}) }