2018-11-07 10:10:51 +00:00
|
|
|
package controllers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/astaxie/beego"
|
|
|
|
)
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
// JSONBasicResponse The minimal JSON response
|
|
|
|
type JSONBasicResponse struct {
|
2018-11-07 10:10:51 +00:00
|
|
|
Status int
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
// JSONError code for a input error
|
|
|
|
const JSONError int = 400
|
|
|
|
|
|
|
|
// JSONInternalError code for an internal error
|
|
|
|
const JSONInternalError int = 500
|
|
|
|
|
|
|
|
// JSONSuccess code for a success
|
|
|
|
const JSONSuccess int = 200
|
2018-11-07 10:10:51 +00:00
|
|
|
|
|
|
|
// BaseController operations for BaseController
|
|
|
|
type BaseController struct {
|
|
|
|
beego.Controller
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
// ServeJSONError respond with a JSON error
|
|
|
|
func (c *BaseController) ServeJSONError(message string) {
|
|
|
|
json := JSONBasicResponse{JSONError, message}
|
2018-11-07 15:27:39 +00:00
|
|
|
c.Data["json"] = &json
|
|
|
|
///c.Ctx.ResponseWriter.WriteHeader(400)
|
|
|
|
c.ServeJSON()
|
2018-11-07 10:10:51 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
// ServeJSONErrorWithCode respond with a JSON error and specify code
|
|
|
|
func (c *BaseController) ServeJSONErrorWithCode(errorcode int, message string) {
|
|
|
|
json := JSONBasicResponse{errorcode, message}
|
2018-11-07 15:27:39 +00:00
|
|
|
c.Data["json"] = &json
|
|
|
|
c.ServeJSON()
|
|
|
|
}
|
|
|
|
|
2018-11-07 19:13:26 +00:00
|
|
|
// ServeJSONSuccess respond with a JSON success message
|
|
|
|
func (c *BaseController) ServeJSONSuccess(message string) {
|
|
|
|
json := JSONBasicResponse{JSONSuccess, message}
|
2018-11-07 15:27:39 +00:00
|
|
|
c.Data["json"] = &json
|
|
|
|
c.ServeJSON()
|
2018-11-07 10:10:51 +00:00
|
|
|
}
|