oc-discovery/controllers/peer.go

81 lines
1.8 KiB
Go
Raw Normal View History

2023-03-07 13:29:08 +01:00
package controllers
import (
"encoding/json"
"oc-discovery/models"
beego "github.com/beego/beego/v2/server/web"
)
// Operations about peer
type PeerController struct {
beego.Controller
}
// @Title Create
2023-03-08 16:48:36 +01:00
// @Description create peers
// @Param body body []models.Peer true "The peer content"
2023-03-07 13:29:08 +01:00
// @Success 200 {string} models.Peer.Id
// @Failure 403 body is empty
// @router / [post]
func (o *PeerController) Post() {
2023-03-08 16:48:36 +01:00
var ob []models.Peer
2023-03-07 13:29:08 +01:00
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
2023-03-08 16:48:36 +01:00
models.AddPeers(ob)
o.Data["json"] = map[string]string{"Added": "OK"}
2023-03-07 13:29:08 +01:00
o.ServeJSON()
}
// @Title Get
// @Description find peer by peerid
// @Param peerId path string true "the peerid you want to get"
// @Success 200 {peer} models.Peer
// @Failure 403 :peerId is empty
// @router /:peerId [get]
func (o *PeerController) Get() {
peerId := o.Ctx.Input.Param(":peerId")
2023-03-08 16:48:36 +01:00
peer, err := models.GetPeer(peerId)
if err != nil {
o.Data["json"] = err.Error()
} else {
o.Data["json"] = peer
2023-03-07 13:29:08 +01:00
}
o.ServeJSON()
}
2023-03-08 16:48:36 +01:00
// @Title Find
// @Description find peers with query
// @Param query path string true "the keywords you need"
// @Success 200 {peers} []models.Peer
// @Failure 403
// @router /find/:query [get]
func (o *PeerController) Find() {
query := o.Ctx.Input.Param(":query")
peers, err := models.FindPeers(query)
2023-03-07 13:29:08 +01:00
if err != nil {
o.Data["json"] = err.Error()
} else {
2023-03-08 16:48:36 +01:00
o.Data["json"] = peers
2023-03-07 13:29:08 +01:00
}
o.ServeJSON()
}
// @Title Delete
// @Description delete the peer
// @Param peerId path string true "The peerId you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 peerId is empty
// @router /:peerId [delete]
func (o *PeerController) Delete() {
peerId := o.Ctx.Input.Param(":peerId")
2023-03-08 16:48:36 +01:00
err := models.Delete(peerId)
if err != nil {
o.Data["json"] = err.Error()
} else {
o.Data["json"] = "delete success!"
}
2023-03-07 13:29:08 +01:00
o.ServeJSON()
}