From 7bea76065a001c880da7b2b8ef5d02832c6d22a4 Mon Sep 17 00:00:00 2001 From: Hoang Date: Mon, 8 Nov 2021 17:23:03 +0700 Subject: [PATCH] fix comment --- action_delete.go | 4 ++-- action_get_all.go | 2 +- action_update.go | 4 ++-- db.go | 34 ++++++++++++++++++++++++++++++++++ devicemngt.go | 2 +- helper.go | 27 --------------------------- model.go | 4 ++-- 7 files changed, 42 insertions(+), 35 deletions(-) create mode 100644 db.go diff --git a/action_delete.go b/action_delete.go index 8c3a87b..74c1253 100644 --- a/action_delete.go +++ b/action_delete.go @@ -14,14 +14,14 @@ func (s Service) DeleteByDeviceID(deviceID string) error { ctx = context.Background() col = s.getDeviceCollection() cond = bson.M{ - "deviceID": deviceID, + "deviceId": deviceID, } ) // Delete if _, err := col.DeleteOne(ctx, cond); err != nil { logger.Error("devicemngt - deleteByDeviceID", logger.LogData{ - "deviceID": deviceID, + "deviceId": deviceID, "err": err.Error(), }) return fmt.Errorf("error when delete device: %s", err.Error()) diff --git a/action_get_all.go b/action_get_all.go index 654f530..c7889bc 100644 --- a/action_get_all.go +++ b/action_get_all.go @@ -14,7 +14,7 @@ func (s Service) FindAllDevicesByUserID(userID string) []Device { col = s.getDeviceCollection() result = make([]Device, 0) cond = bson.M{ - "userID": userID, + "userId": userID, } ) diff --git a/action_update.go b/action_update.go index b92321c..9313b88 100644 --- a/action_update.go +++ b/action_update.go @@ -26,7 +26,7 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error ctx = context.Background() col = s.getDeviceCollection() cond = bson.M{ - "deviceID": deviceID, + "deviceId": deviceID, } ) @@ -60,7 +60,7 @@ func (s Service) UpdateByDeviceID(deviceID string, payload UpdateOptions) error _, err = col.UpdateOne(ctx, cond, updateData) if err != nil { logger.Error("devicemngt - updateByDeviceID", logger.LogData{ - "deviceID": deviceID, + "deviceId": deviceID, "err": err.Error(), }) return fmt.Errorf("error when update device: %s", err.Error()) diff --git a/db.go b/db.go new file mode 100644 index 0000000..222c272 --- /dev/null +++ b/db.go @@ -0,0 +1,34 @@ +package devicemngmt + +import ( + "context" + "fmt" + + "github.com/Selly-Modules/logger" + "go.mongodb.org/mongo-driver/bson" + "go.mongodb.org/mongo-driver/mongo" +) + +// getDeviceCollection ... +func (s Service) getDeviceCollection() *mongo.Collection { + if s.TablePrefix != "" { + return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableDevice)) + } + return s.DB.Collection(tableDevice) +} + +func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool { + var ( + col = s.getDeviceCollection() + device = Device{} + ) + + if err := col.FindOne(ctx, bson.M{"deviceId": deviceID}).Decode(&device); err != nil { + logger.Error("devicemngt - findByDeviceID", logger.LogData{ + "deviceId": deviceID, + "err": err.Error(), + }) + return true + } + return !device.ID.IsZero() +} diff --git a/devicemngt.go b/devicemngt.go index 568e032..cdcfab8 100644 --- a/devicemngt.go +++ b/devicemngt.go @@ -31,7 +31,7 @@ var s *Service // Init ... func Init(config Config) (*Service, error) { - if config.MongoDB.Host == "" || config.TablePrefix == "" { + if config.MongoDB.Host == "" { return nil, errors.New("please provide all necessary information for init device") } diff --git a/helper.go b/helper.go index 2e5d736..95c43bc 100644 --- a/helper.go +++ b/helper.go @@ -1,36 +1,9 @@ package devicemngmt import ( - "context" - "fmt" - - "github.com/Selly-Modules/logger" ua "github.com/mssola/user_agent" - "go.mongodb.org/mongo-driver/bson" - "go.mongodb.org/mongo-driver/mongo" ) -// getDeviceCollection ... -func (s Service) getDeviceCollection() *mongo.Collection { - return s.DB.Collection(fmt.Sprintf("%s-%s", s.TablePrefix, tableDevice)) -} - -func (s Service) isDeviceIDExisted(ctx context.Context, deviceID string) bool { - var ( - col = s.getDeviceCollection() - device = Device{} - ) - - if err := col.FindOne(ctx, bson.M{"deviceID": deviceID}).Decode(&device); err != nil { - logger.Error("devicemngt - findByDeviceID", logger.LogData{ - "deviceID": deviceID, - "err": err.Error(), - }) - return true - } - return !device.ID.IsZero() -} - func getUserAgentData(userAgent string) (string, string, bool) { uaData := ua.New(userAgent) return uaData.OSInfo().Name, uaData.OSInfo().Version, uaData.Mobile() diff --git a/model.go b/model.go index 2550f65..5a73a5d 100644 --- a/model.go +++ b/model.go @@ -9,7 +9,7 @@ import ( // Device ... type Device struct { ID primitive.ObjectID `bson:"_id" json:"_id"` - DeviceID string `bson:"deviceID" json:"deviceId"` // unique + DeviceID string `bson:"deviceId" json:"deviceId"` // unique IP string `bson:"ip" json:"ip"` OSName string `bson:"osName" json:"osName"` OSVersion string `bson:"osVersion" json:"osVersion"` @@ -17,7 +17,7 @@ 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 string `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" json:"model"`