PEER -> SearchExtended

This commit is contained in:
mr
2026-04-08 11:29:16 +02:00
parent 781b4e3add
commit 4484c2d5d9
8 changed files with 296 additions and 58 deletions

View File

@@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"oc-peer/infrastructure"
"strconv"
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
@@ -23,6 +24,8 @@ type PeerController struct {
// @Description search workspace
// @Param search path string true "the word search you want to get"
// @Param is_draft query string false
// @Param offset query string false
// @Param limit query string false
// @Success 200 {workspace} models.workspace
// @router /search/:search [get]
func (o *PeerController) Search() {
@@ -30,27 +33,57 @@ func (o *PeerController) Search() {
// store and return Id or post with UUIDLibDataEnum
search := o.Ctx.Input.Param(":search")
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")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(nil, search, isDraft == "true", int64(offset), int64(limit))
o.ServeJSON()
}
// @Title Search
// @Description search workspace
// @Param is_draft query string false
// @Param offset query string false
// @Param limit query string false
// @Param data body json true "body for data content (Json format)"
// @Success 200 {workspace} models.workspace
// @router /extended/search [post]
func (o *PeerController) SearchExtended() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// store and return Id or post with UUIDLibDataEnum
isDraft := o.Ctx.Input.Query("is_draft")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
var res map[string]interface{}
json.Unmarshal(o.Ctx.Input.CopyBody(100000), &res)
fmt.Println(res, oclib.FiltersFromFlatMap(res, &peer.Peer{}))
data := oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(
oclib.FiltersFromFlatMap(res, &peer.Peer{}), "", isDraft == "true", int64(offset), int64(limit))
fmt.Println(data.Data[0].GetName())
o.Data["json"] = data
o.ServeJSON()
}
// @Title GetAll
// @Description find all peer
// @Param is_draft query string false
// @Param offset query string false
// @Param limit query string false
// @Success 200 {peer} models.peer
// @router / [get]
func (o *PeerController) GetAll() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
isDraft := o.Ctx.Input.Query("is_draft")
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
verify := o.Ctx.Input.Query("verify")
if verify == "true" {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"verify": {{Operator: dbs.EQUAL.String(), Value: true}},
},
}, "", false)
}, "", false, int64(offset), int64(limit))
} else {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadAll(isDraft == "true")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
}
o.ServeJSON()
}
@@ -113,11 +146,38 @@ func (o *PeerController) Valid() {
o.ServeJSON()
}
// @Title add
// @Description add peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /add/:id [post]
func (o *PeerController) Add() {
user, _, _ := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
infrastructure.SearchMu.Lock()
if infrastructure.SearchStreamAction[user] != nil {
for _, p := range infrastructure.SearchStreamAction[user] {
if p.GetID() == id {
req := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil)
o.Data["json"] = req.StoreOne(p.Serialize(p))
o.ServeJSON()
return
}
}
}
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 404,
"error": "peer not received found",
}
o.ServeJSON()
}
// @Title known
// @Description add kwown peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /:id/known [post]
// @router /known/:id [post]
func (o *PeerController) Known() {
user, _, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
@@ -131,7 +191,7 @@ func (o *PeerController) Known() {
// @Description add partner peer by peerid
// @Param id path string true "the peer id you want to blacklist"
// @Success 200 {peer} models.peer
// @router /:id/partner [post]
// @router /partner/:id [post]
func (o *PeerController) Partner() {
user, _, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
@@ -144,7 +204,7 @@ func (o *PeerController) Partner() {
// @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]
// @router /blacklist/:id [post]
func (o *PeerController) Blacklist() {
user, _, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
id := o.Ctx.Input.Param(":id")
@@ -215,25 +275,32 @@ func (o *PeerController) changeRelation(id string, dest *peer.Peer, user string,
}
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"code": 404,
"error": "peer not found.",
}
o.ServeJSON()
}
// TODO : link
// @Title DeleteState
// @Description delete state peer by peerid
// @Title Delete
// @Description delete 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() {
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
// @router /:id [delete]
func (o *PeerController) Delete() {
id := o.Ctx.Input.Param(":id")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).UpdateOne(map[string]interface{}{
"state": peer.NONE,
}, id)
if ok, _ := oclib.IsMySelf(id); ok {
o.Data["json"] = map[string]interface{}{
"data": nil,
"code": 400,
"error": "can't remove myself",
}
o.ServeJSON()
return
}
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.PEER), user, peerID, groups, nil).DeleteOne(id)
fmt.Println(o.Data["json"])
o.ServeJSON()
}