Merge pull request #4 from Selly-Modules/feature/delete-getUserIDByTokenMethod
add delete and getUserIDByToken method
This commit is contained in:
commit
1e95e10fc4
|
@ -0,0 +1,31 @@
|
|||
package devicemngmt
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/Selly-Modules/logger"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
)
|
||||
|
||||
// DeleteByDeviceID ...
|
||||
func (s Service) DeleteByDeviceID(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 - deleteByDeviceID", logger.LogData{
|
||||
"deviceID": deviceID,
|
||||
"err": err.Error(),
|
||||
})
|
||||
return fmt.Errorf("error when delete device: %s", err.Error())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue