Merge pull request 'add field type and reserved' (#41) from layout_api1 into master
This commit is contained in:
		
						commit
						24ca0c91a5
					
				|  | @ -16,4 +16,11 @@ type Layout struct { | ||||||
| 	} `json:"creator"` | 	} `json:"creator"` | ||||||
| 	ViewID string `json:"view_id" elastic_mapping:"view_id: { type: keyword }"` | 	ViewID string `json:"view_id" elastic_mapping:"view_id: { type: keyword }"` | ||||||
| 	Config interface{} `json:"config" elastic_mapping:"config: { type: object, enabled:false }"` | 	Config interface{} `json:"config" elastic_mapping:"config: { type: object, enabled:false }"` | ||||||
| } | 	Reserved bool  `json:"reserved,omitempty" elastic_mapping:"reserved:{type:boolean}"` | ||||||
|  | 	Type LayoutType `json:"type" elastic_mapping:"type: { type: keyword }"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type LayoutType string | ||||||
|  | const ( | ||||||
|  | 	LayoutTypeWorkspace LayoutType = "workspace" | ||||||
|  | ) | ||||||
|  | @ -5,7 +5,6 @@ | ||||||
| package layout | package layout | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"fmt" |  | ||||||
| 	log "github.com/cihub/seelog" | 	log "github.com/cihub/seelog" | ||||||
| 	"infini.sh/console/model" | 	"infini.sh/console/model" | ||||||
| 	"infini.sh/framework/core/api/rbac" | 	"infini.sh/framework/core/api/rbac" | ||||||
|  | @ -127,6 +126,10 @@ func (h *LayoutAPI) deleteLayout(w http.ResponseWriter, req *http.Request, ps ht | ||||||
| 		}, http.StatusNotFound) | 		}, http.StatusNotFound) | ||||||
| 		return | 		return | ||||||
| 	} | 	} | ||||||
|  | 	if obj.Reserved { | ||||||
|  | 		h.WriteError(w, "this layout is reserved", http.StatusInternalServerError) | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	ctx := &orm.Context{ | 	ctx := &orm.Context{ | ||||||
| 		Refresh: "wait_for", | 		Refresh: "wait_for", | ||||||
|  | @ -145,20 +148,37 @@ func (h *LayoutAPI) searchLayout(w http.ResponseWriter, req *http.Request, ps ht | ||||||
| 
 | 
 | ||||||
| 	var ( | 	var ( | ||||||
| 		keyword        = h.GetParameterOrDefault(req, "keyword", "") | 		keyword        = h.GetParameterOrDefault(req, "keyword", "") | ||||||
| 		queryDSL    = `{"query":{"bool":{"must":[%s]}}, "size": %d, "from": %d}` |  | ||||||
| 		strSize     = h.GetParameterOrDefault(req, "size", "20") | 		strSize     = h.GetParameterOrDefault(req, "size", "20") | ||||||
| 		strFrom     = h.GetParameterOrDefault(req, "from", "0") | 		strFrom     = h.GetParameterOrDefault(req, "from", "0") | ||||||
| 		viewID     = h.GetParameterOrDefault(req, "view_id", "") | 		viewID     = strings.TrimSpace(h.GetParameterOrDefault(req, "view_id", "")) | ||||||
| 		mustBuilder = &strings.Builder{} | 		typ     = strings.TrimSpace(h.GetParameterOrDefault(req, "type", "")) | ||||||
|  | 		mustQ []util.MapStr | ||||||
| 	) | 	) | ||||||
| 	if viewID != "" { | 	if viewID != "" { | ||||||
| 		mustBuilder.WriteString(fmt.Sprintf(`{"term":{"view_id":{"value":"%s"}}}`, viewID)) | 		mustQ = append(mustQ, util.MapStr{ | ||||||
|  | 			"term": util.MapStr{ | ||||||
|  | 				"view_id": util.MapStr{ | ||||||
|  | 					"value": viewID, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}) | ||||||
|  | 	} | ||||||
|  | 	if typ != "" { | ||||||
|  | 		mustQ = append(mustQ, util.MapStr{ | ||||||
|  | 			"term": util.MapStr{ | ||||||
|  | 				"type": util.MapStr{ | ||||||
|  | 					"value": typ, | ||||||
|  | 				}, | ||||||
|  | 			}, | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| 	if keyword != "" { | 	if keyword != "" { | ||||||
| 		if mustBuilder.Len() > 0 { | 		mustQ = append(mustQ, util.MapStr{ | ||||||
| 			mustBuilder.WriteString(",") | 			"query_string": util.MapStr{ | ||||||
| 		} | 				"default_field":"*", | ||||||
| 		mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) | 				"query": keyword, | ||||||
|  | 			}, | ||||||
|  | 		}) | ||||||
| 	} | 	} | ||||||
| 	size, _ := strconv.Atoi(strSize) | 	size, _ := strconv.Atoi(strSize) | ||||||
| 	if size <= 0 { | 	if size <= 0 { | ||||||
|  | @ -168,10 +188,21 @@ func (h *LayoutAPI) searchLayout(w http.ResponseWriter, req *http.Request, ps ht | ||||||
| 	if from < 0 { | 	if from < 0 { | ||||||
| 		from = 0 | 		from = 0 | ||||||
| 	} | 	} | ||||||
|  | 	query := util.MapStr{ | ||||||
|  | 		"size": size, | ||||||
|  | 		"from": from, | ||||||
|  | 	} | ||||||
|  | 	if len(mustQ) > 0 { | ||||||
|  | 		query["query"] = util.MapStr{ | ||||||
|  | 			"bool": util.MapStr{ | ||||||
|  | 				"must": mustQ, | ||||||
|  | 			}, | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
| 
 | 
 | ||||||
| 	q := orm.Query{} | 	q := orm.Query{ | ||||||
| 	queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), size, from) | 		RawQuery: util.MustToJSONBytes(query), | ||||||
| 	q.RawQuery = []byte(queryDSL) | 	} | ||||||
| 
 | 
 | ||||||
| 	err, res := orm.Search(&model.Layout{}, &q) | 	err, res := orm.Search(&model.Layout{}, &q) | ||||||
| 	if err != nil { | 	if err != nil { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue