package resolver import ( "context" "git.aiterp.net/rpdata/api/internal/session" "git.aiterp.net/rpdata/api/model/channel" ) // ChannelResolver for the Channel graphql type type ChannelResolver struct{ C channel.Channel } // ChannelArgs is args for channel query type ChannelArgs struct { Name string } // Channel implements the channel query func (r *QueryResolver) Channel(ctx context.Context, args *ChannelArgs) (*ChannelResolver, error) { channel, err := channel.FindName(args.Name) if err != nil { return nil, err } return &ChannelResolver{C: channel}, nil } // ChannelsArgs is args for channel query type ChannelsArgs struct { Logged *bool } // Channels implements the channels query func (r *QueryResolver) Channels(ctx context.Context, args *ChannelsArgs) ([]*ChannelResolver, error) { channels, err := channel.List(args.Logged != nil && *args.Logged) if err != nil { return nil, err } resolvers := make([]*ChannelResolver, len(channels)) for i := range channels { resolvers[i] = &ChannelResolver{C: channels[i]} } return resolvers, nil } // ChannelAddEditInput is input for the addChannel mutation type ChannelAddEditInput struct { Name string Logged *bool Hub *bool EventName *string LocationName *string } // AddChannel resolves the addChannel mutation func (r *MutationResolver) AddChannel(ctx context.Context, args struct{ Input *ChannelAddEditInput }) (*ChannelResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("channel.add") { return nil, ErrUnauthorized } logged := args.Input.Logged != nil && *args.Input.Logged hub := args.Input.Hub != nil && *args.Input.Hub eventName := "" if args.Input.EventName != nil { eventName = *args.Input.EventName } locationName := "" if args.Input.LocationName != nil { locationName = *args.Input.LocationName } channel, err := channel.New(args.Input.Name, logged, hub, eventName, locationName) if err != nil { return nil, err } return &ChannelResolver{C: channel}, nil } // EditChannel resolves the editChannel mutation func (r *MutationResolver) EditChannel(ctx context.Context, args struct{ Input *ChannelAddEditInput }) (*ChannelResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("channel.edit") { return nil, ErrUnauthorized } channel, err := channel.FindName(args.Input.Name) if err != nil { return nil, err } err = channel.Edit(args.Input.Logged, args.Input.Hub, args.Input.EventName, args.Input.LocationName) if err != nil { return nil, err } return &ChannelResolver{C: channel}, nil } // RemoveChannel resolves the editChannel mutation func (r *MutationResolver) RemoveChannel(ctx context.Context, args ChannelArgs) (*ChannelResolver, error) { user := session.FromContext(ctx).User() if user == nil || !user.Permitted("channel.remove") { return nil, ErrUnauthorized } channel, err := channel.FindName(args.Name) if err != nil { return nil, err } err = channel.Remove() if err != nil { return nil, err } return &ChannelResolver{C: channel}, nil } // Name resolves channel.name func (r *ChannelResolver) Name() string { return r.C.Name } // Logged resolves channel.logged func (r *ChannelResolver) Logged() bool { return r.C.Logged } // Hub resolves channel.hub func (r *ChannelResolver) Hub() bool { return r.C.Hub } // EventName resolves channel.eventName func (r *ChannelResolver) EventName() *string { if r.C.Event == "" { return nil } return &r.C.Event } // LocationName resolves channel.locationName func (r *ChannelResolver) LocationName() *string { if r.C.Location == "" { return nil } return &r.C.Location }