78 lines
1.8 KiB
Go
78 lines
1.8 KiB
Go
package controllers
|
|
|
|
import (
|
|
"cloud.o-forge.io/core/oc-catalog/models"
|
|
|
|
beego "github.com/beego/beego/v2/server/web"
|
|
)
|
|
|
|
// StorageController operations about storage
|
|
type StorageController struct {
|
|
beego.Controller
|
|
}
|
|
|
|
// @Title Get
|
|
// @Description find storage by ID
|
|
// @Param ID path string true "the ID you want to get"
|
|
// @Success 200 {object} models.StorageModel
|
|
// @Failure 403 ID is empty
|
|
// @router /:ID [get]
|
|
func (o *StorageController) GetOneStorage(ID string) {
|
|
if ID != "" {
|
|
ob, err := models.GetOneStorage(ID)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = ob
|
|
}
|
|
}
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Get multiple storages by IDs
|
|
// @Description Return Storage objects if found in the DB. Not found IDs will be ignored
|
|
// @Param IDs path []string true "List of storage IDs"
|
|
// @Success 200 {object} []models.ComputingModel
|
|
// @Failure 403 IDs are empty
|
|
// @router /multi/:IDs [get]
|
|
func (o *StorageController) GetMultipleStorage(IDs []string) {
|
|
if len(IDs) != 0 {
|
|
ob, err := models.GetMultipleStorage(IDs)
|
|
if err != nil {
|
|
o.Ctx.Output.SetStatus(500)
|
|
} else {
|
|
o.Data["json"] = ob
|
|
}
|
|
} else {
|
|
o.Ctx.Output.SetStatus(403)
|
|
}
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Create Storage
|
|
// @Description submit storage object
|
|
// @Param body body models.StorageNEWModel true "The object content"
|
|
// @Success 200 {string} models.StorageModel
|
|
// @Failure 403 Missing body or fields
|
|
// @router / [post]
|
|
func (o *StorageController) PostStorage(body models.StorageNEWModel) {
|
|
err := validate.Struct(body)
|
|
// validationErrors := err.(validator.ValidationErrors)
|
|
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
o.Ctx.Output.Status = 403
|
|
o.ServeJSON()
|
|
return
|
|
}
|
|
|
|
ID, err := models.PostOneStorage(body)
|
|
if err != nil {
|
|
o.Ctx.Output.SetStatus(500)
|
|
return
|
|
}
|
|
|
|
o.Data["json"] = map[string]string{"ID": ID}
|
|
o.ServeJSON()
|
|
}
|