diff --git a/pkg/response/actions/mgm/control.go b/pkg/response/actions/mgm/control.go index 3ef442d..940162a 100644 --- a/pkg/response/actions/mgm/control.go +++ b/pkg/response/actions/mgm/control.go @@ -33,6 +33,25 @@ func NewControl(b Base, key string) *Control { } } +func (e *Control) Handler() gin.HandlersChain { + h := func(c *gin.Context) { + if e.Model == nil { + response.Make(c).Err(http.StatusNotImplemented, "not implemented") + return + } + switch c.Request.Method { + case http.MethodPost: + e.create(c) + case http.MethodPut: + e.update(c) + default: + response.Make(c).Err(http.StatusNotImplemented, "not implemented") + } + } + chain := gin.HandlersChain{h} + return chain +} + // String action name func (*Control) String() string { return "control" diff --git a/pkg/response/actions/mgm/delete.go b/pkg/response/actions/mgm/delete.go index f4a96d5..7efdf5e 100644 --- a/pkg/response/actions/mgm/delete.go +++ b/pkg/response/actions/mgm/delete.go @@ -33,6 +33,25 @@ func NewDelete(b Base, key string) *Delete { } } +func (e *Delete) Handler() gin.HandlersChain { + h := func(c *gin.Context) { + ids := make([]string, 0) + v := c.Param(e.Key) + if v == "batch" { + api := response.Make(c).Bind(&ids) + if api.Error != nil || len(ids) == 0 { + api.Err(http.StatusUnprocessableEntity) + return + } + e.delete(c, ids...) + return + } + e.delete(c, v) + } + chain := gin.HandlersChain{h} + return chain +} + // String action name func (*Delete) String() string { return "deleteMgm" diff --git a/pkg/response/actions/mgm/get.go b/pkg/response/actions/mgm/get.go index 186f3ce..1fca672 100644 --- a/pkg/response/actions/mgm/get.go +++ b/pkg/response/actions/mgm/get.go @@ -40,6 +40,17 @@ func (*Get) String() string { return "get" } +func (e *Get) Handler() gin.HandlersChain { + h := func(c *gin.Context) { + if e.Model == nil { + response.Make(c).Err(http.StatusNotImplemented, "not implemented") + } + e.get(c, e.Key) + } + chain := gin.HandlersChain{h} + return chain +} + func (e *Get) get(c *gin.Context, key string) { api := response.Make(c) m := pkg.ModelDeepCopy(e.Model) diff --git a/pkg/response/actions/mgm/search.go b/pkg/response/actions/mgm/search.go index ac3cb32..98ef91d 100644 --- a/pkg/response/actions/mgm/search.go +++ b/pkg/response/actions/mgm/search.go @@ -48,6 +48,18 @@ func (*Search) String() string { return "search" } +func (e *Search) Handler() gin.HandlersChain { + h := func(c *gin.Context) { + if e.Model == nil { + response.Make(c).Err(http.StatusNotImplemented, "not implemented") + return + } + e.searchMgm(c) + } + chain := gin.HandlersChain{h} + return chain +} + func (e *Search) searchMgm(c *gin.Context) { req := pkg.DeepCopy(e.Search).(response.Searcher) api := response.Make(c).Bind(req) @@ -81,6 +93,7 @@ func (e *Search) searchMgm(c *gin.Context) { defer result.Close(c) items := make([]any, 0, req.GetPageSize()) for result.Next(c) { + //var data any m := pkg.ModelDeepCopy(e.Model) err = result.Decode(m) if err != nil { @@ -91,6 +104,7 @@ func (e *Search) searchMgm(c *gin.Context) { items = append(items, m) } api.PageOK(items, count, req.GetPage(), req.GetPageSize()) + return } //use Aggregate //https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/ @@ -110,9 +124,12 @@ func (e *Search) searchMgm(c *gin.Context) { {Key: "$limit", Value: req.GetPageSize()}, }, bson.D{ {Key: "$skip", Value: req.GetPageSize() * (req.GetPage() - 1)}, - }, bson.D{ - {Key: "$sort", Value: sort}, }) + if sort != nil { + pipeline = append(pipeline, bson.D{ + {Key: "$sort", Value: sort}, + }) + } result, err := mgm.Coll(e.Model).Aggregate(c, pipeline) if err != nil { api.AddError(err).Log.ErrorContext(c, "find items error", "error", err)