devicemngmt/action_delete.go

69 lines
1.4 KiB
Go
Raw Normal View History

2021-11-05 10:36:30 +00:00
package devicemngmt
import (
"context"
2021-11-23 09:44:42 +00:00
"errors"
2021-11-05 10:36:30 +00:00
"fmt"
2022-10-11 07:33:56 +00:00
"git.selly.red/Selly-Modules/logger"
"git.selly.red/Selly-Modules/mongodb"
2021-11-05 10:36:30 +00:00
"go.mongodb.org/mongo-driver/bson"
)
2021-11-06 10:35:29 +00:00
// DeleteByDeviceID ...
func (s Service) DeleteByDeviceID(deviceID string) error {
2021-11-05 10:36:30 +00:00
var (
ctx = context.Background()
col = s.getDeviceCollection()
cond = bson.M{
2021-11-08 10:23:03 +00:00
"deviceId": deviceID,
2021-11-05 10:36:30 +00:00
}
)
2021-11-23 09:44:42 +00:00
total, _ := col.CountDocuments(ctx, cond)
if total == 0 {
return errors.New("deviceId not found")
}
2021-11-05 10:36:30 +00:00
// Delete
if _, err := col.DeleteOne(ctx, cond); err != nil {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - deleteByDeviceID", logger.LogData{
2022-10-05 10:23:29 +00:00
Source: "devicemngmt.DeleteByDeviceID",
Message: err.Error(),
Data: deviceID,
2021-11-05 10:36:30 +00:00
})
return fmt.Errorf("error when delete device: %s", err.Error())
}
return nil
}
2021-11-24 08:36:24 +00:00
// DeleteByUserID ...
func (s Service) DeleteByUserID(userID string) error {
var (
2022-02-22 09:36:26 +00:00
ctx = context.Background()
col = s.getDeviceCollection()
2021-11-24 08:36:24 +00:00
)
2022-02-22 09:36:26 +00:00
id, isValid := mongodb.NewIDFromString(userID)
if !isValid {
return errors.New("invalid userID data")
}
cond := bson.M{
"userId": id,
}
2021-11-24 08:36:24 +00:00
// Delete
if _, err := col.DeleteMany(ctx, cond); err != nil {
logger.Error("devicemngmt - deleteByUserID", logger.LogData{
2022-10-05 10:23:29 +00:00
Source: "devicemngmt.DeleteByUserID",
Message: err.Error(),
Data: userID,
2021-11-24 08:36:24 +00:00
})
return fmt.Errorf("error when delete device by userId: %s", err.Error())
}
return nil
}