agenda/handlers/events.go

114 lines
2.7 KiB
Go

package handlers
import (
"errors"
"time"
"git.coopgo.io/coopgo-platform/agenda/storage"
"github.com/google/uuid"
)
func (h AgendaHandler) CreateEvent(event storage.Event) (*storage.Event, error) {
if event.ID == "" {
event.ID = uuid.NewString()
}
if event.Subscriptions == nil {
event.Subscriptions = []storage.Subscription{}
}
if err := h.storage.CreateEvent(event); err != nil {
return nil, err
}
return &event, nil
}
func (h AgendaHandler) GetEvent(id string) (event *storage.Event, err error) {
event, err = h.storage.GetEvent(id)
return
}
func (h AgendaHandler) GetEvents(namespaces []string, mindate *time.Time, maxdate *time.Time) (results []storage.Event, err error) {
results = []storage.Event{}
events, err := h.storage.GetEvents(namespaces)
if err == nil {
for _, event := range events {
if mindate != nil {
if event.Enddate.Add(24 * time.Hour).Before(*mindate) {
continue
}
}
if maxdate != nil {
if event.Enddate.Add(24 * time.Hour).After(*mindate) {
continue
}
}
results = append(results, event)
}
}
return
}
func (h AgendaHandler) SubscribeEvent(eventid string, subscriber string, data map[string]any) (err error) {
if eventid == "" || subscriber == "" {
return errors.New("missing eventid or subscriber")
}
now := time.Now()
id := uuid.NewString()
subscription := storage.Subscription{
ID: id,
Subscriber: subscriber,
Tags: []string{},
CreatedAt: now,
Data: map[string]any{},
}
// Initiate data map
for k, v := range data {
subscription.Data[k] = v
}
err = h.storage.AddSubscription(eventid, subscription)
return
}
func (h AgendaHandler) DeleteSubscription(eventid string, subscriber string, data map[string]any) (err error) {
if eventid == "" || subscriber == "" {
return errors.New("missing eventid or subscriber")
}
now := time.Now()
id := uuid.NewString()
deletesubscription := storage.Subscription{
ID: id,
Subscriber: subscriber,
Tags: []string{},
CreatedAt: now,
Data: map[string]any{},
}
// Initiate data map
for k, v := range data {
deletesubscription.Data[k] = v
}
err = h.storage.UpdateSubscription(eventid, subscriber, deletesubscription)
return
}
func (h AgendaHandler) UpdateEvent(event storage.Event) (*storage.Event, error) {
// Store the account
if err := h.storage.UpdateEvent(event); err != nil {
return nil, err
}
return &event, nil
}
func (h AgendaHandler) GetSubscriptionByUser(subscriber string) (results []storage.Subscription, err error) {
if subscriber == "" {
return nil, errors.New("missing subscriber")
}
results, err = h.storage.GetSubscriptionByUser(subscriber)
if err != nil {
return nil, err
}
return results, nil
}