feat: optimized api response code

This commit is contained in:
Marvin Zhang
2024-11-13 17:31:05 +08:00
parent 86669be84b
commit c6ca5cb2a9
3 changed files with 16 additions and 6 deletions

View File

@@ -153,7 +153,7 @@ func (ctr *BaseController[T]) DeleteById(c *gin.Context) {
func (ctr *BaseController[T]) DeleteList(c *gin.Context) {
type Payload struct {
Ids []primitive.ObjectID `json:"ids"`
Ids []string `json:"ids"`
}
var payload Payload
@@ -162,9 +162,19 @@ func (ctr *BaseController[T]) DeleteList(c *gin.Context) {
return
}
var ids []primitive.ObjectID
for _, id := range payload.Ids {
objectId, err := primitive.ObjectIDFromHex(id)
if err != nil {
HandleErrorBadRequest(c, err)
return
}
ids = append(ids, objectId)
}
if err := ctr.modelSvc.DeleteMany(bson.M{
"_id": bson.M{
"$in": payload.Ids,
"$in": ids,
},
}); err != nil {
HandleErrorInternalServerError(c, err)