Add missing gRPC functions

This commit is contained in:
2022-08-11 17:14:21 +02:00
parent 26090e9299
commit 6530d024f8
12 changed files with 448 additions and 164 deletions

View File

@@ -3,6 +3,7 @@ package storage
import (
"context"
"errors"
"fmt"
"github.com/spf13/viper"
"go.mongodb.org/mongo-driver/bson"
@@ -46,17 +47,6 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
return storage, err
}
func (s MongoDBStorage) GetAccount(id string) (*Account, error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
account := &Account{}
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(account); err != nil {
return nil, err
}
return account, nil
}
// LocalAuthentication returns an Account matching with one of username, email or password.
// If username, is provided (not an empty string), it will search by username only
// If username is an empty string and email is provided, it will search by email
@@ -85,6 +75,17 @@ func (s MongoDBStorage) LocalAuthentication(namespace string, username string, e
return account, nil
}
func (s MongoDBStorage) GetAccount(id string) (*Account, error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
account := &Account{}
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(account); err != nil {
return nil, err
}
return account, nil
}
func (s MongoDBStorage) GetAccounts(namespaces []string) (accounts []Account, err error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
@@ -123,6 +124,41 @@ func (s MongoDBStorage) GetAccounts(namespaces []string) (accounts []Account, er
return
}
func (s MongoDBStorage) GetAccountsByIds(accountids []string) (accounts []Account, err error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
var cur *mongo.Cursor
findOptions := options.Find()
if len(accountids) == 0 {
return accounts, errors.New("missing account ids")
} else {
cur, err = collection.Find(context.TODO(), bson.M{"_id": bson.M{"$in": accountids}}, findOptions)
if err != nil {
return accounts, err
}
}
for cur.Next(context.TODO()) {
var account Account
var elem bson.M
err := cur.Decode(&elem)
if err != nil {
return accounts, err
}
bsonBytes, _ := bson.Marshal(elem)
bson.Unmarshal(bsonBytes, &account)
accounts = append(accounts, account)
}
return
}
func (s MongoDBStorage) CreateAccount(account Account) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
if _, err := collection.InsertOne(context.TODO(), account); err != nil {
@@ -135,6 +171,7 @@ func (s MongoDBStorage) CreateAccount(account Account) error {
func (s MongoDBStorage) UpdateAccount(account Account) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["users"])
if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": account.ID}, account); err != nil {
fmt.Println(err)
return err
}