|
@@ -35,6 +35,12 @@ const (
|
|
|
ErrorAccessDeniedMessage = "access denied"
|
|
|
)
|
|
|
|
|
|
+const (
|
|
|
+ restCtxRequest = "rest-ctx-request"
|
|
|
+ restCtxResponse = "rest-ctx-response"
|
|
|
+ restCtxHttpctx = "rest-ctx-httpctx"
|
|
|
+)
|
|
|
+
|
|
|
const (
|
|
|
OperatorEqual = "eq"
|
|
|
OperatorGreaterThan = "gt"
|
|
@@ -193,7 +199,7 @@ func (r *Restful) prepareConditions(ctx context.Context, requestCtx *http.Contex
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok = model.(ActiveModel); ok {
|
|
|
- if err = activeModel.BeforeQuery(ctx, query); err != nil {
|
|
|
+ if err = activeModel.OnBeforeQuery(ctx, query); err != nil {
|
|
|
return
|
|
|
}
|
|
|
}
|
|
@@ -301,7 +307,7 @@ func (r *Restful) prepareConditions(ctx context.Context, requestCtx *http.Contex
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok = model.(ActiveModel); ok {
|
|
|
- err = activeModel.AfterQuery(ctx, query)
|
|
|
+ err = activeModel.OnAfterQuery(ctx, query)
|
|
|
}
|
|
|
if r.opts.Delegate != nil {
|
|
|
err = r.opts.Delegate.AfterQuery(ctx, query)
|
|
@@ -309,6 +315,17 @@ func (r *Restful) prepareConditions(ctx context.Context, requestCtx *http.Contex
|
|
|
return
|
|
|
}
|
|
|
|
|
|
+func (r *Restful) createContext(httpCtx *http.Context) context.Context {
|
|
|
+ ctx := httpCtx.Request().Context()
|
|
|
+ if ctx == nil {
|
|
|
+ ctx = context.Background()
|
|
|
+ }
|
|
|
+ ctx = context.WithValue(ctx, restCtxRequest, httpCtx.Request())
|
|
|
+ ctx = context.WithValue(ctx, restCtxResponse, httpCtx.Response())
|
|
|
+ ctx = context.WithValue(ctx, restCtxHttpctx, httpCtx)
|
|
|
+ return ctx
|
|
|
+}
|
|
|
+
|
|
|
func (r *Restful) actionIndex(httpCtx *http.Context) (err error) {
|
|
|
var (
|
|
|
page int
|
|
@@ -320,13 +337,8 @@ func (r *Restful) actionIndex(httpCtx *http.Context) (err error) {
|
|
|
if !r.hasScenario(ScenarioList) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
|
}
|
|
|
- ctx := httpCtx.Request().Context()
|
|
|
- if ctx == nil {
|
|
|
- ctx = context.Background()
|
|
|
- }
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
namespace = httpCtx.ParamValue(NamespaceVariable)
|
|
|
- ctx = context.WithValue(ctx, NamespaceField, namespace)
|
|
|
- ctx = context.WithValue(ctx, "request", httpCtx.Request())
|
|
|
page, _ = strconv.Atoi(httpCtx.FormValue("page"))
|
|
|
pageSize, _ = strconv.Atoi(httpCtx.FormValue("pagesize"))
|
|
|
if pageSize <= 0 {
|
|
@@ -374,10 +386,7 @@ func (r *Restful) actionCreate(httpCtx *http.Context) (err error) {
|
|
|
refModel reflect.Value
|
|
|
diffAttrs []*DiffAttr
|
|
|
)
|
|
|
- ctx := httpCtx.Request().Context()
|
|
|
- if ctx == nil {
|
|
|
- ctx = context.Background()
|
|
|
- }
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
diffAttrs = make([]*DiffAttr, 0, 10)
|
|
|
if !r.hasScenario(ScenarioCreate) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
@@ -407,10 +416,10 @@ func (r *Restful) actionCreate(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.BeforeCreate(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnBeforeCreate(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
- if errTx = activeModel.BeforeSave(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnBeforeSave(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
}
|
|
@@ -428,10 +437,10 @@ func (r *Restful) actionCreate(httpCtx *http.Context) (err error) {
|
|
|
})
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.AfterCreate(ctx, model, diffAttrs); errTx != nil {
|
|
|
+ if errTx = activeModel.OnAfterCreate(ctx, model, diffAttrs); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, err.Error())
|
|
|
}
|
|
|
- if errTx = activeModel.AfterSave(ctx, model, diffAttrs); errTx != nil {
|
|
|
+ if errTx = activeModel.OnAfterSave(ctx, model, diffAttrs); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, err.Error())
|
|
|
}
|
|
|
}
|
|
@@ -474,10 +483,7 @@ func (r *Restful) actionUpdate(httpCtx *http.Context) (err error) {
|
|
|
diffs = make(map[string]interface{})
|
|
|
diffAttrs = make([]*DiffAttr, 0)
|
|
|
)
|
|
|
- ctx := httpCtx.Request().Context()
|
|
|
- if ctx == nil {
|
|
|
- ctx = context.Background()
|
|
|
- }
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
if !r.hasScenario(ScenarioUpdate) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
|
}
|
|
@@ -514,10 +520,10 @@ func (r *Restful) actionUpdate(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.BeforeUpdate(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnBeforeUpdate(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
- if errTx = activeModel.BeforeSave(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnBeforeSave(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
}
|
|
@@ -541,10 +547,10 @@ func (r *Restful) actionUpdate(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.AfterUpdate(ctx, model, diffAttrs); errTx != nil {
|
|
|
+ if errTx = activeModel.OnAfterUpdate(ctx, model, diffAttrs); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
- if errTx = activeModel.AfterSave(ctx, model, diffAttrs); errTx != nil {
|
|
|
+ if errTx = activeModel.OnAfterSave(ctx, model, diffAttrs); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
}
|
|
@@ -585,10 +591,7 @@ func (r *Restful) actionDelete(httpCtx *http.Context) (err error) {
|
|
|
if !r.hasScenario(ScenarioDelete) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
|
}
|
|
|
- ctx := httpCtx.Request().Context()
|
|
|
- if ctx == nil {
|
|
|
- ctx = context.Background()
|
|
|
- }
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
idStr := httpCtx.ParamValue("id")
|
|
|
namespace = httpCtx.ParamValue(NamespaceVariable)
|
|
|
model = reflect.New(r.reflectType).Interface()
|
|
@@ -608,7 +611,7 @@ func (r *Restful) actionDelete(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.BeforeDelete(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnBeforeDelete(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
}
|
|
@@ -616,7 +619,7 @@ func (r *Restful) actionDelete(httpCtx *http.Context) (err error) {
|
|
|
return errTx
|
|
|
}
|
|
|
if activeModel, ok := model.(ActiveModel); ok {
|
|
|
- if errTx = activeModel.AfterDelete(ctx, model); errTx != nil {
|
|
|
+ if errTx = activeModel.OnAfterDelete(ctx, model); errTx != nil {
|
|
|
return httpCtx.Error(HttpRequestCallbackFailed, errTx.Error())
|
|
|
}
|
|
|
}
|
|
@@ -645,11 +648,7 @@ func (r *Restful) actionExport(httpCtx *http.Context) (err error) {
|
|
|
if !r.hasScenario(ScenarioExport) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
|
}
|
|
|
- ctx := httpCtx.Request().Context()
|
|
|
- if ctx == nil {
|
|
|
- ctx = context.Background()
|
|
|
- }
|
|
|
- ctx = context.WithValue(ctx, NamespaceVariable, namespace)
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
namespace = httpCtx.ParamValue(NamespaceVariable)
|
|
|
sliceValue := reflect.MakeSlice(reflect.SliceOf(r.reflectType), 0, 0)
|
|
|
models := reflect.New(sliceValue.Type())
|
|
@@ -704,6 +703,7 @@ func (r *Restful) actionView(httpCtx *http.Context) (err error) {
|
|
|
if !r.hasScenario(ScenarioView) {
|
|
|
return httpCtx.Error(HttpAccessDenied, ErrorAccessDeniedMessage)
|
|
|
}
|
|
|
+ ctx := r.createContext(httpCtx)
|
|
|
namespace = httpCtx.ParamValue(NamespaceVariable)
|
|
|
scenario := httpCtx.FormValue("scenario")
|
|
|
idStr := httpCtx.ParamValue("id")
|
|
@@ -721,16 +721,11 @@ func (r *Restful) actionView(httpCtx *http.Context) (err error) {
|
|
|
//获取指定场景下面的字段进行渲染显示
|
|
|
var schemas []*Schema
|
|
|
if scenario == "" {
|
|
|
- schemas = r.opts.LookupFunc(httpCtx.Request().Context(), namespace, r.model.ModuleName(), r.model.TableName(), ScenarioView)
|
|
|
+ schemas = r.opts.LookupFunc(ctx, namespace, r.model.ModuleName(), r.model.TableName(), ScenarioView)
|
|
|
} else {
|
|
|
- schemas = r.opts.LookupFunc(httpCtx.Request().Context(), namespace, r.model.ModuleName(), r.model.TableName(), scenario)
|
|
|
- }
|
|
|
- requestCtx := httpCtx.Request().Context()
|
|
|
- if requestCtx == nil {
|
|
|
- requestCtx = context.Background()
|
|
|
+ schemas = r.opts.LookupFunc(ctx, namespace, r.model.ModuleName(), r.model.TableName(), scenario)
|
|
|
}
|
|
|
- requestCtx = context.WithValue(requestCtx, NamespaceVariable, namespace)
|
|
|
- return httpCtx.Success(r.opts.Formatter.formatModel(requestCtx, model, schemas, r.statement))
|
|
|
+ return httpCtx.Success(r.opts.Formatter.formatModel(ctx, model, schemas, r.statement))
|
|
|
}
|
|
|
return httpCtx.Success(model)
|
|
|
}
|