add code grpc to manage new collection db groupMember

This commit is contained in:
soukainna 2022-12-19 09:14:31 +01:00
parent daf39bc067
commit 57540f2587
4 changed files with 325 additions and 0 deletions

64
grpcapi/groupsmember.go Normal file
View File

@ -0,0 +1,64 @@
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.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{
Memberid: group.ID,
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
}

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

@ -0,0 +1,170 @@
// 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
Memberid string `protobuf:"bytes,1,opt,name=memberid,proto3" json:"memberid,omitempty"`
Groupid string `protobuf:"bytes,2,opt,name=groupid,proto3" json:"groupid,omitempty"`
Data *_struct.Struct `protobuf:"bytes,3,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) 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, 0x70, 0x0a, 0x0b, 0x47, 0x72, 0x6f, 0x75, 0x70, 0x4d, 0x65, 0x6d, 0x62, 0x65,
0x72, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x6d, 0x62, 0x65, 0x72, 0x69, 0x64, 0x18, 0x01, 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, 0x02, 0x20, 0x01, 0x28, 0x09, 0x52, 0x07,
0x67, 0x72, 0x6f, 0x75, 0x70, 0x69, 0x64, 0x12, 0x2b, 0x0a, 0x04, 0x64, 0x61, 0x74, 0x61, 0x18,
0x03, 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,11 @@
syntax = "proto3";
option go_package = "git.coopgo.io/coopgo-platform/groups-management/grpcapi";
import "google/protobuf/struct.proto";
message GroupMember {
string memberid = 1;
string groupid = 2;
google.protobuf.Struct data = 3;
}

80
handlers/groupsmember.go Normal file
View File

@ -0,0 +1,80 @@
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 {
//return err
groups := h.storage.CreateGroupMember(group)
if groups != nil {
return nil, err
} else {
h.storage.UpdateGroupMember(group)
return &group, nil
}
}
groupp.Groupid = group.Groupid
groupp.Data = group.Data
h.storage.UpdateGroupMember(*groupp)
// // return nil
// // if group.Memberid == "" {
// // group.Memberid = uuid.NewString()
// // }
// Store the account
// if err := h.storage.CreateGroupMember(group); err != nil {
// return nil, err
// }
return &group, nil
}
// func (h GroupsManagementHandler) UnsubscribeMember(groupid string, subscriber string) error {
// group, err := h.storage.GetGroupMember(groupid)
// if err != nil {
// return err
// }
// members := []string{}
// for _, m := range group.Members {
// if m != subscriber {
// members = append(members, m)
// }
// }
// group.Members = members
// h.storage.UpdateGroupMember(*group)
// return nil
// }