Compare commits

..

3 Commits

Author SHA1 Message Date
soukainna 312200e210 code to create and delete groupMmber collection in DB 2023-01-17 15:07:16 +01:00
soukainna 57540f2587 add code grpc to manage new collection db groupMember 2022-12-19 09:14:31 +01:00
soukainna daf39bc067 add code grpc to manage new collection db groupMember 2022-12-19 09:13:56 +01:00
12 changed files with 1772 additions and 125 deletions

View File

@ -19,6 +19,7 @@ func ReadConfig() (*viper.Viper, error) {
"db_name": "coopgo_platform", "db_name": "coopgo_platform",
"collections": map[string]any{ "collections": map[string]any{
"groups": "groups", "groups": "groups",
"groups_member": "groups_member",
}, },
}, },
}, },

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@ syntax = "proto3";
option go_package = "git.coopgo.io/coopgo-platform/groups-management/grpcapi"; option go_package = "git.coopgo.io/coopgo-platform/groups-management/grpcapi";
import "groups.proto"; import "groups.proto";
import "groupsmember.proto";
service GroupsManagement { service GroupsManagement {
rpc AddGroup(AddGroupRequest) returns (AddGroupResponse) {} rpc AddGroup(AddGroupRequest) returns (AddGroupResponse) {}
@ -13,56 +14,100 @@ service GroupsManagement {
rpc GetGroupsBatch(GetGroupsBatchRequest) returns (GetGroupsBatchResponse) {} rpc GetGroupsBatch(GetGroupsBatchRequest) returns (GetGroupsBatchResponse) {}
rpc Subscribe(SubscribeRequest) returns (SubscribeResponse) {} rpc Subscribe(SubscribeRequest) returns (SubscribeResponse) {}
rpc Unsubscribe(UnsubscribeRequest) returns (UnsubscribeResponse) {} rpc Unsubscribe(UnsubscribeRequest) returns (UnsubscribeResponse) {}
//////////member
rpc AddGroupMember(AddGroupMemberRequest) returns (AddGroupMemberResponse) {}
rpc GetGroupMember(GetGroupMemberRequest) returns (GetGroupMemberResponse) {}
rpc GetGroupsMember(GetGroupsMemberRequest) returns (GetGroupsMemberResponse) {}
rpc GetGroupsBatchMember(GetGroupsBatchMemberRequest) returns (GetGroupsBatchMemberResponse) {}
rpc SubscribeMember(SubscribeMemberRequest) returns (SubscribeMemberResponse) {}
rpc UnsubscribeMember(UnsubscribeMemberRequest) returns (UnsubscribeMemberResponse) {}
} }
message AddGroupRequest { message AddGroupRequest {
Group group = 1; Group group = 1;
} }
message AddGroupResponse { message AddGroupResponse {
Group group = 2; Group group = 2;
} }
message GetGroupRequest { message GetGroupRequest {
string id = 3; string id = 3;
string namespace = 4; string namespace = 4;
} }
message GetGroupResponse { message GetGroupResponse {
Group group = 5; Group group = 5;
} }
message GetGroupsRequest { message GetGroupsRequest {
repeated string namespaces = 6; repeated string namespaces = 6;
string member = 7; string member = 7;
} }
message GetGroupsResponse { message GetGroupsResponse {
repeated Group groups = 8; repeated Group groups = 8;
} }
message GetGroupsBatchRequest { message GetGroupsBatchRequest {
repeated string groupids = 10; repeated string groupids = 10;
} }
message GetGroupsBatchResponse { message GetGroupsBatchResponse {
repeated Group groups = 11; repeated Group groups = 11;
} }
message SubscribeRequest { message SubscribeRequest {
string groupid = 20; string groupid = 20;
string memberid = 21; string memberid = 21;
} }
message SubscribeResponse { message SubscribeResponse {
bool ok = 22; bool ok = 22;
} }
message UnsubscribeRequest { message UnsubscribeRequest {
string groupid = 30; string groupid = 30;
string memberid = 31; string memberid = 31;
} }
message UnsubscribeResponse { message UnsubscribeResponse {
bool ok = 32; bool ok = 32;
} }
//////////////////member
message AddGroupMemberRequest {
GroupMember group = 33;
}
message AddGroupMemberResponse {
GroupMember group = 34;
}
message GetGroupMemberRequest {
string id = 35;
string groupid = 36;
}
message GetGroupMemberResponse {
GroupMember group = 37;
}
message GetGroupsMemberRequest {
repeated string namespaces = 38;
}
message GetGroupsMemberResponse {
repeated GroupMember groups = 39;
}
message GetGroupsBatchMemberRequest {
repeated string groupids = 40;
}
message GetGroupsBatchMemberResponse {
repeated GroupMember groups = 41;
}
message SubscribeMemberRequest {
GroupMember group = 42;
}
message SubscribeMemberResponse {
GroupMember group = 43;
}
message UnsubscribeMemberRequest {
string id = 44;
}
message UnsubscribeMemberResponse {
string id = 45;
}

View File

@ -1,7 +1,7 @@
// Code generated by protoc-gen-go-grpc. DO NOT EDIT. // Code generated by protoc-gen-go-grpc. DO NOT EDIT.
// versions: // versions:
// - protoc-gen-go-grpc v1.2.0 // - protoc-gen-go-grpc v1.2.0
// - protoc v3.19.4 // - protoc v3.12.4
// source: groupsmanagement.proto // source: groupsmanagement.proto
package grpcapi package grpcapi
@ -28,6 +28,13 @@ type GroupsManagementClient interface {
GetGroupsBatch(ctx context.Context, in *GetGroupsBatchRequest, opts ...grpc.CallOption) (*GetGroupsBatchResponse, error) GetGroupsBatch(ctx context.Context, in *GetGroupsBatchRequest, opts ...grpc.CallOption) (*GetGroupsBatchResponse, error)
Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (*SubscribeResponse, error) Subscribe(ctx context.Context, in *SubscribeRequest, opts ...grpc.CallOption) (*SubscribeResponse, error)
Unsubscribe(ctx context.Context, in *UnsubscribeRequest, opts ...grpc.CallOption) (*UnsubscribeResponse, error) Unsubscribe(ctx context.Context, in *UnsubscribeRequest, opts ...grpc.CallOption) (*UnsubscribeResponse, error)
//////////member
AddGroupMember(ctx context.Context, in *AddGroupMemberRequest, opts ...grpc.CallOption) (*AddGroupMemberResponse, error)
GetGroupMember(ctx context.Context, in *GetGroupMemberRequest, opts ...grpc.CallOption) (*GetGroupMemberResponse, error)
GetGroupsMember(ctx context.Context, in *GetGroupsMemberRequest, opts ...grpc.CallOption) (*GetGroupsMemberResponse, error)
GetGroupsBatchMember(ctx context.Context, in *GetGroupsBatchMemberRequest, opts ...grpc.CallOption) (*GetGroupsBatchMemberResponse, error)
SubscribeMember(ctx context.Context, in *SubscribeMemberRequest, opts ...grpc.CallOption) (*SubscribeMemberResponse, error)
UnsubscribeMember(ctx context.Context, in *UnsubscribeMemberRequest, opts ...grpc.CallOption) (*UnsubscribeMemberResponse, error)
} }
type groupsManagementClient struct { type groupsManagementClient struct {
@ -92,6 +99,60 @@ func (c *groupsManagementClient) Unsubscribe(ctx context.Context, in *Unsubscrib
return out, nil return out, nil
} }
func (c *groupsManagementClient) AddGroupMember(ctx context.Context, in *AddGroupMemberRequest, opts ...grpc.CallOption) (*AddGroupMemberResponse, error) {
out := new(AddGroupMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/AddGroupMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *groupsManagementClient) GetGroupMember(ctx context.Context, in *GetGroupMemberRequest, opts ...grpc.CallOption) (*GetGroupMemberResponse, error) {
out := new(GetGroupMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/GetGroupMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *groupsManagementClient) GetGroupsMember(ctx context.Context, in *GetGroupsMemberRequest, opts ...grpc.CallOption) (*GetGroupsMemberResponse, error) {
out := new(GetGroupsMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/GetGroupsMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *groupsManagementClient) GetGroupsBatchMember(ctx context.Context, in *GetGroupsBatchMemberRequest, opts ...grpc.CallOption) (*GetGroupsBatchMemberResponse, error) {
out := new(GetGroupsBatchMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/GetGroupsBatchMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *groupsManagementClient) SubscribeMember(ctx context.Context, in *SubscribeMemberRequest, opts ...grpc.CallOption) (*SubscribeMemberResponse, error) {
out := new(SubscribeMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/SubscribeMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *groupsManagementClient) UnsubscribeMember(ctx context.Context, in *UnsubscribeMemberRequest, opts ...grpc.CallOption) (*UnsubscribeMemberResponse, error) {
out := new(UnsubscribeMemberResponse)
err := c.cc.Invoke(ctx, "/GroupsManagement/UnsubscribeMember", in, out, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// GroupsManagementServer is the server API for GroupsManagement service. // GroupsManagementServer is the server API for GroupsManagement service.
// All implementations must embed UnimplementedGroupsManagementServer // All implementations must embed UnimplementedGroupsManagementServer
// for forward compatibility // for forward compatibility
@ -102,6 +163,13 @@ type GroupsManagementServer interface {
GetGroupsBatch(context.Context, *GetGroupsBatchRequest) (*GetGroupsBatchResponse, error) GetGroupsBatch(context.Context, *GetGroupsBatchRequest) (*GetGroupsBatchResponse, error)
Subscribe(context.Context, *SubscribeRequest) (*SubscribeResponse, error) Subscribe(context.Context, *SubscribeRequest) (*SubscribeResponse, error)
Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error) Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error)
//////////member
AddGroupMember(context.Context, *AddGroupMemberRequest) (*AddGroupMemberResponse, error)
GetGroupMember(context.Context, *GetGroupMemberRequest) (*GetGroupMemberResponse, error)
GetGroupsMember(context.Context, *GetGroupsMemberRequest) (*GetGroupsMemberResponse, error)
GetGroupsBatchMember(context.Context, *GetGroupsBatchMemberRequest) (*GetGroupsBatchMemberResponse, error)
SubscribeMember(context.Context, *SubscribeMemberRequest) (*SubscribeMemberResponse, error)
UnsubscribeMember(context.Context, *UnsubscribeMemberRequest) (*UnsubscribeMemberResponse, error)
mustEmbedUnimplementedGroupsManagementServer() mustEmbedUnimplementedGroupsManagementServer()
} }
@ -127,6 +195,24 @@ func (UnimplementedGroupsManagementServer) Subscribe(context.Context, *Subscribe
func (UnimplementedGroupsManagementServer) Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error) { func (UnimplementedGroupsManagementServer) Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented") return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented")
} }
func (UnimplementedGroupsManagementServer) AddGroupMember(context.Context, *AddGroupMemberRequest) (*AddGroupMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method AddGroupMember not implemented")
}
func (UnimplementedGroupsManagementServer) GetGroupMember(context.Context, *GetGroupMemberRequest) (*GetGroupMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetGroupMember not implemented")
}
func (UnimplementedGroupsManagementServer) GetGroupsMember(context.Context, *GetGroupsMemberRequest) (*GetGroupsMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetGroupsMember not implemented")
}
func (UnimplementedGroupsManagementServer) GetGroupsBatchMember(context.Context, *GetGroupsBatchMemberRequest) (*GetGroupsBatchMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetGroupsBatchMember not implemented")
}
func (UnimplementedGroupsManagementServer) SubscribeMember(context.Context, *SubscribeMemberRequest) (*SubscribeMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method SubscribeMember not implemented")
}
func (UnimplementedGroupsManagementServer) UnsubscribeMember(context.Context, *UnsubscribeMemberRequest) (*UnsubscribeMemberResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method UnsubscribeMember not implemented")
}
func (UnimplementedGroupsManagementServer) mustEmbedUnimplementedGroupsManagementServer() {} func (UnimplementedGroupsManagementServer) mustEmbedUnimplementedGroupsManagementServer() {}
// UnsafeGroupsManagementServer may be embedded to opt out of forward compatibility for this service. // UnsafeGroupsManagementServer may be embedded to opt out of forward compatibility for this service.
@ -248,6 +334,114 @@ func _GroupsManagement_Unsubscribe_Handler(srv interface{}, ctx context.Context,
return interceptor(ctx, in, info, handler) return interceptor(ctx, in, info, handler)
} }
func _GroupsManagement_AddGroupMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(AddGroupMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).AddGroupMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/AddGroupMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).AddGroupMember(ctx, req.(*AddGroupMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
func _GroupsManagement_GetGroupMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetGroupMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).GetGroupMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/GetGroupMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).GetGroupMember(ctx, req.(*GetGroupMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
func _GroupsManagement_GetGroupsMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetGroupsMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).GetGroupsMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/GetGroupsMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).GetGroupsMember(ctx, req.(*GetGroupsMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
func _GroupsManagement_GetGroupsBatchMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(GetGroupsBatchMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).GetGroupsBatchMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/GetGroupsBatchMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).GetGroupsBatchMember(ctx, req.(*GetGroupsBatchMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
func _GroupsManagement_SubscribeMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(SubscribeMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).SubscribeMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/SubscribeMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).SubscribeMember(ctx, req.(*SubscribeMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
func _GroupsManagement_UnsubscribeMember_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(UnsubscribeMemberRequest)
if err := dec(in); err != nil {
return nil, err
}
if interceptor == nil {
return srv.(GroupsManagementServer).UnsubscribeMember(ctx, in)
}
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/GroupsManagement/UnsubscribeMember",
}
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(GroupsManagementServer).UnsubscribeMember(ctx, req.(*UnsubscribeMemberRequest))
}
return interceptor(ctx, in, info, handler)
}
// GroupsManagement_ServiceDesc is the grpc.ServiceDesc for GroupsManagement service. // GroupsManagement_ServiceDesc is the grpc.ServiceDesc for GroupsManagement service.
// It's only intended for direct use with grpc.RegisterService, // It's only intended for direct use with grpc.RegisterService,
// and not to be introspected or modified (even as a copy) // and not to be introspected or modified (even as a copy)
@ -279,6 +473,30 @@ var GroupsManagement_ServiceDesc = grpc.ServiceDesc{
MethodName: "Unsubscribe", MethodName: "Unsubscribe",
Handler: _GroupsManagement_Unsubscribe_Handler, Handler: _GroupsManagement_Unsubscribe_Handler,
}, },
{
MethodName: "AddGroupMember",
Handler: _GroupsManagement_AddGroupMember_Handler,
},
{
MethodName: "GetGroupMember",
Handler: _GroupsManagement_GetGroupMember_Handler,
},
{
MethodName: "GetGroupsMember",
Handler: _GroupsManagement_GetGroupsMember_Handler,
},
{
MethodName: "GetGroupsBatchMember",
Handler: _GroupsManagement_GetGroupsBatchMember_Handler,
},
{
MethodName: "SubscribeMember",
Handler: _GroupsManagement_SubscribeMember_Handler,
},
{
MethodName: "UnsubscribeMember",
Handler: _GroupsManagement_UnsubscribeMember_Handler,
},
}, },
Streams: []grpc.StreamDesc{}, Streams: []grpc.StreamDesc{},
Metadata: "groupsmanagement.proto", Metadata: "groupsmanagement.proto",

66
grpcapi/groupsmember.go Normal file
View File

@ -0,0 +1,66 @@
package grpcapi
import (
"encoding/json"
"fmt"
"git.coopgo.io/coopgo-platform/groups-management/storage"
"google.golang.org/protobuf/encoding/protojson"
structpb "google.golang.org/protobuf/types/known/structpb"
)
func (g GroupMember) ToStorageType() storage.GroupMember {
group := storage.GroupMember{
ID: g.Id,
Memberid: g.Memberid,
Groupid: g.Groupid,
Data: map[string]any{},
}
for k, d := range g.Data.GetFields() {
jsondata, err := protojson.Marshal(d)
if err != nil {
fmt.Println(err)
break
}
var data any
json.Unmarshal(jsondata, &data)
group.Data[k] = data
}
return group
}
func GroupFromStorageTypeMember(group *storage.GroupMember) (*GroupMember, error) {
d, err := sanitizeDatta(group.Data)
if err != nil {
return nil, err
}
data, err := structpb.NewStruct(d)
if err != nil {
fmt.Println(err)
return nil, err
}
return &GroupMember{
Id: group.ID,
Memberid: group.Memberid,
Groupid: group.Groupid,
Data: data,
}, nil
}
func sanitizeDatta(data map[string]any) (d map[string]any, err error) {
j, err := json.Marshal(data)
if err != nil {
return nil, err
}
if err = json.Unmarshal(j, &d); err != nil {
return nil, err
}
return d, nil
}

179
grpcapi/groupsmember.pb.go Normal file
View File

@ -0,0 +1,179 @@
// Code generated by protoc-gen-go. DO NOT EDIT.
// versions:
// protoc-gen-go v1.28.1
// protoc v3.12.4
// source: groupsmember.proto
package grpcapi
import (
_struct "github.com/golang/protobuf/ptypes/struct"
protoreflect "google.golang.org/protobuf/reflect/protoreflect"
protoimpl "google.golang.org/protobuf/runtime/protoimpl"
reflect "reflect"
sync "sync"
)
const (
// Verify that this generated code is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
// Verify that runtime/protoimpl is sufficiently up-to-date.
_ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
)
type GroupMember struct {
state protoimpl.MessageState
sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields
Id string `protobuf:"bytes,1,opt,name=id,proto3" json:"id,omitempty"`
Memberid string `protobuf:"bytes,2,opt,name=memberid,proto3" json:"memberid,omitempty"`
Groupid string `protobuf:"bytes,3,opt,name=groupid,proto3" json:"groupid,omitempty"`
Data *_struct.Struct `protobuf:"bytes,4,opt,name=data,proto3" json:"data,omitempty"`
}
func (x *GroupMember) Reset() {
*x = GroupMember{}
if protoimpl.UnsafeEnabled {
mi := &file_groupsmember_proto_msgTypes[0]
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
ms.StoreMessageInfo(mi)
}
}
func (x *GroupMember) String() string {
return protoimpl.X.MessageStringOf(x)
}
func (*GroupMember) ProtoMessage() {}
func (x *GroupMember) ProtoReflect() protoreflect.Message {
mi := &file_groupsmember_proto_msgTypes[0]
if protoimpl.UnsafeEnabled && x != nil {
ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
if ms.LoadMessageInfo() == nil {
ms.StoreMessageInfo(mi)
}
return ms
}
return mi.MessageOf(x)
}
// Deprecated: Use GroupMember.ProtoReflect.Descriptor instead.
func (*GroupMember) Descriptor() ([]byte, []int) {
return file_groupsmember_proto_rawDescGZIP(), []int{0}
}
func (x *GroupMember) GetId() string {
if x != nil {
return x.Id
}
return ""
}
func (x *GroupMember) GetMemberid() string {
if x != nil {
return x.Memberid
}
return ""
}
func (x *GroupMember) GetGroupid() string {
if x != nil {
return x.Groupid
}
return ""
}
func (x *GroupMember) GetData() *_struct.Struct {
if x != nil {
return x.Data
}
return nil
}
var File_groupsmember_proto protoreflect.FileDescriptor
var file_groupsmember_proto_rawDesc = []byte{
0x0a, 0x12, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x73, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x2e, 0x70,
0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2f, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x62, 0x75, 0x66, 0x2f, 0x73, 0x74, 0x72, 0x75, 0x63, 0x74, 0x2e, 0x70, 0x72, 0x6f,
0x74, 0x6f, 0x22, 0x80, 0x01, 0x0a, 0x0b, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x4d, 0x65, 0x6d, 0x62,
0x65, 0x72, 0x12, 0x0e, 0x0a, 0x02, 0x69, 0x64, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x02,
0x69, 0x64, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x69, 0x64, 0x18, 0x02,
0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x69, 0x64, 0x12, 0x18,
0x0a, 0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x69, 0x64, 0x18, 0x03, 0x20, 0x01, 0x28, 0x09, 0x52,
0x07, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x69, 0x64, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61,
0x18, 0x04, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x17, 0x2e, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x2e,
0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x75, 0x66, 0x2e, 0x53, 0x74, 0x72, 0x75, 0x63, 0x74, 0x52,
0x04, 0x64, 0x61, 0x74, 0x61, 0x42, 0x39, 0x5a, 0x37, 0x67, 0x69, 0x74, 0x2e, 0x63, 0x6f, 0x6f,
0x70, 0x67, 0x6f, 0x2e, 0x69, 0x6f, 0x2f, 0x63, 0x6f, 0x6f, 0x70, 0x67, 0x6f, 0x2d, 0x70, 0x6c,
0x61, 0x74, 0x66, 0x6f, 0x72, 0x6d, 0x2f, 0x67, 0x72, 0x6f, 0x75, 0x70, 0x73, 0x2d, 0x6d, 0x61,
0x6e, 0x61, 0x67, 0x65, 0x6d, 0x65, 0x6e, 0x74, 0x2f, 0x67, 0x72, 0x70, 0x63, 0x61, 0x70, 0x69,
0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33,
}
var (
file_groupsmember_proto_rawDescOnce sync.Once
file_groupsmember_proto_rawDescData = file_groupsmember_proto_rawDesc
)
func file_groupsmember_proto_rawDescGZIP() []byte {
file_groupsmember_proto_rawDescOnce.Do(func() {
file_groupsmember_proto_rawDescData = protoimpl.X.CompressGZIP(file_groupsmember_proto_rawDescData)
})
return file_groupsmember_proto_rawDescData
}
var file_groupsmember_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
var file_groupsmember_proto_goTypes = []interface{}{
(*GroupMember)(nil), // 0: GroupMember
(*_struct.Struct)(nil), // 1: google.protobuf.Struct
}
var file_groupsmember_proto_depIdxs = []int32{
1, // 0: GroupMember.data:type_name -> google.protobuf.Struct
1, // [1:1] is the sub-list for method output_type
1, // [1:1] is the sub-list for method input_type
1, // [1:1] is the sub-list for extension type_name
1, // [1:1] is the sub-list for extension extendee
0, // [0:1] is the sub-list for field type_name
}
func init() { file_groupsmember_proto_init() }
func file_groupsmember_proto_init() {
if File_groupsmember_proto != nil {
return
}
if !protoimpl.UnsafeEnabled {
file_groupsmember_proto_msgTypes[0].Exporter = func(v interface{}, i int) interface{} {
switch v := v.(*GroupMember); i {
case 0:
return &v.state
case 1:
return &v.sizeCache
case 2:
return &v.unknownFields
default:
return nil
}
}
}
type x struct{}
out := protoimpl.TypeBuilder{
File: protoimpl.DescBuilder{
GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
RawDescriptor: file_groupsmember_proto_rawDesc,
NumEnums: 0,
NumMessages: 1,
NumExtensions: 0,
NumServices: 0,
},
GoTypes: file_groupsmember_proto_goTypes,
DependencyIndexes: file_groupsmember_proto_depIdxs,
MessageInfos: file_groupsmember_proto_msgTypes,
}.Build()
File_groupsmember_proto = out.File
file_groupsmember_proto_rawDesc = nil
file_groupsmember_proto_goTypes = nil
file_groupsmember_proto_depIdxs = nil
}

View File

@ -0,0 +1,12 @@
syntax = "proto3";
option go_package = "git.coopgo.io/coopgo-platform/groups-management/grpcapi";
import "google/protobuf/struct.proto";
message GroupMember {
string id = 1;
string memberid = 2;
string groupid = 3;
google.protobuf.Struct data = 4;
}

View File

@ -94,8 +94,14 @@ func (s GroupsManagementServerImpl) Subscribe(ctx context.Context, req *Subscrib
}, nil }, nil
} }
func (s GroupsManagementServerImpl) Unsubscribe(context.Context, *UnsubscribeRequest) (*UnsubscribeResponse, error) { func (s GroupsManagementServerImpl) Unsubscribe(ctx context.Context, req *UnsubscribeRequest) (*UnsubscribeResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented") err := s.handler.Unsubscribe(req.Groupid, req.Memberid)
if err != nil {
return nil, status.Errorf(codes.AlreadyExists, "could not subscribe : %v", err)
}
return &UnsubscribeResponse{
Ok: true,
}, nil
} }
func (s GroupsManagementServerImpl) mustEmbedUnimplementedGroupsManagementServer() {} func (s GroupsManagementServerImpl) mustEmbedUnimplementedGroupsManagementServer() {}
@ -122,3 +128,90 @@ func Run(done chan error, cfg *viper.Viper, handler handlers.GroupsManagementHan
done <- err done <- err
} }
} }
////////////////////////////////////Code for the new DB groupMember//////////////////////////////////////
func (s GroupsManagementServerImpl) AddGroupMember(ctx context.Context, req *AddGroupMemberRequest) (*AddGroupMemberResponse, error) {
g := req.Group.ToStorageType()
group, err := s.handler.AddGroupMember(g)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "group creation failed : %v", err)
}
response, err := GroupFromStorageTypeMember(group)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
}
return &AddGroupMemberResponse{Group: response}, nil
}
func (s GroupsManagementServerImpl) GetGroupMember(ctx context.Context, req *GetGroupMemberRequest) (*GetGroupMemberResponse, error) {
group, err := s.handler.GetGroupMember(req.Id)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "issue while retrieving group : %v", err)
}
response, err := GroupFromStorageTypeMember(group)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
}
return &GetGroupMemberResponse{Group: response}, nil
}
func (s GroupsManagementServerImpl) GetGroupsMember(ctx context.Context, req *GetGroupsMemberRequest) (*GetGroupsMemberResponse, error) {
responses, err := s.handler.GetGroupsMember(req.Namespaces)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
}
var groups []*GroupMember
for _, g := range responses {
group, err := GroupFromStorageTypeMember(&g)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
}
groups = append(groups, group)
}
return &GetGroupsMemberResponse{Groups: groups}, nil
}
func (s GroupsManagementServerImpl) GetGroupsBatchMember(ctx context.Context, req *GetGroupsBatchMemberRequest) (*GetGroupsBatchMemberResponse, error) {
responses, err := s.handler.GetGroupsBatchMember(req.Groupids)
if err != nil {
return nil, status.Errorf(codes.NotFound, "could not get groups : %v", err)
}
var groups []*GroupMember
for _, g := range responses {
group, err := GroupFromStorageTypeMember(&g)
if err != nil {
return nil, status.Errorf(codes.Internal, "could not get groups : %v", err)
}
groups = append(groups, group)
}
return &GetGroupsBatchMemberResponse{Groups: groups}, nil
}
func (s GroupsManagementServerImpl) SubscribeMember(ctx context.Context, req *SubscribeMemberRequest) (*SubscribeMemberResponse, error) {
g := req.Group.ToStorageType()
group, err := s.handler.SubscribeMember(g)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.AlreadyExists, "group creation failed : %v", err)
}
response, err := GroupFromStorageTypeMember(group)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "issue while retrieving group : %v", err)
}
return &SubscribeMemberResponse{Group: response}, nil
}
func (s GroupsManagementServerImpl) UnsubscribeMember(ctx context.Context, req *UnsubscribeMemberRequest) (*UnsubscribeMemberResponse, error) {
err := s.handler.UnsubscribeMember(req.Id)
if err != nil {
return nil, status.Errorf(codes.AlreadyExists, "could not unsubscribe : %v", err)
}
return &UnsubscribeMemberResponse{}, nil
}

71
handlers/groupsmember.go Normal file
View File

@ -0,0 +1,71 @@
package handlers
import (
"git.coopgo.io/coopgo-platform/groups-management/storage"
"github.com/google/uuid"
)
func (h GroupsManagementHandler) AddGroupMember(group storage.GroupMember) (*storage.GroupMember, error) {
if group.ID == "" {
group.ID = uuid.NewString()
}
// Store the account
if err := h.storage.CreateGroupMember(group); err != nil {
return nil, err
}
return &group, nil
}
func (h GroupsManagementHandler) GetGroupMember(id string) (group *storage.GroupMember, err error) {
group, err = h.storage.GetGroupMember(id)
return
}
func (h GroupsManagementHandler) GetGroupsMember(namespaces []string) (groups []storage.GroupMember, err error) {
groups, err = h.storage.GetGroupsMember(namespaces)
return
}
func (h GroupsManagementHandler) GetGroupsBatchMember(groupids []string) (groups []storage.GroupMember, err error) {
groups, err = h.storage.GetGroupsMemberByIds(groupids)
return
}
func (h GroupsManagementHandler) SubscribeMember(group storage.GroupMember) (*storage.GroupMember, error) {
groupp, err := h.storage.GetGroupMember(group.ID)
if err != nil {
groups := h.storage.CreateGroupMember(group)
if groups != nil {
return nil, err
} else {
h.storage.UpdateGroupMember(group)
return &group, nil
}
}
groupp.Memberid = group.Memberid
groupp.Groupid = group.Groupid
groupp.Data = group.Data
h.storage.UpdateGroupMember(*groupp)
return &group, nil
}
func (h GroupsManagementHandler) UnsubscribeMember(groupid string) error {
_, err := h.storage.GetGroupMember(groupid)
if err != nil {
return err
}
erreur := h.storage.DeleteGroupMember(groupid)
if erreur != nil {
return erreur
}
return nil
}

View File

@ -6,3 +6,11 @@ type Group struct {
Members []string `json:"members"` Members []string `json:"members"`
Data map[string]any `json:"data"` Data map[string]any `json:"data"`
} }
///////////////////code
type GroupMember struct {
ID string `json:"id" bson:"_id"`
Memberid string `json:"memberid"`
Groupid string `json:"grouid"`
Data map[string]any `json:"data"`
}

View File

@ -23,6 +23,8 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
mongodb_port = cfg.GetString("storage.db.mongodb.port") mongodb_port = cfg.GetString("storage.db.mongodb.port")
mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name") mongodb_dbname = cfg.GetString("storage.db.mongodb.db_name")
mongodb_groups = cfg.GetString("storage.db.mongodb.collections.groups") mongodb_groups = cfg.GetString("storage.db.mongodb.collections.groups")
////////////////////////code
mongodb_groups_members = cfg.GetString("storage.db.mongodb.collections.groups_member")
) )
client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port)) client, err := mongo.NewClient(options.Client().ApplyURI("mongodb://" + mongodb_host + ":" + mongodb_port))
@ -41,6 +43,8 @@ func NewMongoDBStorage(cfg *viper.Viper) (MongoDBStorage, error) {
DbName: mongodb_dbname, DbName: mongodb_dbname,
Collections: map[string]string{ Collections: map[string]string{
"groups": mongodb_groups, "groups": mongodb_groups,
/////////////////////////////code
"groups_member": mongodb_groups_members,
}, },
} }
//TODO Indexes //TODO Indexes
@ -147,3 +151,123 @@ func (s MongoDBStorage) UpdateGroup(group Group) error {
return nil return nil
} }
/*********************************************************************************************************************************/
/*********************************************************Code********************************************************************/
/*********************************************************************************************************************************/
/*********************************************************************************************************************************/
func (s MongoDBStorage) CreateGroupMember(groupMember GroupMember) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
if _, err := collection.InsertOne(context.TODO(), groupMember); err != nil {
return err
}
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"])
groupMember := &GroupMember{}
if err := collection.FindOne(context.TODO(), bson.M{"_id": id}).Decode(groupMember); err != nil {
return nil, err
}
return groupMember, nil
}
func (s MongoDBStorage) GetGroupsMember(namespaces []string) (groupsMember []GroupMember, err error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
var cur *mongo.Cursor
findOptions := options.Find()
cur, err = collection.Find(context.TODO(), bson.D{}, 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
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return groupsMember, err
}
bsonBytes, _ := bson.Marshal(elem)
bson.Unmarshal(bsonBytes, &group)
groupsMember = append(groupsMember, group)
}
return
}
func (s MongoDBStorage) GetGroupsMemberByIds(groupids []string) (groupsMember []GroupMember, err error) {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
var cur *mongo.Cursor
findOptions := options.Find()
if len(groupids) == 0 {
return groupsMember, errors.New("no group id provided")
} else {
cur, err = collection.Find(context.TODO(), bson.M{"groupid": bson.M{"$in": groupids}}, findOptions)
if err != nil {
return groupsMember, err
}
}
for cur.Next(context.TODO()) {
var group GroupMember
var elem bson.M
if err := cur.Decode(&elem); err != nil {
return groupsMember, err
}
bsonBytes, _ := bson.Marshal(elem)
bson.Unmarshal(bsonBytes, &group)
groupsMember = append(groupsMember, group)
}
return
}
func (s MongoDBStorage) UpdateGroupMember(group GroupMember) error {
collection := s.Client.Database(s.DbName).Collection(s.Collections["groups_member"])
if _, err := collection.ReplaceOne(context.TODO(), bson.M{"_id": group.ID}, group); err != nil {
fmt.Println(err)
return err
}
return nil
}

View File

@ -12,6 +12,13 @@ type Storage interface {
GetGroups([]string) ([]Group, error) GetGroups([]string) ([]Group, error)
GetGroupsByIds([]string) ([]Group, error) GetGroupsByIds([]string) ([]Group, error)
UpdateGroup(Group) error UpdateGroup(Group) error
/*******************************************************/
CreateGroupMember(GroupMember) error
GetGroupMember(string) (*GroupMember, error)
GetGroupsMember([]string) ([]GroupMember, error)
GetGroupsMemberByIds([]string) ([]GroupMember, error)
UpdateGroupMember(GroupMember) error
DeleteGroupMember(string) error
} }
func NewStorage(cfg *viper.Viper) (Storage, error) { func NewStorage(cfg *viper.Viper) (Storage, error) {