deprecated-oc-catalog/controllers/data.go
2023-03-03 14:43:11 +01:00

86 lines
1.9 KiB
Go

package controllers
import (
"cloud.o-forge.io/core/oc-catalog/models"
beego "github.com/beego/beego/v2/server/web"
"github.com/go-playground/validator/v10"
)
// All operations related to the rType data
type DataController struct {
beego.Controller
}
var validate *validator.Validate
func init() {
validate = validator.New()
}
// @Title Get data by ID
// @Description Find rType data based on ID
// @Param ID path string true "The ID of the data resource"
// @Success 200 {object} models.DataModel
// @Failure 403 ID is empty
// @router /:ID [get]
func (o *DataController) GetOneData(ID string) {
if ID != "" {
ob, err := models.GetOneData(ID)
if err != nil {
o.Ctx.Output.SetStatus(500)
} else {
o.Data["json"] = ob
}
} else {
o.Ctx.Output.SetStatus(403)
}
o.ServeJSON()
}
// @Title Get multiple data by IDs
// @Description Return Data object if found in the DB. Not found IDs will be ignored
// @Param IDs path []string true "List of data IDs"
// @Success 200 {object} []models.DataModel
// @Failure 403 IDs are empty
// @router /multi/:IDs [get]
func (o *DataController) GetMultipleData(IDs []string) {
if len(IDs) != 0 {
ob, err := models.GetMultipleData(IDs)
if err != nil {
o.Ctx.Output.SetStatus(500)
} else {
o.Data["json"] = ob
}
} else {
o.Ctx.Output.SetStatus(403)
}
o.ServeJSON()
}
// @Title Create Data
// @Description Submit data object
// @Param body body models.DataNEWModel true "The object content"
// @Success 200 {string} ID
// @Failure 403 Missing body or fields
// @router / [post]
func (o *DataController) PostData(body models.DataNEWModel) {
err := validate.Struct(body)
// validationErrors := err.(validator.ValidationErrors)
if err != nil {
o.Ctx.Output.Status = 403
o.ServeJSON()
return
}
ID, err := models.PostOneData(body)
if err != nil {
o.Ctx.Output.SetStatus(500)
return
}
o.Data["json"] = map[string]string{"ID": ID}
o.ServeJSON()
}