68 lines
1.5 KiB
Go
68 lines
1.5 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) (groups []storage.Group, err error) {
|
||
|
groups, err = h.storage.GetGroups(namespaces)
|
||
|
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
|
||
|
}
|