mirror of
https://github.com/mainflux/mainflux.git
synced 2025-04-27 13:48:49 +08:00

* Add Event Sourcing on Users Service Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Modify Events Sourced From Things Service Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Rename Cache Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Remove SharedBy From Events Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Use Combined Publisher Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Format Docker Compose File Signed-off-by: rodneyosodo <blackd0t@protonmail.com> * Rename Events Signed-off-by: rodneyosodo <blackd0t@protonmail.com> --------- Signed-off-by: rodneyosodo <blackd0t@protonmail.com>
156 lines
3.3 KiB
Go
156 lines
3.3 KiB
Go
// Copyright (c) Mainflux
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package redis
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/go-redis/redis/v8"
|
|
mfredis "github.com/mainflux/mainflux/internal/clients/redis"
|
|
mfgroups "github.com/mainflux/mainflux/pkg/groups"
|
|
"github.com/mainflux/mainflux/users/groups"
|
|
)
|
|
|
|
const (
|
|
streamID = "mainflux.users"
|
|
streamLen = 1000
|
|
)
|
|
|
|
var _ groups.Service = (*eventStore)(nil)
|
|
|
|
type eventStore struct {
|
|
mfredis.Publisher
|
|
svc groups.Service
|
|
client *redis.Client
|
|
}
|
|
|
|
// NewEventStoreMiddleware returns wrapper around things service that sends
|
|
// events to event store.
|
|
func NewEventStoreMiddleware(ctx context.Context, svc groups.Service, client *redis.Client) groups.Service {
|
|
es := eventStore{
|
|
svc: svc,
|
|
client: client,
|
|
Publisher: mfredis.NewEventStore(client, streamID, streamLen),
|
|
}
|
|
|
|
go es.StartPublishingRoutine(ctx)
|
|
|
|
return es
|
|
}
|
|
|
|
func (es eventStore) CreateGroup(ctx context.Context, token string, group mfgroups.Group) (mfgroups.Group, error) {
|
|
group, err := es.svc.CreateGroup(ctx, token, group)
|
|
if err != nil {
|
|
return group, err
|
|
}
|
|
|
|
event := createGroupEvent{
|
|
group,
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return group, nil
|
|
}
|
|
|
|
func (es eventStore) UpdateGroup(ctx context.Context, token string, group mfgroups.Group) (mfgroups.Group, error) {
|
|
group, err := es.svc.UpdateGroup(ctx, token, group)
|
|
if err != nil {
|
|
return group, err
|
|
}
|
|
|
|
event := updateGroupEvent{
|
|
group,
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return group, nil
|
|
}
|
|
|
|
func (es eventStore) ViewGroup(ctx context.Context, token, id string) (mfgroups.Group, error) {
|
|
group, err := es.svc.ViewGroup(ctx, token, id)
|
|
if err != nil {
|
|
return group, err
|
|
}
|
|
event := viewGroupEvent{
|
|
group,
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return group, nil
|
|
}
|
|
|
|
func (es eventStore) ListGroups(ctx context.Context, token string, pm mfgroups.GroupsPage) (mfgroups.GroupsPage, error) {
|
|
gp, err := es.svc.ListGroups(ctx, token, pm)
|
|
if err != nil {
|
|
return gp, err
|
|
}
|
|
event := listGroupEvent{
|
|
pm,
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return gp, err
|
|
}
|
|
|
|
return gp, nil
|
|
}
|
|
|
|
func (es eventStore) ListMemberships(ctx context.Context, token, clientID string, pm mfgroups.GroupsPage) (mfgroups.MembershipsPage, error) {
|
|
mp, err := es.svc.ListMemberships(ctx, token, clientID, pm)
|
|
if err != nil {
|
|
return mp, err
|
|
}
|
|
event := listGroupMembershipEvent{
|
|
pm, clientID,
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return mp, err
|
|
}
|
|
|
|
return mp, nil
|
|
}
|
|
|
|
func (es eventStore) EnableGroup(ctx context.Context, token, id string) (mfgroups.Group, error) {
|
|
group, err := es.svc.EnableGroup(ctx, token, id)
|
|
if err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return es.delete(ctx, group)
|
|
}
|
|
|
|
func (es eventStore) DisableGroup(ctx context.Context, token, id string) (mfgroups.Group, error) {
|
|
group, err := es.svc.DisableGroup(ctx, token, id)
|
|
if err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return es.delete(ctx, group)
|
|
}
|
|
|
|
func (es eventStore) delete(ctx context.Context, group mfgroups.Group) (mfgroups.Group, error) {
|
|
event := removeGroupEvent{
|
|
id: group.ID,
|
|
updatedAt: group.UpdatedAt,
|
|
updatedBy: group.UpdatedBy,
|
|
status: group.Status.String(),
|
|
}
|
|
|
|
if err := es.Publish(ctx, event); err != nil {
|
|
return group, err
|
|
}
|
|
|
|
return group, nil
|
|
}
|