groups-management/handlers/groups.go

83 lines
1.8 KiB
Go

package handlers
import (
"errors"
"git.coopgo.io/coopgo-platform/groups-management/storage"
"github.com/google/uuid"
)
func (h GroupsManagementHandler) AddGroup(group storage.Group) (*storage.Group, error) {
if group.ID == "" {
group.ID = uuid.NewString()
}
// Store the account
if err := h.storage.CreateGroup(group); err != nil {
return nil, err
}
return &group, nil
}
func (h GroupsManagementHandler) GetGroup(id string) (group *storage.Group, err error) {
group, err = h.storage.GetGroup(id)
return
}
func (h GroupsManagementHandler) GetGroups(namespaces []string, member string) (groups []storage.Group, err error) {
groups = []storage.Group{}
g, err := h.storage.GetGroups(namespaces)
if err != nil {
return
}
if member == "" {
return g, nil
}
for _, group := range g {
for _, m := range group.Members {
if m == member {
groups = append(groups, group)
break
}
}
}
return
}
func (h GroupsManagementHandler) GetGroupsBatch(groupids []string) (groups []storage.Group, err error) {
groups, err = h.storage.GetGroupsByIds(groupids)
return
}
func (h GroupsManagementHandler) Subscribe(groupid string, subscriber string) error {
group, err := h.storage.GetGroup(groupid)
if err != nil {
return err
}
for _, m := range group.Members {
if m == subscriber {
return errors.New("already subscribed")
}
}
group.Members = append(group.Members, subscriber)
h.storage.UpdateGroup(*group)
return nil
}
func (h GroupsManagementHandler) Unsubscribe(groupid string, subscriber string) error {
group, err := h.storage.GetGroup(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.UpdateGroup(*group)
return nil
}