GraphQL API and utilities for the rpdata project
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

  1. package services
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/models"
  5. "git.aiterp.net/rpdata/api/models/changekeys"
  6. "git.aiterp.net/rpdata/api/repositories"
  7. "git.aiterp.net/rpdata/api/services/loaders"
  8. )
  9. // ChannelService is an application service for dealing with channels.
  10. type ChannelService struct {
  11. channels repositories.ChannelRepository
  12. loader *loaders.ChannelLoader
  13. changeService *ChangeService
  14. authService *AuthService
  15. }
  16. func (s *ChannelService) Find(ctx context.Context, id string) (*models.Channel, error) {
  17. return s.loader.Load(id)
  18. }
  19. func (s *ChannelService) List(ctx context.Context, filter models.ChannelFilter) ([]*models.Channel, error) {
  20. if filter.LocationName != nil && filter.EventName != nil && filter.Logged != nil && filter.Limit == 0 && len(filter.Names) > 0 {
  21. channels, errs := s.loader.LoadAll(filter.Names)
  22. for _, err := range errs {
  23. if err != nil && err != repositories.ErrNotFound {
  24. return nil, err
  25. }
  26. }
  27. output := make([]*models.Channel, 0, len(channels))
  28. for _, channel := range channels {
  29. if channel == nil {
  30. continue
  31. }
  32. output = append(output, channel)
  33. }
  34. return output, nil
  35. }
  36. return s.channels.List(ctx, filter)
  37. }
  38. func (s *ChannelService) Create(ctx context.Context, name string, logged, hub bool, eventName, locationName string) (*models.Channel, error) {
  39. err := s.authService.CheckPermission(ctx, "add", &models.Channel{})
  40. if err != nil {
  41. return nil, err
  42. }
  43. channel, err := s.channels.Insert(ctx, models.Channel{
  44. Name: name,
  45. Logged: logged,
  46. Hub: hub,
  47. EventName: eventName,
  48. LocationName: locationName,
  49. })
  50. if err != nil {
  51. return nil, err
  52. }
  53. s.changeService.Submit(ctx, "Channel", "add", true, changekeys.Listed(channel), channel)
  54. return channel, nil
  55. }
  56. func (s *ChannelService) Ensure(ctx context.Context, name string) (*models.Channel, error) {
  57. channel, err := s.channels.Find(ctx, name)
  58. if err != nil {
  59. channel, err = s.channels.Insert(ctx, models.Channel{
  60. Name: name,
  61. Logged: false,
  62. Hub: false,
  63. EventName: "",
  64. LocationName: "",
  65. })
  66. if err != nil {
  67. return nil, err
  68. }
  69. s.changeService.Submit(ctx, "Channel", "add", true, changekeys.Listed(channel), channel)
  70. }
  71. return channel, nil
  72. }
  73. func (s *ChannelService) Update(ctx context.Context, name string, update models.ChannelUpdate) (*models.Channel, error) {
  74. channel, err := s.channels.Find(ctx, name)
  75. if err != nil {
  76. return nil, err
  77. }
  78. err = s.authService.CheckPermission(ctx, "edit", channel)
  79. if err != nil {
  80. return nil, err
  81. }
  82. channel, err = s.channels.Update(ctx, *channel, update)
  83. if err != nil {
  84. return nil, err
  85. }
  86. s.loader.Clear(channel.Name)
  87. s.changeService.Submit(ctx, "Channel", "edit", true, changekeys.Listed(channel), channel)
  88. return channel, nil
  89. }
  90. func (s *ChannelService) Delete(ctx context.Context, name string) (*models.Channel, error) {
  91. channel, err := s.channels.Find(ctx, name)
  92. if err != nil {
  93. return nil, err
  94. }
  95. err = s.authService.CheckPermission(ctx, "remove", channel)
  96. if err != nil {
  97. return nil, err
  98. }
  99. err = s.channels.Remove(ctx, *channel)
  100. if err != nil {
  101. return nil, err
  102. }
  103. s.changeService.Submit(ctx, "Channel", "remove", true, changekeys.Listed(channel), channel)
  104. s.loader.Clear(channel.Name)
  105. return channel, nil
  106. }