oc-peer/controllers/peer.go

113 lines
3.8 KiB
Go
Raw Permalink Normal View History

2024-08-21 16:25:24 +02:00
package controllers
import (
"encoding/json"
oclib "cloud.o-forge.io/core/oc-lib"
2024-10-15 16:55:29 +02:00
"cloud.o-forge.io/core/oc-lib/models/peer"
2024-08-21 16:25:24 +02:00
beego "github.com/beego/beego/v2/server/web"
)
// Operations about workflow
type PeerController struct {
beego.Controller
}
// @Title Search
// @Description search workspace
// @Param search path string true "the word search you want to get"
2025-01-17 17:22:25 +01:00
// @Param is_draft query string false
2024-08-21 16:25:24 +02:00
// @Success 200 {workspace} models.workspace
// @router /search/:search [get]
func (o *PeerController) Search() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-11-07 13:39:45 +01:00
// store and return Id or post with UUIDLibDataEnum
2024-08-21 16:25:24 +02:00
search := o.Ctx.Input.Param(":search")
2025-01-17 17:22:25 +01:00
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(nil, search, isDraft == "true")
2024-08-21 16:25:24 +02:00
o.ServeJSON()
}
// @Title Update
// @Description create peers
// @Param id path string true "the peer id you want to get"
// @Param body body models.peer true "The peer content"
// @Success 200 {object} models.peer
// @router /:id [put]
func (o *PeerController) Put() {
// store and return Id or post with UUID
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-21 16:25:24 +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:22:25 +01:00
data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(res, id)
2024-08-21 16:25:24 +02:00
o.Data["json"] = data
o.ServeJSON()
}
// @Title GetAll
// @Description find all peer
2025-01-17 17:22:25 +01:00
// @Param is_draft query string false
2024-08-21 16:25:24 +02:00
// @Success 200 {peer} models.peer
// @router / [get]
func (o *PeerController) GetAll() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadAll(isDraft == "true")
2024-08-21 16:25:24 +02:00
o.ServeJSON()
}
// @Title Get
// @Description find peer by peerid
// @Param id path string true "the peer id you want to get"
// @Success 200 {peer} models.peer
// @router /:id [get]
func (o *PeerController) Get() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-08-21 16:25:24 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:22:25 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadOne(id)
2024-08-21 16:25:24 +02:00
o.ServeJSON()
}
2024-10-15 16:55:29 +02:00
// @Title Partner
// @Description add partner peer by peerid
// @Param id path string true "the peer id you want to partner"
// @Success 200 {peer} models.peer
// @router /:id/partner [post]
func (o *PeerController) Partner() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-10-15 16:55:29 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:22:25 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{
2024-10-15 16:55:29 +02:00
"state": peer.PARTNER,
}, id)
o.ServeJSON()
}
// @Title Blacklist
// @Description add blacklist peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /:id/blacklist [post]
func (o *PeerController) Blacklist() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-10-15 16:55:29 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:22:25 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{
2024-10-15 16:55:29 +02:00
"state": peer.BLACKLIST,
}, id)
o.ServeJSON()
}
// @Title DeleteState
// @Description delete state peer by peerid
// @Param id path string true "the peer id you want to delete state"
// @Success 200 {peer} models.peer
// @router /:id/undo_state [post]
func (o *PeerController) DeleteState() {
2025-01-17 17:22:25 +01:00
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
2024-10-15 16:55:29 +02:00
id := o.Ctx.Input.Param(":id")
2025-01-17 17:22:25 +01:00
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{
2024-10-15 16:55:29 +02:00
"state": peer.NONE,
}, id)
o.ServeJSON()
}