From 8fcb6cdb08ae06473b4ecba7794f294d39fca658 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 5 Nov 2021 16:57:33 +0700 Subject: [PATCH 1/2] add allMethod --- action_create.go | 20 ++++++++++-------- action_get_all.go | 52 +++++++++++++++++++++++++++++++++++++++++++++++ model.go | 24 ++++++++++++++++++++++ validate.go | 4 ++-- 4 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 action_get_all.go diff --git a/action_create.go b/action_create.go index eebbd0e..75788f8 100644 --- a/action_create.go +++ b/action_create.go @@ -11,14 +11,16 @@ import ( // CreateOptions ... type CreateOptions struct { - DeviceID string - UserID string - UserAgent string - AppVersion string - IP string - FCMToken string - AuthToken string - Language string + DeviceID string + UserID string + UserAgent string + AppVersion string + IP string + FCMToken string + AuthToken string + Language string + Model string + Manufacturer string } // Create ... @@ -71,6 +73,8 @@ func (payload CreateOptions) newDevice() (result Device, err error) { LastActivatedAt: timeNow, CreatedAt: timeNow, FCMToken: payload.FCMToken, + Model: payload.Model, + Manufacturer: payload.Manufacturer, } // App version diff --git a/action_get_all.go b/action_get_all.go new file mode 100644 index 0000000..b0d4bcd --- /dev/null +++ b/action_get_all.go @@ -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 +} diff --git a/model.go b/model.go index 7c00d78..1036b19 100644 --- a/model.go +++ b/model.go @@ -20,5 +20,29 @@ type Device struct { UserID primitive.ObjectID `bson:"userID" json:"userId"` AuthToken string `bson:"authToken" json:"authToken"` 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"` } + +// 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"` +} diff --git a/validate.go b/validate.go index 9efc49f..476badd 100644 --- a/validate.go +++ b/validate.go @@ -6,7 +6,7 @@ import ( "github.com/Selly-Modules/logger" ) -func (co CreateOptions) validate() error{ +func (co CreateOptions) validate() error { // DeviceID if co.DeviceID == "" { 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{ "payload": co, }) - return errors.New("no userAgent data") + return errors.New("no userAgent data") } // IP From 7a3fa56b3c33207ffeccdda0a12a10e642b6cf00 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 5 Nov 2021 17:24:08 +0700 Subject: [PATCH 2/2] fix comment --- action_create.go | 1 + action_get_all.go | 35 ++++++++--------------------------- model.go | 28 +++------------------------- 3 files changed, 12 insertions(+), 52 deletions(-) diff --git a/action_create.go b/action_create.go index 75788f8..00e9898 100644 --- a/action_create.go +++ b/action_create.go @@ -75,6 +75,7 @@ func (payload CreateOptions) newDevice() (result Device, err error) { FCMToken: payload.FCMToken, Model: payload.Model, Manufacturer: payload.Manufacturer, + UserID: payload.UserID, } // App version diff --git a/action_get_all.go b/action_get_all.go index b0d4bcd..654f530 100644 --- a/action_get_all.go +++ b/action_get_all.go @@ -3,50 +3,31 @@ package devicemngmt import ( "context" - "github.com/Selly-Modules/mongodb" + "github.com/Selly-Modules/logger" "go.mongodb.org/mongo-driver/bson" ) // FindAllDevicesByUserID ... -func (s Service) FindAllDevicesByUserID(userID string) []ResponseDevice { +func (s Service) FindAllDevicesByUserID(userID string) []Device { var ( ctx = context.Background() col = s.getDeviceCollection() - docs = make([]Device, 0) - result = make([]ResponseDevice, 0) + result = make([]Device, 0) cond = bson.M{ - "userID": mongodb.NewIDFromString(userID), + "userID": userID, } ) // Find cursor, err := col.Find(ctx, cond) if err != nil { + logger.Error("devicemngt - findAllDevicesByUserID ", logger.LogData{ + "err": err.Error(), + }) 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, - }) - } + cursor.All(ctx, &result) return result } diff --git a/model.go b/model.go index 1036b19..2550f65 100644 --- a/model.go +++ b/model.go @@ -17,32 +17,10 @@ type Device struct { Language string `bson:"language" json:"language"` // vi, en IsMobile bool `bson:"isMobile" json:"isMobile"` LastActivatedAt time.Time `bson:"lastActivatedAt" json:"lastActivatedAt"` - UserID primitive.ObjectID `bson:"userID" json:"userId"` + UserID string `bson:"userID" json:"userId"` AuthToken string `bson:"authToken" json:"authToken"` FCMToken string `bson:"fcmToken" json:"fcmToken"` - Model string `bson:"model,omitempty" json:"model,omitempty"` - Manufacturer string `bson:"manufacturer,omitempty" json:"manufacturer,omitempty"` + Model string `bson:"model" json:"model"` + Manufacturer string `bson:"manufacturer" json:"manufacturer"` 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"` -}