agenda/handlers/events.go

94 lines
2.1 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) UnsubscribeEvent(eventid string, subscriber string) (err error) {
// if eventid == "" || subscriber == "" {
// return errors.New("missing eventid or subscriber")
// }
// event, err = h.storage.GetEvent(eventid)
// for i := 0; i < event.Subscriptions; i++ {
// }
// 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
}