Skip to content

Commit

Permalink
🐛 fix: mongo action (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing committed Aug 26, 2024
1 parent e542a7a commit 5311098
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
19 changes: 19 additions & 0 deletions pkg/response/actions/mgm/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
19 changes: 19 additions & 0 deletions pkg/response/actions/mgm/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
11 changes: 11 additions & 0 deletions pkg/response/actions/mgm/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 19 additions & 2 deletions pkg/response/actions/mgm/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand All @@ -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/
Expand All @@ -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)
Expand Down

0 comments on commit 5311098

Please sign in to comment.