|
@@ -5,6 +5,7 @@ import (
|
|
|
"git.nspix.com/golang/micro/gateway/http"
|
|
|
"git.nspix.com/golang/rest/v3/cache"
|
|
|
"gorm.io/gorm"
|
|
|
+ "time"
|
|
|
)
|
|
|
|
|
|
type (
|
|
@@ -23,13 +24,16 @@ type (
|
|
|
Condition string `json:"condition,omitempty"`
|
|
|
Limit int `json:"limit,omitempty"`
|
|
|
}
|
|
|
-
|
|
|
feedResponse struct {
|
|
|
- Label interface{} `json:"label"`
|
|
|
- Value interface{} `json:"value"`
|
|
|
+ Label interface{} `json:"label" yaml:"label"`
|
|
|
+ Value interface{} `json:"value" yaml:"value"`
|
|
|
}
|
|
|
)
|
|
|
|
|
|
+func (f *feedRequest) cacheID() string {
|
|
|
+ return "feed:" + f.ModuleName + f.TableName + f.ValueField + f.LabelField
|
|
|
+}
|
|
|
+
|
|
|
func (api *Api) handleListSchema(httpCtx *http.Context) (err error) {
|
|
|
var (
|
|
|
schemas []*Schema
|
|
@@ -149,16 +153,37 @@ func (api *Api) handleFeed(httpCtx *http.Context) (err error) {
|
|
|
if !api.isExists(req.TableName + "@" + req.ModuleName) {
|
|
|
return httpCtx.Error(HTTPUnknownFailed, fmt.Sprintf("model %s not register", req.TableName))
|
|
|
}
|
|
|
- result := make([]feedResponse, 0, 10)
|
|
|
- query := api.crud.db.Table(req.TableName).Select(req.LabelField+" AS label", req.ValueField+" AS value")
|
|
|
+ if api.crud.enableCache {
|
|
|
+ if v, ok := cache.Get(req.cacheID()); ok {
|
|
|
+ return httpCtx.Success(v)
|
|
|
+ }
|
|
|
+ }
|
|
|
+ result := make([]map[string]interface{}, 0, 10)
|
|
|
+ query := api.crud.db.Table(req.TableName).Select(req.LabelField, req.ValueField)
|
|
|
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)
|
|
|
+ if err = query.Scan(&result).Error; err == nil {
|
|
|
+ feeds := make([]feedResponse, 0, len(result))
|
|
|
+ for _, pairs := range result {
|
|
|
+ feed := feedResponse{}
|
|
|
+ for k, v := range pairs {
|
|
|
+ if k == req.LabelField {
|
|
|
+ feed.Label = v
|
|
|
+ }
|
|
|
+ if k == req.ValueField {
|
|
|
+ feed.Value = v
|
|
|
+ }
|
|
|
+ }
|
|
|
+ feeds = append(feeds, feed)
|
|
|
+ }
|
|
|
+ if api.crud.enableCache {
|
|
|
+ cache.SetEx(req.cacheID(), feeds, time.Second*10)
|
|
|
+ }
|
|
|
+ return httpCtx.Success(feeds)
|
|
|
} else {
|
|
|
return httpCtx.Error(HttpDatabaseFindFailed, err.Error())
|
|
|
}
|