/
delete_mgm.go
85 lines (76 loc) · 1.55 KB
/
delete_mgm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
* @Author: lwnmengjing
* @Date: 2023/1/25 17:13:59
* @Last Modified by: lwnmengjing
* @Last Modified time: 2023/1/25 17:13:59
*/
package actions
import (
"errors"
"fmt"
"net/http"
"github.com/gin-gonic/gin"
"github.com/kamva/mgm/v3"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"github.com/mss-boot-io/mss-boot/pkg/response"
)
// Delete action
type Delete struct {
Base
Key string
}
// NewDeleteMgm new deleteMgm action
func NewDeleteMgm(m mgm.Model, key string) *Delete {
return &Delete{
Base: Base{ModelMgm: m},
Key: key,
}
}
// String action name
func (*Delete) String() string {
return "deleteMgm"
}
// Handler action handler
func (e *Delete) Handler() gin.HandlerFunc {
return 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 {
api.Err(http.StatusUnprocessableEntity)
return
}
}
if e.ModelMgm != nil {
e.deleteMgm(c, v)
return
}
if e.ModelGorm != nil {
e.deleteGorm(c, v)
return
}
response.Error(c,
http.StatusNotImplemented,
fmt.Errorf("not implemented"))
}
}
// deleteMgm batch and single deleteMgm
func (e *Delete) deleteMgm(c *gin.Context, ids ...string) {
api := response.Make(c)
if len(ids) == 0 {
api.OK(nil)
return
}
_, err := mgm.Coll(e.ModelMgm).DeleteMany(c, bson.M{"_id": bson.M{"$in": ids}})
if err != nil {
if errors.Is(err, mongo.ErrNoDocuments) {
api.OK(nil)
return
}
api.Err(http.StatusInternalServerError)
return
}
api.OK(nil)
}