fix comment

This commit is contained in:
Hoang 2021-11-08 17:23:03 +07:00
parent 506a78c566
commit 7bea76065a
7 changed files with 42 additions and 35 deletions

View File

@ -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())

View File

@ -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,
}
)

View File

@ -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())

34
db.go Normal file
View File

@ -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()
}

View File

@ -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")
}

View File

@ -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()

View File

@ -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"`