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.

94 lines
2.4 KiB

  1. package resolvers
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graph2/graphcore"
  6. "git.aiterp.net/rpdata/api/internal/auth"
  7. "git.aiterp.net/rpdata/api/models"
  8. "git.aiterp.net/rpdata/api/models/changekeys"
  9. "git.aiterp.net/rpdata/api/models/changes"
  10. "git.aiterp.net/rpdata/api/models/channels"
  11. )
  12. // Queries
  13. func (r *queryResolver) Channel(ctx context.Context, name string) (*models.Channel, error) {
  14. channel, err := channels.FindName(name)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return &channel, nil
  19. }
  20. func (r *queryResolver) Channels(ctx context.Context, filter *channels.Filter) ([]*models.Channel, error) {
  21. channels, err := channels.List(filter)
  22. if err != nil {
  23. return nil, err
  24. }
  25. channels2 := make([]*models.Channel, len(channels))
  26. for i := range channels {
  27. channels2[i] = &channels[i]
  28. }
  29. return channels2, nil
  30. }
  31. // Mutations
  32. func (r *mutationResolver) AddChannel(ctx context.Context, input graphcore.ChannelAddInput) (*models.Channel, error) {
  33. token := auth.TokenFromContext(ctx)
  34. if !token.Authenticated() || !token.Permitted("channel.add") {
  35. return nil, errors.New("You are not permitted to add channels")
  36. }
  37. logged := false
  38. if input.Logged != nil {
  39. logged = *input.Logged
  40. }
  41. hub := false
  42. if input.Hub != nil {
  43. hub = *input.Hub
  44. }
  45. eventName := ""
  46. if input.EventName != nil {
  47. eventName = *input.EventName
  48. }
  49. locationName := ""
  50. if input.LocationName != nil {
  51. locationName = *input.LocationName
  52. }
  53. channel, err := channels.Add(input.Name, logged, hub, eventName, locationName)
  54. if err != nil {
  55. return nil, errors.New("Failed to add channel: " + err.Error())
  56. }
  57. go changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel)
  58. return &channel, nil
  59. }
  60. func (r *mutationResolver) EditChannel(ctx context.Context, input graphcore.ChannelEditInput) (*models.Channel, error) {
  61. token := auth.TokenFromContext(ctx)
  62. if !token.Authenticated() || !token.Permitted("channel.edit") {
  63. return nil, errors.New("You are not permitted to edit channels")
  64. }
  65. channel, err := channels.FindName(input.Name)
  66. if err != nil {
  67. return nil, errors.New("Channel not found")
  68. }
  69. channel, err = channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName)
  70. if err != nil {
  71. return nil, errors.New("Failed to edit channel: " + err.Error())
  72. }
  73. go changes.Submit("Channel", "edit", token.UserID, true, changekeys.Listed(channel), channel)
  74. return &channel, nil
  75. }