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.

154 lines
3.6 KiB

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