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.

191 lines
4.3 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. )
  8. // ChannelResolver for the Channel graphql type
  9. type ChannelResolver struct{ C channel.Channel }
  10. // ChannelArgs is args for channel query
  11. type ChannelArgs struct {
  12. Name string
  13. }
  14. // Channel implements the channel query
  15. func (r *QueryResolver) Channel(ctx context.Context, args *ChannelArgs) (*ChannelResolver, error) {
  16. channel, err := channel.FindName(args.Name)
  17. if err != nil {
  18. return nil, err
  19. }
  20. return &ChannelResolver{C: channel}, nil
  21. }
  22. // ChannelsArgs is args for channel query
  23. type ChannelsArgs struct {
  24. Logged *bool
  25. }
  26. // Channels implements the channels query
  27. func (r *QueryResolver) Channels(ctx context.Context, args *ChannelsArgs) ([]*ChannelResolver, error) {
  28. channels, err := channel.List(args.Logged != nil && *args.Logged)
  29. if err != nil {
  30. return nil, err
  31. }
  32. resolvers := make([]*ChannelResolver, len(channels))
  33. for i := range channels {
  34. resolvers[i] = &ChannelResolver{C: channels[i]}
  35. }
  36. return resolvers, nil
  37. }
  38. // ChannelAddArgs is input for the addChannel mutation
  39. type ChannelAddArgs struct {
  40. Input *struct {
  41. Name string
  42. Logged *bool
  43. Hub *bool
  44. EventName *string
  45. LocationName *string
  46. }
  47. }
  48. // AddChannel resolves the addChannel mutation
  49. func (r *MutationResolver) AddChannel(ctx context.Context, args *ChannelAddArgs) (*ChannelResolver, error) {
  50. input := args.Input
  51. user := session.FromContext(ctx).User()
  52. if user == nil || !user.Permitted("channel.add") {
  53. return nil, ErrUnauthorized
  54. }
  55. logged := input.Logged != nil && *input.Logged
  56. hub := input.Hub != nil && *input.Hub
  57. eventName := ""
  58. if input.EventName != nil {
  59. eventName = *input.EventName
  60. }
  61. locationName := ""
  62. if input.LocationName != nil {
  63. locationName = *input.LocationName
  64. }
  65. channel, err := channel.New(input.Name, logged, hub, eventName, locationName)
  66. if err != nil {
  67. return nil, err
  68. }
  69. go change.Submit("Channel", "add", user.ID, channel.Name, map[string]interface{}{
  70. "logged": channel.Logged,
  71. "hub": channel.Hub,
  72. "location": input.LocationName,
  73. "event": input.EventName,
  74. })
  75. return &ChannelResolver{C: channel}, nil
  76. }
  77. // ChannelEditArgs is input for the editChannel mutation
  78. type ChannelEditArgs struct {
  79. Input *struct {
  80. Name string
  81. Logged *bool
  82. Hub *bool
  83. EventName *string
  84. LocationName *string
  85. }
  86. }
  87. // EditChannel resolves the editChannel mutation
  88. func (r *MutationResolver) EditChannel(ctx context.Context, args *ChannelEditArgs) (*ChannelResolver, error) {
  89. input := args.Input
  90. user := session.FromContext(ctx).User()
  91. if user == nil || !user.Permitted("channel.edit") {
  92. return nil, ErrUnauthorized
  93. }
  94. channel, err := channel.FindName(input.Name)
  95. if err != nil {
  96. return nil, err
  97. }
  98. err = channel.Edit(input.Logged, input.Hub, input.EventName, input.LocationName)
  99. if err != nil {
  100. return nil, err
  101. }
  102. go change.Submit("Channel", "edit", user.ID, channel.Name, map[string]interface{}{
  103. "logged": input.Logged,
  104. "hub": input.Hub,
  105. "location": input.LocationName,
  106. "event": input.EventName,
  107. })
  108. return &ChannelResolver{C: channel}, nil
  109. }
  110. // RemoveChannel resolves the editChannel mutation
  111. func (r *MutationResolver) RemoveChannel(ctx context.Context, args ChannelArgs) (*ChannelResolver, error) {
  112. user := session.FromContext(ctx).User()
  113. if user == nil || !user.Permitted("channel.remove") {
  114. return nil, ErrUnauthorized
  115. }
  116. channel, err := channel.FindName(args.Name)
  117. if err != nil {
  118. return nil, err
  119. }
  120. err = channel.Remove()
  121. if err != nil {
  122. return nil, err
  123. }
  124. go change.Submit("Channel", "remove", user.ID, channel.Name, nil)
  125. return &ChannelResolver{C: channel}, nil
  126. }
  127. // Name resolves channel.name
  128. func (r *ChannelResolver) Name() string {
  129. return r.C.Name
  130. }
  131. // Logged resolves channel.logged
  132. func (r *ChannelResolver) Logged() bool {
  133. return r.C.Logged
  134. }
  135. // Hub resolves channel.hub
  136. func (r *ChannelResolver) Hub() bool {
  137. return r.C.Hub
  138. }
  139. // EventName resolves channel.eventName
  140. func (r *ChannelResolver) EventName() *string {
  141. if r.C.Event == "" {
  142. return nil
  143. }
  144. return &r.C.Event
  145. }
  146. // LocationName resolves channel.locationName
  147. func (r *ChannelResolver) LocationName() *string {
  148. if r.C.Location == "" {
  149. return nil
  150. }
  151. return &r.C.Location
  152. }