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.

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