/** POST: collect train record data body: json, two types, append and flush { "op" : $op(append, flush) "player": $player "data": $data } result: { "success": $succ } */ func (this *RawTrainRecordController) Post() { type PostData struct { Op string `json:"op"` Player string `json:"player"` Data types.RawTrainRecord `json:"data"` } type Success struct { Succ bool `json:"success"` } var data PostData json.Unmarshal(this.Ctx.Input.RequestBody, &data) switch data.Op { case "append": models.AppendRawTrainData(data.Player, &data.Data) this.Data["json"] = &Success{true} case "flush": err := models.FlushRawTrainData() if err != nil { beego.Warn("Flush train data error, database error: ", err) this.Data["json"] = errors.Issue("Flush raw train data failed", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINRECORD+errors.E_DETAIL_INTERNAL_DB_ERROR, this.Ctx.Request.URL.String()) } else { this.Data["json"] = &Success{true} } default: this.Data["json"] = errors.Issue("parameter op needed", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINRECORD+errors.E_DETAIL_ILLEGAL_PARAM, this.Ctx.Request.URL.String()) } this.ServeJson() }
/** GET: request the history train data(processed remark) params: player=$player&page=$page&num=$num result: { "result": [{}, {}, ...], "total_number": 2, "before": 0, "current": 1, "next": 0 } */ func (this *TrainHistoryController) Get() { page, err := this.GetInt("page", 0) if err != nil { beego.Warn("Query train history error, parse page error: ", err) this.Data["json"] = errors.Issue("Parse page error", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINHISTORY+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } num, err := this.GetInt("num", 10) if err != nil { beego.Warn("Query train history error, parse num error: ", err) this.Data["json"] = errors.Issue("Parse num error", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINHISTORY+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } if page < 0 || num < 0 { beego.Warn("Query train history error, wrong params: page, num = ", page, num) this.Data["json"] = errors.Issue("Page and num must be non-negative", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINHISTORY+errors.E_DETAIL_ILLEGAL_PARAM, this.Ctx.Request.URL.String()) this.ServeJson() return } player := this.GetString("player") if len(player) != 40 { beego.Warn("Query train history error, wrong params: player = ", player) this.Data["json"] = errors.Issue("Invalid player", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINHISTORY+errors.E_DETAIL_ILLEGAL_PARAM, this.Ctx.Request.URL.String()) this.ServeJson() return } this.Data["json"] = models.QueryTrainRecord(player, page, num) beego.Info(fmt.Sprintf("Query train history, req: %s, player: %s, page: %d, num: %d", this.Ctx.Request.URL.String(), player, page, num)) this.ServeJson() }
/** GET: for redirect, return error message { "ErrorMsg": "You need login first", "ErrorCode": "20007", "Request": "/api/player?page=0\u0026num=10" } */ func (this *AuthController) Get() { req, err := base64.URLEncoding.DecodeString(this.GetString("req")) var reqStr string if err != nil { reqStr = "" } else { reqStr = string(req) } this.Data["json"] = errors.Issue("You need login first", errors.E_TYPE_SERVICE+errors.E_MODULE_ANY+errors.E_DETAIL_NEED_LOGIN, reqStr) this.ServeJson() }
/** GET: request the processed remark params: player=$player&page=$page&num=$num result: { "result": [...], "total_number": 2, "before": 0, "current": 1, "next": 0 } */ func (this *TrainRemarkController) Get() { player := this.GetString("player") if len(player) != 40 { beego.Warn("Query train remark error, wrong params: player = ", player) this.Data["json"] = errors.Issue("Invalid player", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINREMARK+errors.E_DETAIL_ILLEGAL_PARAM, this.Ctx.Request.URL.String()) this.ServeJson() return } page, err := this.GetInt("page", 0) if err != nil { beego.Warn("Get players error, parse page error: ", err) this.Data["json"] = errors.Issue("Parse page error", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINREMARK+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } num, err := this.GetInt("num", 10) if err != nil { beego.Warn("Get players error, parse num error: ", err) this.Data["json"] = errors.Issue("Parse num error", errors.E_TYPE_SERVICE+errors.E_MODULE_TRAINREMARK+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } this.Data["json"] = models.GetTrainRecord(player, page, num) beego.Info(fmt.Sprintf("Query train remark, req: %s, player: %s, page: %d, num: %d", this.Ctx.Request.URL.String(), player, page, num)) this.ServeJson() }
/** GET: query players params: page, num result: json error happened: { "error": "Error message", "error_code": $code, "request": "/api/player?page=$page&num=$num" } success: { "result": [{}, {}, ...], "total_number": 2, "before": 0, "current": 1, "next": 0 } */ func (this *PlayersController) Get() { page, err := this.GetInt("page", 0) if err != nil { beego.Warn("Get players error, parse page error: ", err) this.Data["json"] = errors.Issue("Parse page error", errors.E_TYPE_SERVICE+errors.E_MODULE_PLAYER+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } num, err := this.GetInt("num", 10) if err != nil { beego.Warn("Get players error, parse num error: ", err) this.Data["json"] = errors.Issue("Parse num error", errors.E_TYPE_SERVICE+errors.E_MODULE_PLAYER+errors.E_DETAIL_PARSE_PARAM_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } if page < 0 || num < 0 { beego.Warn("Get players error, wrong params: page, num = ", page, num) this.Data["json"] = errors.Issue("Page and num must be non-negative", errors.E_TYPE_SERVICE+errors.E_MODULE_PLAYER+errors.E_DETAIL_ILLEGAL_PARAM, this.Ctx.Request.URL.String()) this.ServeJson() return } beego.Info(fmt.Sprintf("Query player, req: %s, page: %d, num: %d", this.Ctx.Request.URL.String(), page, num)) this.Data["json"] = models.QueryPlayer(page, num) this.ServeJson() }
/** POST: insert or update player(ObjId not empty means update) request body: player info(json) result: json success: { "success": true, "id": $ObjId } */ func (this *PlayersController) Post() { var player types.Player json.Unmarshal(this.Ctx.Input.RequestBody, &player) /*if !player.Valid() { beego.Warn("Add/Update players error, invalid data: ", player, string(this.Ctx.Input.RequestBody)) this.Data["json"] = errors.Issue("Player info incomplete", errors.E_TYPE_SERVICE + errors.E_MODULE_PLAYER + errors.E_DETAIL_INCOMPLETE_DATA, this.Ctx.Request.URL.String()) this.ServeJson() return }*/ beego.Info("data & player ", string(this.Ctx.Input.RequestBody), player) var err error if player.ObjId != "" { err = models.UpdatePlayer(&player) } else { err = models.InsertPlayer(&player) } if err != nil { beego.Warn("Add/Update players error, database error: ", err) this.Data["json"] = errors.Issue("Database operate error", errors.E_TYPE_SERVICE+errors.E_MODULE_PLAYER+errors.E_DETAIL_INTERNAL_DB_ERROR, this.Ctx.Request.URL.String()) this.ServeJson() return } beego.Info("Add/Update players success, player id: ", player.ObjId.Hex()) type Success struct { Succ bool `json:"success"` types.Player `json:"player"` } this.Data["json"] = &Success{true, player} this.ServeJson() }