agenda/handlers/events.go

83 lines
1.8 KiB
Go
Raw Normal View History

2022-09-05 05:27:52 +00:00
package handlers
import (
2022-12-05 16:21:12 +00:00
"errors"
2022-10-17 03:00:35 +00:00
"time"
2022-09-05 05:27:52 +00:00
"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()
}
2022-10-17 03:00:35 +00:00
if event.Subscriptions == nil {
event.Subscriptions = []storage.Subscription{}
2022-09-05 05:27:52 +00:00
}
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
}
2022-12-05 16:21:12 +00:00
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)
}
}
2022-09-05 05:27:52 +00:00
return
}
2022-12-05 16:21:12 +00:00
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()
2022-10-17 03:00:35 +00:00
subscription := storage.Subscription{
2022-12-05 16:21:12 +00:00
ID: id,
2022-10-17 03:00:35 +00:00
Subscriber: subscriber,
Tags: []string{},
2022-12-05 16:21:12 +00:00
CreatedAt: now,
Data: map[string]any{},
}
// Initiate data map
for k, v := range data {
subscription.Data[k] = v
2022-10-17 03:00:35 +00:00
}
err = h.storage.AddSubscription(eventid, subscription)
2022-09-05 05:27:52 +00:00
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
}