This commit is contained in:
mr
2026-05-28 08:15:02 +02:00
parent 3be023b9af
commit 55c1b70064
7 changed files with 97 additions and 19 deletions
+62 -12
View File
@@ -9,6 +9,7 @@ import (
oclib "cloud.o-forge.io/core/oc-lib"
"cloud.o-forge.io/core/oc-lib/dbs"
"cloud.o-forge.io/core/oc-lib/models/booking"
"cloud.o-forge.io/core/oc-lib/models/peer"
beego "github.com/beego/beego/v2/server/web"
"github.com/gorilla/websocket"
"go.mongodb.org/mongo-driver/bson/primitive"
@@ -21,41 +22,77 @@ type BookingController struct {
var BookingExample booking.Booking
// bookingDestFilter builds a dest_peer_id filter for booking queries.
// Default: filter by self (master's own bookings).
// With nanoID: verify the ID belongs to a NANO peer, then filter by it.
// Returns nil when nanoID is provided but does not resolve to a NANO peer
// (caller should return HTTP 404 or empty result).
func bookingDestFilter(nanoID string) *dbs.Filters {
targetID := ""
if nanoID != "" {
d := oclib.NewRequestAdmin(oclib.LibDataEnum(oclib.PEER), nil).Search(&dbs.Filters{
And: map[string][]dbs.Filter{
"id": {{Operator: dbs.EQUAL.String(), Value: nanoID}},
"relation": {{Operator: dbs.EQUAL.String(), Value: peer.NANO}},
},
}, "", false, 0, 1)
if len(d.Data) == 0 {
return nil
}
targetID = nanoID
} else {
if self, err := oclib.GetMySelf(); err == nil && self != nil {
targetID = self.GetID()
}
}
if targetID == "" {
return nil
}
return &dbs.Filters{
And: map[string][]dbs.Filter{
"dest_peer_id": {{Operator: dbs.EQUAL.String(), Value: targetID}},
},
}
}
// @Title Search
// @Description search bookings
// @Param start_date path string true "the word search you want to get"
// @Param end_date path string true "the word search you want to get"
// @Param is_draft query string false "draft wished"
// @Param nano_id query string false "nano peer UUID — if set, return bookings for that nano instead of self"
// @Param offset query string false
// @Param limit query string false
// @Success 200 {workspace} models.workspace
// @router /search/:start_date/:end_date [get]
func (o *BookingController) Search() {
/*
* This is a sample of how to use the search function
* The search function is used to search for data in the database
* The search function takes in a filter and a data type
* The filter is a struct that contains the search parameters
* The data type is an enum that specifies the type of data to search for
* The search function returns a list of data that matches the filter
* The data is then returned as a json object
*/
// store and return Id or post with UUID
user, peerID, groups := oclib.ExtractTokenInfo(*o.Ctx.Request)
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
start_date, _ := time.ParseInLocation("2006-01-02", o.Ctx.Input.Param(":start_date"), time.UTC)
end_date, _ := time.ParseInLocation("2006-01-02", o.Ctx.Input.Param(":end_date"), time.UTC)
isDraft := o.Ctx.Input.Query("is_draft")
nanoID := o.Ctx.Input.Query("nano_id")
sd := primitive.NewDateTimeFromTime(start_date)
ed := primitive.NewDateTimeFromTime(end_date)
fmt.Println("SEARCH START END", start_date, end_date)
df := bookingDestFilter(nanoID)
if nanoID != "" && df == nil {
o.Ctx.Output.SetStatus(http.StatusNotFound)
o.Data["json"] = map[string]string{"error": "nano peer not found: " + nanoID}
o.ServeJSON()
return
}
f := dbs.Filters{
And: map[string][]dbs.Filter{
"expected_start_date": {{Operator: "gte", Value: sd}, {Operator: "lte", Value: ed}},
},
}
if df != nil {
for k, v := range df.And {
f.And[k] = v
}
}
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(&f, "", isDraft == "true", int64(offset), int64(limit))
o.ServeJSON()
}
@@ -63,6 +100,7 @@ func (o *BookingController) Search() {
// @Title GetAll
// @Description find booking by id
// @Param is_draft query string false "draft wished"
// @Param nano_id query string false "nano peer UUID — if set, return bookings for that nano instead of self"
// @Param offset query string false
// @Param limit query string false
// @Success 200 {booking} models.booking
@@ -72,7 +110,19 @@ func (o *BookingController) GetAll() {
offset, _ := strconv.Atoi(o.Ctx.Input.Query("offset"))
limit, _ := strconv.Atoi(o.Ctx.Input.Query("limit"))
isDraft := o.Ctx.Input.Query("is_draft")
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
nanoID := o.Ctx.Input.Query("nano_id")
df := bookingDestFilter(nanoID)
if nanoID != "" && df == nil {
o.Ctx.Output.SetStatus(http.StatusNotFound)
o.Data["json"] = map[string]string{"error": "nano peer not found: " + nanoID}
o.ServeJSON()
return
}
if df != nil {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).Search(df, "", isDraft == "true", int64(offset), int64(limit))
} else {
o.Data["json"] = oclib.NewRequest(oclib.LibDataEnum(oclib.BOOKING), user, peerID, groups, nil).LoadAll(isDraft == "true", int64(offset), int64(limit))
}
o.ServeJSON()
}