You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
132 lines
3.3 KiB
132 lines
3.3 KiB
package services
|
|
|
|
import (
|
|
"context"
|
|
"git.aiterp.net/rpdata/api/internal/auth"
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"git.aiterp.net/rpdata/api/models/changekeys"
|
|
"git.aiterp.net/rpdata/api/repositories"
|
|
"git.aiterp.net/rpdata/api/services/loaders"
|
|
)
|
|
|
|
// ChannelService is an application service for dealing with channels.
|
|
type ChannelService struct {
|
|
channels repositories.ChannelRepository
|
|
loader *loaders.ChannelLoader
|
|
changeService *ChangeService
|
|
}
|
|
|
|
func (s *ChannelService) Find(ctx context.Context, id string) (*models.Channel, error) {
|
|
return s.loader.Load(id)
|
|
}
|
|
|
|
func (s *ChannelService) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
|
|
if filter.LocationName != nil && filter.EventName != nil && filter.Logged != nil && filter.Limit == 0 && len(filter.Names) > 0 {
|
|
channels, errs := s.loader.LoadAll(filter.Names)
|
|
for _, err := range errs {
|
|
if err != nil && err != repositories.ErrNotFound {
|
|
return nil, err
|
|
}
|
|
}
|
|
|
|
output := make([]*models.Channel, 0, len(channels))
|
|
for _, channel := range channels {
|
|
if channel == nil {
|
|
continue
|
|
}
|
|
output = append(output, channel)
|
|
}
|
|
|
|
return output, nil
|
|
}
|
|
|
|
return s.channels.List(ctx, filter)
|
|
}
|
|
|
|
func (s *ChannelService) Create(ctx context.Context, name string, logged, hub bool, eventName, locationName string) (*models.Channel, error) {
|
|
err := auth.CheckPermission(ctx, "add", &models.Channel{})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err := s.channels.Insert(ctx, models.Channel{
|
|
Name: name,
|
|
Logged: logged,
|
|
Hub: hub,
|
|
EventName: eventName,
|
|
LocationName: locationName,
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.changeService.Submit(ctx, "Channel", "add", true, changekeys.Listed(channel), channel)
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
func (s *ChannelService) Ensure(ctx context.Context, name string) (*models.Channel, error) {
|
|
channel, err := s.channels.Find(ctx, name)
|
|
if err != nil {
|
|
channel, err = s.channels.Insert(ctx, models.Channel{
|
|
Name: name,
|
|
Logged: false,
|
|
Hub: false,
|
|
EventName: "",
|
|
LocationName: "",
|
|
})
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.changeService.Submit(ctx, "Channel", "add", true, changekeys.Listed(channel), channel)
|
|
}
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
func (s *ChannelService) Update(ctx context.Context, name string, update models.ChannelUpdate) (*models.Channel, error) {
|
|
channel, err := s.channels.Find(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = auth.CheckPermission(ctx, "edit", channel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
channel, err = s.channels.Update(ctx, *channel, update)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.loader.Clear(channel.Name)
|
|
|
|
s.changeService.Submit(ctx, "Channel", "edit", true, changekeys.Listed(channel), channel)
|
|
|
|
return channel, nil
|
|
}
|
|
|
|
func (s *ChannelService) Delete(ctx context.Context, name string) (*models.Channel, error) {
|
|
channel, err := s.channels.Find(ctx, name)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = auth.CheckPermission(ctx, "remove", channel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
err = s.channels.Remove(ctx, *channel)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
s.changeService.Submit(ctx, "Channel", "remove", true, changekeys.Listed(channel), channel)
|
|
|
|
s.loader.Clear(channel.Name)
|
|
|
|
return channel, nil
|
|
}
|