|
@@ -7,9 +7,25 @@ import (
|
|
|
"gorm.io/gorm"
|
|
|
)
|
|
|
|
|
|
-type Api struct {
|
|
|
- crud *CRUD
|
|
|
-}
|
|
|
+type (
|
|
|
+ Api struct {
|
|
|
+ crud *CRUD
|
|
|
+ }
|
|
|
+
|
|
|
+ feedRequest struct {
|
|
|
+ ModuleName string `json:"module_name"`
|
|
|
+ TableName string `json:"table_name"`
|
|
|
+ ValueField string `json:"value_field"`
|
|
|
+ LabelField string `json:"label_field"`
|
|
|
+ Condition string `json:"condition,omitempty"`
|
|
|
+ Limit int `json:"limit,omitempty"`
|
|
|
+ }
|
|
|
+
|
|
|
+ feedResponse struct {
|
|
|
+ Label interface{} `json:"label"`
|
|
|
+ Value interface{} `json:"value"`
|
|
|
+ }
|
|
|
+)
|
|
|
|
|
|
func (api *Api) handleListSchema(httpCtx *http.Context) (err error) {
|
|
|
var (
|
|
@@ -26,6 +42,7 @@ func (api *Api) handleListSchema(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//handleSaveSchema 保存schema
|
|
|
func (api *Api) handleSaveSchema(httpCtx *http.Context) (err error) {
|
|
|
var (
|
|
|
rest *Restful
|
|
@@ -72,6 +89,7 @@ func (api *Api) handleSaveSchema(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//handleListSchemas 查看表schema
|
|
|
func (api *Api) handleListSchemas(httpCtx *http.Context) (err error) {
|
|
|
var (
|
|
|
moduleLabel string
|
|
@@ -98,6 +116,7 @@ func (api *Api) handleListSchemas(httpCtx *http.Context) (err error) {
|
|
|
return httpCtx.Success(ts)
|
|
|
}
|
|
|
|
|
|
+//handleDeleteSchema 删除指定的表结构
|
|
|
func (api *Api) handleDeleteSchema(httpCtx *http.Context) (err error) {
|
|
|
id := httpCtx.ParamValue("id")
|
|
|
model := &Schema{}
|
|
@@ -118,11 +137,33 @@ func (api *Api) handleDeleteSchema(httpCtx *http.Context) (err error) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+//handleFeed 订阅指定模块数据
|
|
|
+func (api *Api) handleFeed(httpCtx *http.Context) (err error) {
|
|
|
+ req := &feedRequest{}
|
|
|
+ if err = httpCtx.Bind(req); err != nil {
|
|
|
+ return httpCtx.Error(HttpInvalidPayload, err.Error())
|
|
|
+ }
|
|
|
+ result := make([]feedResponse, 0, 10)
|
|
|
+ query := api.crud.db.Table(req.TableName).Select(req.LabelField+" AS label", req.ValueField+" AS value")
|
|
|
+ if req.Condition != "" {
|
|
|
+ query = query.Where(req.Condition)
|
|
|
+ }
|
|
|
+ if req.Limit > 0 {
|
|
|
+ query = query.Limit(req.Limit)
|
|
|
+ }
|
|
|
+ if err = query.Find(&result).Error; err == nil {
|
|
|
+ return httpCtx.Success(result)
|
|
|
+ } else {
|
|
|
+ return httpCtx.Error(HttpDatabaseFindFailed, err.Error())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
func (api *Api) Router(svr *http.Server, ms ...http.Middleware) {
|
|
|
svr.Handle("GET", "/rest/schemas", api.handleListSchemas, ms...)
|
|
|
svr.Handle("GET", "/rest/schema/:module/:table", api.handleListSchema, ms...)
|
|
|
svr.Handle("PUT", "/rest/schema/:module/:table", api.handleSaveSchema, ms...)
|
|
|
svr.Handle("DELETE", "/rest/schema/:id", api.handleDeleteSchema, ms...)
|
|
|
+ svr.Handle("POST", "/rest/feed", api.handleFeed, ms...)
|
|
|
|
|
|
for _, rest := range api.crud.modules {
|
|
|
if rest.hasScenario(ScenarioList) {
|