oc-catalog/controllers/storage.go

95 lines
3.2 KiB
Go
Raw Normal View History

2024-07-26 13:07:25 +02:00
package controllers
import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about storage
2024-07-26 13:07:25 +02:00
type StorageController struct {
beego.Controller
}
2025-01-17 17:03:32 +01:00
var storage_collection = oclib.LibDataEnum(oclib.STORAGE_RESOURCE)
2024-07-26 13:07:25 +02:00
// @Title Update
// @Description create storages
// @Param id path string true "the storage id you want to get"
// @Param body body models.storage true "The storage content"
// @Success 200 {storage} models.storage
// @router /:id [put]
func (o *StorageController) Put() {
// store and return Id or post with UUID
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-07-26 13:07:25 +02:00
var res map[string]interface{}
id := o.Ctx.Input.Param(":id")
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
2025-01-17 17:03:32 +01:00
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).UpdateOne(res, id)
o.ServeJSON()
}
2024-07-26 13:07:25 +02:00
// @Title Create
// @Description create storage
// @Param storage body json true "body for storage content (Json format)"
// @Success 200 {storage} models.storage
// @router / [post]
func (o *StorageController) Post() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-07-26 13:07:25 +02:00
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
2025-01-17 17:03:32 +01:00
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).StoreOne(res)
2024-07-26 13:07:25 +02:00
o.ServeJSON()
}
// @Title GetAll
// @Description find storage by id
2025-01-17 17:03:32 +01:00
// @Param is_draft query string false "draft wished"
2024-07-26 13:07:25 +02:00
// @Success 200 {storage} models.storage
// @router / [get]
func (o *StorageController) GetAll() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).LoadAll(isDraft == "true")
o.ServeJSON()
}
// @Title Get
// @Description find storage by key word
// @Param search path string true "the search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {storage} models.storage
// @router /search/:search [get]
func (o *StorageController) Search() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
search := o.Ctx.Input.Param(":search")
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).Search(nil, search, isDraft == "true")
2024-07-26 13:07:25 +02:00
o.ServeJSON()
}
// @Title Get
// @Description find storage by id
// @Param id path string true "the id you want to get"
// @Success 200 {storage} models.storage
// @router /:id [get]
func (o *StorageController) Get() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-07-26 13:07:25 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:03:32 +01:00
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).LoadOne(id)
2024-07-26 13:07:25 +02:00
o.ServeJSON()
}
// @Title Delete
// @Description delete the storage
// @Param id path string true "The id you want to delete"
// @Success 200 {storage} delete success!
// @router /:id [delete]
func (o *StorageController) Delete() {
2025-01-17 17:03:32 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-07-26 13:07:25 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:03:32 +01:00
o.Data["json"] = oclib.NewRequest(storage_collection, user, peerID, groups, nil).DeleteOne(id)
2024-07-26 13:07:25 +02:00
o.ServeJSON()
}