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.

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