Skip to content

Commit 5311098

Browse files
authored
🐛 fix: mongo action (#155)
1 parent e542a7a commit 5311098

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

pkg/response/actions/mgm/control.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ func NewControl(b Base, key string) *Control {
3333
}
3434
}
3535

36+
func (e *Control) Handler() gin.HandlersChain {
37+
h := func(c *gin.Context) {
38+
if e.Model == nil {
39+
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
40+
return
41+
}
42+
switch c.Request.Method {
43+
case http.MethodPost:
44+
e.create(c)
45+
case http.MethodPut:
46+
e.update(c)
47+
default:
48+
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
49+
}
50+
}
51+
chain := gin.HandlersChain{h}
52+
return chain
53+
}
54+
3655
// String action name
3756
func (*Control) String() string {
3857
return "control"

pkg/response/actions/mgm/delete.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ func NewDelete(b Base, key string) *Delete {
3333
}
3434
}
3535

36+
func (e *Delete) Handler() gin.HandlersChain {
37+
h := func(c *gin.Context) {
38+
ids := make([]string, 0)
39+
v := c.Param(e.Key)
40+
if v == "batch" {
41+
api := response.Make(c).Bind(&ids)
42+
if api.Error != nil || len(ids) == 0 {
43+
api.Err(http.StatusUnprocessableEntity)
44+
return
45+
}
46+
e.delete(c, ids...)
47+
return
48+
}
49+
e.delete(c, v)
50+
}
51+
chain := gin.HandlersChain{h}
52+
return chain
53+
}
54+
3655
// String action name
3756
func (*Delete) String() string {
3857
return "deleteMgm"

pkg/response/actions/mgm/get.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ func (*Get) String() string {
4040
return "get"
4141
}
4242

43+
func (e *Get) Handler() gin.HandlersChain {
44+
h := func(c *gin.Context) {
45+
if e.Model == nil {
46+
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
47+
}
48+
e.get(c, e.Key)
49+
}
50+
chain := gin.HandlersChain{h}
51+
return chain
52+
}
53+
4354
func (e *Get) get(c *gin.Context, key string) {
4455
api := response.Make(c)
4556
m := pkg.ModelDeepCopy(e.Model)

pkg/response/actions/mgm/search.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,18 @@ func (*Search) String() string {
4848
return "search"
4949
}
5050

51+
func (e *Search) Handler() gin.HandlersChain {
52+
h := func(c *gin.Context) {
53+
if e.Model == nil {
54+
response.Make(c).Err(http.StatusNotImplemented, "not implemented")
55+
return
56+
}
57+
e.searchMgm(c)
58+
}
59+
chain := gin.HandlersChain{h}
60+
return chain
61+
}
62+
5163
func (e *Search) searchMgm(c *gin.Context) {
5264
req := pkg.DeepCopy(e.Search).(response.Searcher)
5365
api := response.Make(c).Bind(req)
@@ -81,6 +93,7 @@ func (e *Search) searchMgm(c *gin.Context) {
8193
defer result.Close(c)
8294
items := make([]any, 0, req.GetPageSize())
8395
for result.Next(c) {
96+
//var data any
8497
m := pkg.ModelDeepCopy(e.Model)
8598
err = result.Decode(m)
8699
if err != nil {
@@ -91,6 +104,7 @@ func (e *Search) searchMgm(c *gin.Context) {
91104
items = append(items, m)
92105
}
93106
api.PageOK(items, count, req.GetPage(), req.GetPageSize())
107+
return
94108
}
95109
//use Aggregate
96110
//https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
@@ -110,9 +124,12 @@ func (e *Search) searchMgm(c *gin.Context) {
110124
{Key: "$limit", Value: req.GetPageSize()},
111125
}, bson.D{
112126
{Key: "$skip", Value: req.GetPageSize() * (req.GetPage() - 1)},
113-
}, bson.D{
114-
{Key: "$sort", Value: sort},
115127
})
128+
if sort != nil {
129+
pipeline = append(pipeline, bson.D{
130+
{Key: "$sort", Value: sort},
131+
})
132+
}
116133
result, err := mgm.Coll(e.Model).Aggregate(c, pipeline)
117134
if err != nil {
118135
api.AddError(err).Log.ErrorContext(c, "find items error", "error", err)

0 commit comments

Comments
 (0)