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.

137 lines
3.3 KiB

  1. package services
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/auth"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/models/changekeys"
  7. "git.aiterp.net/rpdata/api/repositories"
  8. "git.aiterp.net/rpdata/api/services/loaders"
  9. )
  10. // ChannelService is an application service for dealing with channels.
  11. type ChannelService struct {
  12. channels repositories.ChannelRepository
  13. loader *loaders.ChannelLoader
  14. changeService *ChangeService
  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 := auth.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. err := auth.CheckPermission(ctx, "add", &models.Channel{})
  58. if err != nil {
  59. return nil, err
  60. }
  61. channel, err := s.channels.Find(ctx, name)
  62. if err != nil {
  63. channel, err = s.channels.Insert(ctx, models.Channel{
  64. Name: name,
  65. Logged: false,
  66. Hub: false,
  67. EventName: "",
  68. LocationName: "",
  69. })
  70. if err != nil {
  71. return nil, err
  72. }
  73. s.changeService.Submit(ctx, "Channel", "add", true, changekeys.Listed(channel), channel)
  74. }
  75. return channel, nil
  76. }
  77. func (s *ChannelService) Update(ctx context.Context, name string, update models.ChannelUpdate) (*models.Channel, error) {
  78. channel, err := s.channels.Find(ctx, name)
  79. if err != nil {
  80. return nil, err
  81. }
  82. err = auth.CheckPermission(ctx, "edit", channel)
  83. if err != nil {
  84. return nil, err
  85. }
  86. channel, err = s.channels.Update(ctx, *channel, update)
  87. if err != nil {
  88. return nil, err
  89. }
  90. s.loader.Clear(channel.Name)
  91. s.changeService.Submit(ctx, "Channel", "edit", true, changekeys.Listed(channel), channel)
  92. return channel, nil
  93. }
  94. func (s *ChannelService) Delete(ctx context.Context, name string) (*models.Channel, error) {
  95. channel, err := s.channels.Find(ctx, name)
  96. if err != nil {
  97. return nil, err
  98. }
  99. err = auth.CheckPermission(ctx, "remove", channel)
  100. if err != nil {
  101. return nil, err
  102. }
  103. err = s.channels.Remove(ctx, *channel)
  104. if err != nil {
  105. return nil, err
  106. }
  107. s.changeService.Submit(ctx, "Channel", "remove", true, changekeys.Listed(channel), channel)
  108. s.loader.Clear(channel.Name)
  109. return channel, nil
  110. }