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.

84 lines
2.2 KiB

  1. package queries
  2. import (
  3. "context"
  4. "errors"
  5. "git.aiterp.net/rpdata/api/graph2/input"
  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 *resolver) 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 *resolver) Channels(ctx context.Context, filter *channels.Filter) ([]models.Channel, error) {
  21. return channels.List(filter)
  22. }
  23. // Mutations
  24. func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (*models.Channel, error) {
  25. token := auth.TokenFromContext(ctx)
  26. if !token.Authenticated() || !token.Permitted("channel.add") {
  27. return nil, errors.New("You are not permitted to add channels")
  28. }
  29. logged := false
  30. if input.Logged != nil {
  31. logged = *input.Logged
  32. }
  33. hub := false
  34. if input.Hub != nil {
  35. hub = *input.Hub
  36. }
  37. eventName := ""
  38. if input.EventName != nil {
  39. eventName = *input.EventName
  40. }
  41. locationName := ""
  42. if input.LocationName != nil {
  43. locationName = *input.LocationName
  44. }
  45. channel, err := channels.Add(input.Name, logged, hub, eventName, locationName)
  46. if err != nil {
  47. return nil, errors.New("Failed to add channel: " + err.Error())
  48. }
  49. go changes.Submit("Channel", "add", token.UserID, true, changekeys.Listed(channel), channel)
  50. return &channel, nil
  51. }
  52. func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (*models.Channel, error) {
  53. token := auth.TokenFromContext(ctx)
  54. if !token.Authenticated() || !token.Permitted("channel.edit") {
  55. return nil, errors.New("You are not permitted to edit channels")
  56. }
  57. channel, err := channels.FindName(input.Name)
  58. if err != nil {
  59. return nil, errors.New("Channel not found")
  60. }
  61. channel, err = channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName)
  62. if err != nil {
  63. return nil, errors.New("Failed to edit channel: " + err.Error())
  64. }
  65. go changes.Submit("Channel", "edit", token.UserID, true, changekeys.Listed(channel), channel)
  66. return &channel, nil
  67. }