81 lines
1.8 KiB
Go
81 lines
1.8 KiB
Go
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
|
|
// @Description create peers
|
|
// @Param body body []models.Peer true "The peer content"
|
|
// @Success 200 {string} models.Peer.Id
|
|
// @Failure 403 body is empty
|
|
// @router / [post]
|
|
func (o *PeerController) Post() {
|
|
var ob []models.Peer
|
|
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
|
models.AddPeers(ob)
|
|
o.Data["json"] = map[string]string{"Added": "OK"}
|
|
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")
|
|
|
|
peer, err := models.GetPeer(peerId)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = peer
|
|
}
|
|
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @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)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = peers
|
|
}
|
|
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")
|
|
err := models.Delete(peerId)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = "delete success!"
|
|
}
|
|
o.ServeJSON()
|
|
}
|