91 lines
2.1 KiB
Go
91 lines
2.1 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 peer
|
|
// @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)
|
|
peerid := models.AddOne(ob)
|
|
o.Data["json"] = map[string]string{"PeerId": peerid}
|
|
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")
|
|
if peerId != "" {
|
|
ob, err := models.GetOne(peerId)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = ob
|
|
}
|
|
}
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title GetAll
|
|
// @Description get all peers
|
|
// @Success 200
|
|
// @router / [get]
|
|
func (o *PeerController) GetAll() {
|
|
obs := models.GetAll()
|
|
o.Data["json"] = obs
|
|
o.ServeJSON()
|
|
}
|
|
|
|
// @Title Update
|
|
// @Description update the peer
|
|
// @Param peerId path string true "The peerid you want to update"
|
|
// @Param body body models.Peer true "The body"
|
|
// @Success 200 {peer} models.Peer
|
|
// @Failure 403 :peerId is empty
|
|
// @router /:peerId [put]
|
|
func (o *PeerController) Put() {
|
|
peerId := o.Ctx.Input.Param(":peerId")
|
|
var ob models.Peer
|
|
json.Unmarshal(o.Ctx.Input.RequestBody, &ob)
|
|
|
|
err := models.Update(peerId, ob.Score)
|
|
if err != nil {
|
|
o.Data["json"] = err.Error()
|
|
} else {
|
|
o.Data["json"] = "update success!"
|
|
}
|
|
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")
|
|
models.Delete(peerId)
|
|
o.Data["json"] = "delete success!"
|
|
o.ServeJSON()
|
|
}
|