add allMethod

This commit is contained in:
Hoang 2021-11-05 16:57:33 +07:00
parent 85c9692da0
commit 8fcb6cdb08
4 changed files with 90 additions and 10 deletions

View File

@ -11,14 +11,16 @@ import (
// CreateOptions ... // CreateOptions ...
type CreateOptions struct { type CreateOptions struct {
DeviceID string DeviceID string
UserID string UserID string
UserAgent string UserAgent string
AppVersion string AppVersion string
IP string IP string
FCMToken string FCMToken string
AuthToken string AuthToken string
Language string Language string
Model string
Manufacturer string
} }
// Create ... // Create ...
@ -71,6 +73,8 @@ func (payload CreateOptions) newDevice() (result Device, err error) {
LastActivatedAt: timeNow, LastActivatedAt: timeNow,
CreatedAt: timeNow, CreatedAt: timeNow,
FCMToken: payload.FCMToken, FCMToken: payload.FCMToken,
Model: payload.Model,
Manufacturer: payload.Manufacturer,
} }
// App version // App version

52
action_get_all.go Normal file
View File

@ -0,0 +1,52 @@
package devicemngmt
import (
"context"
"github.com/Selly-Modules/mongodb"
"go.mongodb.org/mongo-driver/bson"
)
// FindAllDevicesByUserID ...
func (s Service) FindAllDevicesByUserID(userID string) []ResponseDevice {
var (
ctx = context.Background()
col = s.getDeviceCollection()
docs = make([]Device, 0)
result = make([]ResponseDevice, 0)
cond = bson.M{
"userID": mongodb.NewIDFromString(userID),
}
)
// Find
cursor, err := col.Find(ctx, cond)
if err != nil {
return result
}
defer cursor.Close(ctx)
cursor.All(ctx, &docs)
// Get response data
for _, doc := range docs {
result = append(result, ResponseDevice{
ID: doc.ID,
DeviceID: doc.DeviceID,
IP: doc.IP,
OS: ResponseOS{
Name: doc.OSName,
Version: doc.OSVersion,
},
AppVersion: doc.AppVersion,
Language: doc.Language,
IsMobile: doc.IsMobile,
FCMToken: doc.FCMToken,
Model: doc.Model,
Manufacturer: doc.Manufacturer,
LastActivatedAt: doc.LastActivatedAt,
CreatedAt: doc.CreatedAt,
})
}
return result
}

View File

@ -20,5 +20,29 @@ type Device struct {
UserID primitive.ObjectID `bson:"userID" json:"userId"` UserID primitive.ObjectID `bson:"userID" json:"userId"`
AuthToken string `bson:"authToken" json:"authToken"` AuthToken string `bson:"authToken" json:"authToken"`
FCMToken string `bson:"fcmToken" json:"fcmToken"` FCMToken string `bson:"fcmToken" json:"fcmToken"`
Model string `bson:"model,omitempty" json:"model,omitempty"`
Manufacturer string `bson:"manufacturer,omitempty" json:"manufacturer,omitempty"`
CreatedAt time.Time `bson:"createdAt" json:"createdAt"` CreatedAt time.Time `bson:"createdAt" json:"createdAt"`
} }
// ResponseOS ...
type ResponseOS struct {
Name string `json:"name"`
Version string `json:"version"`
}
// ResponseDevice ...
type ResponseDevice struct {
ID primitive.ObjectID `json:"_id"`
DeviceID string `json:"deviceId"`
IP string `json:"ip"`
OS ResponseOS `json:"os"`
AppVersion string `json:"appVersion"`
Language string `json:"language"`
IsMobile bool `json:"isMobile"`
FCMToken string `json:"fcmToken"`
Model string `json:"model,omitempty"`
Manufacturer string `json:"manufacturer,omitempty"`
LastActivatedAt time.Time `json:"lastActivatedAt"`
CreatedAt time.Time `json:"createdAt"`
}

View File

@ -6,7 +6,7 @@ import (
"github.com/Selly-Modules/logger" "github.com/Selly-Modules/logger"
) )
func (co CreateOptions) validate() error{ func (co CreateOptions) validate() error {
// DeviceID // DeviceID
if co.DeviceID == "" { if co.DeviceID == "" {
logger.Error("devicemngt - Create: no deviceID data", logger.LogData{ logger.Error("devicemngt - Create: no deviceID data", logger.LogData{
@ -20,7 +20,7 @@ func (co CreateOptions) validate() error{
logger.Error("devicemngt - Create: no userAgent data", logger.LogData{ logger.Error("devicemngt - Create: no userAgent data", logger.LogData{
"payload": co, "payload": co,
}) })
return errors.New("no userAgent data") return errors.New("no userAgent data")
} }
// IP // IP