From 232349cd44a0bcdb183bb0be0cf0b39d4bfa8872 Mon Sep 17 00:00:00 2001 From: Hoang Date: Fri, 5 Nov 2021 17:36:30 +0700 Subject: [PATCH 1/2] add delete and getUserIDByToken method --- action_delete.go | 31 +++++++++++++++++++++++++++++++ action_get_userID.go | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 action_delete.go create mode 100644 action_get_userID.go diff --git a/action_delete.go b/action_delete.go new file mode 100644 index 0000000..cad06c6 --- /dev/null +++ b/action_delete.go @@ -0,0 +1,31 @@ +package devicemngmt + +import ( + "context" + "fmt" + + "github.com/Selly-Modules/logger" + "go.mongodb.org/mongo-driver/bson" +) + +// DeleteDeviceByDeviceID ... +func (s Service) DeleteDeviceByDeviceID(deviceID string) error { + var ( + ctx = context.Background() + col = s.getDeviceCollection() + cond = bson.M{ + "deviceID": deviceID, + } + ) + + // Delete + if _, err := col.DeleteOne(ctx, cond); err != nil { + logger.Error("devicemngt - deleteDeviceByDeviceID", logger.LogData{ + "deviceID": deviceID, + "err": err.Error(), + }) + return fmt.Errorf("error when delete device: %s", err.Error()) + } + + return nil +} diff --git a/action_get_userID.go b/action_get_userID.go new file mode 100644 index 0000000..9c4d47f --- /dev/null +++ b/action_get_userID.go @@ -0,0 +1,32 @@ +package devicemngmt + +import ( + "context" + + "github.com/Selly-Modules/logger" + "go.mongodb.org/mongo-driver/bson" +) + +// GetUserIDByAuthToken ... +func (s Service) GetUserIDByAuthToken(authToken string) (userID string) { + var ( + ctx = context.Background() + col = s.getDeviceCollection() + device = Device{} + cond = bson.M{ + "authToken": authToken, + } + ) + + // Find + if err := col.FindOne(ctx, cond).Decode(&device); err != nil { + logger.Error("devicemngt - getUserIDByAuthToken", logger.LogData{ + "authToken": authToken, + "err": err.Error(), + }) + return + } + + userID = device.UserID + return +} From 1631f921b0388cd52496a9f934bb1cd1b7fbaa8b Mon Sep 17 00:00:00 2001 From: Hoang Date: Sat, 6 Nov 2021 17:35:29 +0700 Subject: [PATCH 2/2] fix comment --- action_delete.go | 6 +++--- action_get_userID.go => action_get_user_id.go | 0 2 files changed, 3 insertions(+), 3 deletions(-) rename action_get_userID.go => action_get_user_id.go (100%) diff --git a/action_delete.go b/action_delete.go index cad06c6..8c3a87b 100644 --- a/action_delete.go +++ b/action_delete.go @@ -8,8 +8,8 @@ import ( "go.mongodb.org/mongo-driver/bson" ) -// DeleteDeviceByDeviceID ... -func (s Service) DeleteDeviceByDeviceID(deviceID string) error { +// DeleteByDeviceID ... +func (s Service) DeleteByDeviceID(deviceID string) error { var ( ctx = context.Background() col = s.getDeviceCollection() @@ -20,7 +20,7 @@ func (s Service) DeleteDeviceByDeviceID(deviceID string) error { // Delete if _, err := col.DeleteOne(ctx, cond); err != nil { - logger.Error("devicemngt - deleteDeviceByDeviceID", logger.LogData{ + logger.Error("devicemngt - deleteByDeviceID", logger.LogData{ "deviceID": deviceID, "err": err.Error(), }) diff --git a/action_get_userID.go b/action_get_user_id.go similarity index 100% rename from action_get_userID.go rename to action_get_user_id.go