oc-catalog/controllers/purchase.go
2025-06-24 09:13:29 +02:00

73 lines
2.3 KiB
Go
Executable File

package controllers
import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about compute
type PurchaseController struct {
beego.Controller
}
var order_collection = oclib.LibDataEnum(oclib.ORDER)
var purchase_collection = oclib.LibDataEnum(oclib.PURCHASE_RESOURCE)
// @Title Create
// @Description create compute
// @Param compute body json true "body for compute content (Json format)"
// @Success 200 {compute} models.compute
// @router / [post]
func (o *PurchaseController) Post() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(10000), &res)
stored := oclib.NewRequest(purchase_collection, user, peerID, groups, nil).StoreOne(res)
if stored.Err != "" {
o.Data["json"] = stored
o.ServeJSON()
return
}
o.ServeJSON()
}
// @Title GetAll
// @Description find compute by id
// @Param is_draft query string false "draft wished"
// @Success 200 {compute} models.compute
// @router / [get]
func (o *PurchaseController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(purchase_collection, user, peerID, groups, nil).LoadAll(isDraft == "true")
o.ServeJSON()
}
// @Title Get
// @Description find compute by key word
// @Param search path string true "the search you want to get"
// @Param is_draft query string false "draft wished"
// @Success 200 {compute} models.compute
// @router /search/:search [get]
func (o *PurchaseController) Search() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
search := o.Ctx.Input.Param(":search")
o.Data["json"] = oclib.NewRequest(purchase_collection, user, peerID, groups, nil).Search(nil, search, isDraft == "true")
o.ServeJSON()
}
// @Title Get
// @Description find compute by id
// @Param id path string true "the id you want to get"
// @Success 200 {compute} models.compute
// @router /:id [get]
func (o *PurchaseController) Get() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.NewRequest(purchase_collection, user, peerID, groups, nil).LoadOne(id)
o.ServeJSON()
}