devicemngmt/action_get_all.go

87 lines
1.8 KiB
Go
Raw Normal View History

2021-11-05 09:57:33 +00:00
package devicemngmt
import (
"context"
2021-12-09 04:35:39 +00:00
"errors"
2021-11-05 09:57:33 +00:00
2022-10-11 07:33:56 +00:00
"git.selly.red/Selly-Modules/logger"
"git.selly.red/Selly-Modules/mongodb"
2021-11-05 09:57:33 +00:00
"go.mongodb.org/mongo-driver/bson"
2021-12-09 04:35:39 +00:00
"go.mongodb.org/mongo-driver/bson/primitive"
2021-11-05 09:57:33 +00:00
)
// FindAllDevicesByUserID ...
2021-11-05 10:24:08 +00:00
func (s Service) FindAllDevicesByUserID(userID string) []Device {
2021-11-05 09:57:33 +00:00
var (
ctx = context.Background()
col = s.getDeviceCollection()
2021-11-05 10:24:08 +00:00
result = make([]Device, 0)
2021-11-22 08:14:36 +00:00
id, _ = mongodb.NewIDFromString(userID)
2021-11-05 09:57:33 +00:00
cond = bson.M{
2021-11-22 08:14:36 +00:00
"userId": id,
2021-11-05 09:57:33 +00:00
}
)
// Find
cursor, err := col.Find(ctx, cond)
if err != nil {
2021-11-10 10:08:39 +00:00
logger.Error("devicemngmt - findAllDevicesByUserID ", logger.LogData{
2022-10-05 10:23:29 +00:00
Source: "devicemngmt.FindAllDevicesByUserID",
Message: err.Error(),
Data: result,
2021-11-05 10:24:08 +00:00
})
2021-11-05 09:57:33 +00:00
return result
}
defer cursor.Close(ctx)
2021-11-05 10:24:08 +00:00
cursor.All(ctx, &result)
2021-11-05 09:57:33 +00:00
return result
}
2021-12-09 04:35:39 +00:00
// FindAllDevicesByUserIDList ...
func (s Service) FindAllDevicesByUserIDList(userIDList []string) (result []Device, err error) {
var (
ctx = context.Background()
col = s.getDeviceCollection()
)
result = make([]Device, 0)
// Validate
if len(userIDList) <= 0 {
err = errors.New("user list must be greater than 0")
}
if len(userIDList) > limit200 {
err = errors.New("user list must be less than 200")
}
// Get ids
ids := make([]primitive.ObjectID, 0)
for _, value := range userIDList {
if id, valid := mongodb.NewIDFromString(value); valid {
ids = append(ids, id)
}
}
// Condition
cond := bson.M{
"userId": bson.M{
"$in": ids,
},
}
// Find
cursor, err := col.Find(ctx, cond)
if err != nil {
logger.Error("devicemngmt - findAllDevicesByUserIDList ", logger.LogData{
2022-10-05 10:23:29 +00:00
Source: "devicemngmt.FindAllDevicesByUserIDList",
Message: err.Error(),
Data: nil,
2021-12-09 04:35:39 +00:00
})
return
}
defer cursor.Close(ctx)
cursor.All(ctx, &result)
return result, nil
}