[+] add function GetSubscriberByUser for the agenda-widget

This commit is contained in:
2023-09-06 13:34:43 +02:00
parent f06ed52547
commit be0835273e
16 changed files with 551 additions and 1556 deletions

View File

@@ -8,11 +8,14 @@ import (
"time"
"git.coopgo.io/coopgo-platform/agenda/handlers"
"git.coopgo.io/coopgo-platform/agenda/storage"
timestamp "github.com/golang/protobuf/ptypes/timestamp"
"github.com/spf13/viper"
"google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
"google.golang.org/grpc/reflection"
status "google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/structpb"
)
type AgendaServerImpl struct {
@@ -95,6 +98,76 @@ func (s AgendaServerImpl) UnsubscribeEvent(context.Context, *UnsubscribeEventReq
return nil, status.Errorf(codes.Unimplemented, "method Unsubscribe not implemented")
}
/////////////////////////////////////////////////////////////////////////////////////////
func ConvertStorageToProtoSubscription(storageSubs []*storage.Subscription) ([]*Subscription, error) {
var protoSubs []*Subscription
for _, storageSub := range storageSubs {
dataStruct, err := ConvertMapToStruct(storageSub.Data)
if err != nil {
return nil, err
}
protoSub := &Subscription{
Id: storageSub.ID,
Eventid: storageSub.EventID,
Subscriber: storageSub.Subscriber,
Tags: storageSub.Tags,
CreatedAt: &timestamp.Timestamp{Seconds: storageSub.CreatedAt.Unix()},
Data: dataStruct,
}
protoSubs = append(protoSubs, protoSub)
}
return protoSubs, nil
}
// ConvertMapToStruct converts a map[string]interface{} to a google.protobuf.Struct
func ConvertMapToStruct(dataMap map[string]interface{}) (*structpb.Struct, error) {
fields := make(map[string]*structpb.Value)
for key, value := range dataMap {
fieldValue, err := structpb.NewValue(value)
if err != nil {
return nil, err
}
fields[key] = fieldValue
}
return &structpb.Struct{
Fields: fields,
}, nil
}
func (s AgendaServerImpl) GetSubscriptionByUser(ctx context.Context, req *GetSubscriptionByUserRequest) (*GetSubscriptionByUserResponse, error) {
results, err := s.handler.GetSubscriptionByUser(req.Subscriber)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.NotFound, "could not get subscriptions : %v", err)
}
var subscriptions []*storage.Subscription
for _, result := range results {
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "could not get subscriptions : %v", err)
}
subscription := &storage.Subscription{
ID: result.ID,
EventID: result.EventID,
Subscriber: result.Subscriber,
Tags: result.Tags,
Data: result.Data,
CreatedAt: result.CreatedAt,
}
subscriptions = append(subscriptions, subscription)
}
sub, err := ConvertStorageToProtoSubscription(subscriptions)
if err != nil {
fmt.Println(err)
return nil, status.Errorf(codes.Internal, "could not get subscriptions : %v", err)
}
return &GetSubscriptionByUserResponse{Subscription: sub}, nil
}
////////////////////////////////////////////////////////////
func (s AgendaServerImpl) DeleteSubscription(ctx context.Context, req *DeleteSubscriptionRequest) (*DeleteSubscriptionResponse, error) {
err := s.handler.DeleteSubscription(req.Eventid, req.Subscriber, req.Data.AsMap())