devicemngmt/action_get_all.go

36 lines
688 B
Go
Raw Normal View History

2021-11-05 09:57:33 +00:00
package devicemngmt
import (
"context"
2021-11-05 10:24:08 +00:00
"github.com/Selly-Modules/logger"
2021-11-22 08:14:36 +00:00
"github.com/Selly-Modules/mongodb"
2021-11-05 09:57:33 +00:00
"go.mongodb.org/mongo-driver/bson"
)
// 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{
2021-11-05 10:24:08 +00:00
"err": err.Error(),
})
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
}