code to create and delete groupMmber collection in DB

This commit is contained in:
soukainna
2023-01-17 15:07:16 +01:00
parent 57540f2587
commit 312200e210
11 changed files with 350 additions and 154 deletions

View File

@@ -9,7 +9,8 @@ type Group struct {
///////////////////code
type GroupMember struct {
ID string `json:"id" bson:"_id"`
Groupid string `json:"grouid" bson:"_groupid"`
Data map[string]any `json:"data"`
ID string `json:"id" bson:"_id"`
Memberid string `json:"memberid"`
Groupid string `json:"grouid"`
Data map[string]any `json:"data"`
}

View File

@@ -166,6 +166,16 @@ func (s MongoDBStorage) CreateGroupMember(groupMember GroupMember) error {
return nil
}
func (s MongoDBStorage) DeleteGroupMember(id string) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
if _, err := collection.DeleteOne(context.TODO(), bson.M{"_id": id}); err != nil {
return err
}
return nil
}
func (s MongoDBStorage) GetGroupMember(id string) (*GroupMember, error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
@@ -188,17 +198,17 @@ func (s MongoDBStorage) GetGroupsMember(namespaces []string) (groupsMember []Gro
if err != nil {
return groupsMember, err
}
// if len(namespaces) == 0 {
// cur, err = collection.Find(context.TODO(), bson.D{}, findOptions)
// if err != nil {
// return groupsMember, err
// }
// } else {
// cur, err = collection.Find(context.TODO(), bson.M{"namespace": bson.M{"$in": namespaces}}, findOptions)
// if err != nil {
// return groupsMember, err
// }
// }
if len(namespaces) == 0 {
cur, err = collection.Find(context.TODO(), bson.D{}, findOptions)
if err != nil {
return groupsMember, err
}
} else {
cur, err = collection.Find(context.TODO(), bson.M{"groupid": bson.M{"$in": namespaces}}, findOptions)
if err != nil {
return groupsMember, err
}
}
for cur.Next(context.TODO()) {
var group GroupMember
@@ -228,7 +238,7 @@ func (s MongoDBStorage) GetGroupsMemberByIds(groupids []string) (groupsMember []
if len(groupids) == 0 {
return groupsMember, errors.New("no group id provided")
} else {
cur, err = collection.Find(context.TODO(), bson.M{"_id": bson.M{"$in": groupids}}, findOptions)
cur, err = collection.Find(context.TODO(), bson.M{"groupid": bson.M{"$in": groupids}}, findOptions)
if err != nil {
return groupsMember, err
}

View File

@@ -18,6 +18,7 @@ type Storage interface {
GetGroupsMember([]string) ([]GroupMember, error)
GetGroupsMemberByIds([]string) ([]GroupMember, error)
UpdateGroupMember(GroupMember) error
DeleteGroupMember(string) error
}
func NewStorage(cfg *viper.Viper) (Storage, error) {