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.

156 lines
3.7 KiB

  1. package resolver
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/session"
  5. "git.aiterp.net/rpdata/api/model/change"
  6. "git.aiterp.net/rpdata/api/model/channel"
  7. "git.aiterp.net/rpdata/api/resolver/types"
  8. )
  9. // ChannelArgs is args for channel query
  10. type ChannelArgs struct {
  11. Name string
  12. }
  13. // Channel implements the channel query
  14. func (r *QueryResolver) Channel(ctx context.Context, args *ChannelArgs) (*types.ChannelResolver, error) {
  15. channel, err := channel.FindName(args.Name)
  16. if err != nil {
  17. return nil, err
  18. }
  19. return &types.ChannelResolver{C: channel}, nil
  20. }
  21. // ChannelsArgs is args for channel query
  22. type ChannelsArgs struct {
  23. Logged *bool
  24. }
  25. // Channels implements the channels query
  26. func (r *QueryResolver) Channels(ctx context.Context, args *ChannelsArgs) ([]*types.ChannelResolver, error) {
  27. channels, err := channel.List(args.Logged != nil && *args.Logged)
  28. if err != nil {
  29. return nil, err
  30. }
  31. resolvers := make([]*types.ChannelResolver, len(channels))
  32. for i := range channels {
  33. resolvers[i] = &types.ChannelResolver{C: channels[i]}
  34. }
  35. return resolvers, nil
  36. }
  37. // ChannelAddArgs is input for the addChannel mutation
  38. type ChannelAddArgs struct {
  39. Input *struct {
  40. Name string
  41. Logged *bool
  42. Hub *bool
  43. EventName *string
  44. LocationName *string
  45. }
  46. }
  47. // AddChannel resolves the addChannel mutation
  48. func (r *MutationResolver) AddChannel(ctx context.Context, args *ChannelAddArgs) (*types.ChannelResolver, error) {
  49. input := args.Input
  50. user := session.FromContext(ctx).User()
  51. if user == nil || !user.Permitted("channel.add") {
  52. return nil, ErrUnauthorized
  53. }
  54. logged := input.Logged != nil && *input.Logged
  55. hub := input.Hub != nil && *input.Hub
  56. eventName := ""
  57. if input.EventName != nil {
  58. eventName = *input.EventName
  59. }
  60. locationName := ""
  61. if input.LocationName != nil {
  62. locationName = *input.LocationName
  63. }
  64. channel, err := channel.New(input.Name, logged, hub, eventName, locationName)
  65. if err != nil {
  66. return nil, err
  67. }
  68. go change.Submit("Channel", "add", user.ID, channel.Name, map[string]interface{}{
  69. "logged": channel.Logged,
  70. "hub": channel.Hub,
  71. "location": input.LocationName,
  72. "event": input.EventName,
  73. })
  74. return &types.ChannelResolver{C: channel}, nil
  75. }
  76. // ChannelEditArgs is input for the editChannel mutation
  77. type ChannelEditArgs struct {
  78. Input *struct {
  79. Name string
  80. Logged *bool
  81. Hub *bool
  82. EventName *string
  83. LocationName *string
  84. }
  85. }
  86. // EditChannel resolves the editChannel mutation
  87. func (r *MutationResolver) EditChannel(ctx context.Context, args *ChannelEditArgs) (*types.ChannelResolver, error) {
  88. input := args.Input
  89. user := session.FromContext(ctx).User()
  90. if user == nil || !user.Permitted("channel.edit") {
  91. return nil, ErrUnauthorized
  92. }
  93. channel, err := channel.FindName(input.Name)
  94. if err != nil {
  95. return nil, err
  96. }
  97. err = channel.Edit(input.Logged, input.Hub, input.EventName, input.LocationName)
  98. if err != nil {
  99. return nil, err
  100. }
  101. go change.Submit("Channel", "edit", user.ID, channel.Name, map[string]interface{}{
  102. "logged": input.Logged,
  103. "hub": input.Hub,
  104. "location": input.LocationName,
  105. "event": input.EventName,
  106. })
  107. return &types.ChannelResolver{C: channel}, nil
  108. }
  109. // RemoveChannel resolves the editChannel mutation
  110. func (r *MutationResolver) RemoveChannel(ctx context.Context, args ChannelArgs) (*types.ChannelResolver, error) {
  111. user := session.FromContext(ctx).User()
  112. if user == nil || !user.Permitted("channel.remove") {
  113. return nil, ErrUnauthorized
  114. }
  115. channel, err := channel.FindName(args.Name)
  116. if err != nil {
  117. return nil, err
  118. }
  119. err = channel.Remove()
  120. if err != nil {
  121. return nil, err
  122. }
  123. go change.Submit("Channel", "remove", user.ID, channel.Name, nil)
  124. return &types.ChannelResolver{C: channel}, nil
  125. }