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.

78 lines
2.1 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/changes"
  9. "git.aiterp.net/rpdata/api/models/channels"
  10. )
  11. // Queries
  12. func (r *resolver) Channel(ctx context.Context, name string) (models.Channel, error) {
  13. return channels.FindName(name)
  14. }
  15. func (r *resolver) Channels(ctx context.Context, filter *channels.Filter) ([]models.Channel, error) {
  16. return channels.List(filter)
  17. }
  18. // Mutations
  19. func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (models.Channel, error) {
  20. token := auth.TokenFromContext(ctx)
  21. if !token.Authenticated() || !token.Permitted("channel.add") {
  22. return models.Channel{}, errors.New("You are not permitted to add channels")
  23. }
  24. logged := false
  25. if input.Logged != nil {
  26. logged = *input.Logged
  27. }
  28. hub := false
  29. if input.Hub != nil {
  30. hub = *input.Hub
  31. }
  32. eventName := ""
  33. if input.EventName != nil {
  34. eventName = *input.EventName
  35. }
  36. locationName := ""
  37. if input.LocationName != nil {
  38. locationName = *input.LocationName
  39. }
  40. channel, err := channels.Add(input.Name, logged, hub, eventName, locationName)
  41. if err != nil {
  42. return models.Channel{}, errors.New("Failed to add channel: " + err.Error())
  43. }
  44. go changes.Submit("Channel", "add", token.UserID, channel)
  45. return channel, nil
  46. }
  47. func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (models.Channel, error) {
  48. token := auth.TokenFromContext(ctx)
  49. if !token.Authenticated() || !token.Permitted("channel.edit") {
  50. return models.Channel{}, errors.New("You are not permitted to edit channels")
  51. }
  52. channel, err := channels.FindName(input.Name)
  53. if err != nil {
  54. return models.Channel{}, errors.New("Channel not found")
  55. }
  56. channel, err = channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName)
  57. if err != nil {
  58. return models.Channel{}, errors.New("Failed to edit channel: " + err.Error())
  59. }
  60. go changes.Submit("Channel", "edit", token.UserID, channel)
  61. return channel, nil
  62. }