oc-catalog/controllers/resource.go
2024-08-01 09:27:38 +02:00

91 lines
3.4 KiB
Go

package controllers
import (
oclib "cloud.o-forge.io/core/oc-lib"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about resource
type ResourceController struct {
beego.Controller
}
// @Title GetAll
// @Description find resource by id
// @Success 200 {resource} models.resource
// @router / [get]
func (o *ResourceController) GetAll() {
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.STORAGE_RESOURCE), oclib.LibDataEnum(oclib.PROCESSING_RESOURCE), oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
d := oclib.LoadAll(resource)
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title Get
// @Description find resource by key word
// @Param search path string true "the search you want to get"
// @Success 200 {resource} models.resource
// @router /search/:search [get]
func (o *ResourceController) Search() {
search := o.Ctx.Input.Param(":search")
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.STORAGE_RESOURCE), oclib.LibDataEnum(oclib.PROCESSING_RESOURCE), oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
d := oclib.Search(nil, search, resource)
if d.Code != 200 || len(d.Data) == 0 {
results[resource.String()] = []interface{}{}
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title Get
// @Description find resource by id
// @Param id path string true "the id you want to get"
// @Success 200 {resource} models.resource
// @router /:id [get]
func (o *ResourceController) Get() {
id := o.Ctx.Input.Param(":id")
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{oclib.LibDataEnum(oclib.DATACENTER_RESOURCE),
oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.STORAGE_RESOURCE), oclib.LibDataEnum(oclib.PROCESSING_RESOURCE), oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
d := oclib.LoadOne(resource, id)
if d.Code != 200 {
results[resource.String()] = nil
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}
// @Title Delete
// @Description delete the resource
// @Param id path string true "The id you want to delete"DeleteOne
// @Success 200 {resource} delete success!
// @router /:id [delete]
func (o *ResourceController) Delete() {
id := o.Ctx.Input.Param(":id")
results := map[string]interface{}{}
for _, resource := range []oclib.LibDataEnum{oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.DATACENTER_RESOURCE), oclib.LibDataEnum(oclib.STORAGE_RESOURCE), oclib.LibDataEnum(oclib.PROCESSING_RESOURCE), oclib.LibDataEnum(oclib.WORKFLOW_RESOURCE)} {
d := oclib.DeleteOne(resource, id)
if d.Code != 200 {
results[resource.String()] = nil
} else {
results[resource.String()] = d.Data
}
}
o.Data["json"] = map[string]interface{}{"data": results, "code": 200, "error": ""}
o.ServeJSON()
}