add delete and getUserIDByToken method

This commit is contained in:
Hoang 2021-11-05 17:36:30 +07:00
parent 7a3fa56b3c
commit 232349cd44
2 changed files with 63 additions and 0 deletions

31
action_delete.go Normal file
View File

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

32
action_get_userID.go Normal file
View File

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