From b8e5ed5813e580e02aaf06ad177ed25eea639548 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Mon, 1 Oct 2018 22:20:52 +0200 Subject: [PATCH] graph2: Added channel mutations. --- graph2/queries/channel.go | 47 +++++++++++++++++++++++++++++++++++++++ graph2/schema/root.gql | 7 ++++++ models/channels/edit.go | 35 +++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) create mode 100644 models/channels/edit.go diff --git a/graph2/queries/channel.go b/graph2/queries/channel.go index 77138a2..a9476af 100644 --- a/graph2/queries/channel.go +++ b/graph2/queries/channel.go @@ -2,11 +2,16 @@ package queries import ( "context" + "errors" + "git.aiterp.net/rpdata/api/graph2/input" + "git.aiterp.net/rpdata/api/internal/auth" "git.aiterp.net/rpdata/api/models" "git.aiterp.net/rpdata/api/models/channels" ) +// Queries + func (r *resolver) Channel(ctx context.Context, name string) (models.Channel, error) { return channels.FindName(name) } @@ -14,3 +19,45 @@ func (r *resolver) Channel(ctx context.Context, name string) (models.Channel, er func (r *resolver) Channels(ctx context.Context, filter *channels.Filter) ([]models.Channel, error) { return channels.List(filter) } + +// Mutations + +func (r *mutationResolver) AddChannel(ctx context.Context, input input.ChannelAddInput) (models.Channel, error) { + token := auth.TokenFromContext(ctx) + if !token.Authenticated() || !token.Permitted("channel.add") { + return models.Channel{}, errors.New("You are not permitted to add channels") + } + + logged := false + if input.Logged != nil { + logged = *input.Logged + } + hub := false + if input.Hub != nil { + hub = *input.Hub + } + eventName := "" + if input.EventName != nil { + eventName = *input.EventName + } + locationName := "" + if input.LocationName != nil { + locationName = *input.LocationName + } + + return channels.Add(input.Name, logged, hub, eventName, locationName) +} + +func (r *mutationResolver) EditChannel(ctx context.Context, input input.ChannelEditInput) (models.Channel, error) { + token := auth.TokenFromContext(ctx) + if !token.Authenticated() || !token.Permitted("channel.edit") { + return models.Channel{}, errors.New("You are not permitted to edit channels") + } + + channel, err := channels.FindName(input.Name) + if err != nil { + return models.Channel{}, errors.New("Channel not found") + } + + return channels.Edit(channel, input.Logged, input.Hub, input.EventName, input.LocationName) +} diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index 870aa32..0e8cebc 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -122,6 +122,13 @@ type Mutation { # Remove a character removeCharacter(input: CharacterRemoveInput!): Character! + + + # Add a channel + addChannel(input: ChannelAddInput!): Channel! + + # Edit a channel + editChannel(input: ChannelEditInput!): Channel! } # A Date represents a RFC3339 encoded date with up to millisecond precision. diff --git a/models/channels/edit.go b/models/channels/edit.go new file mode 100644 index 0000000..da34318 --- /dev/null +++ b/models/channels/edit.go @@ -0,0 +1,35 @@ +package channels + +import ( + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo/bson" +) + +// Edit edits a channels +func Edit(channel models.Channel, logged, hub *bool, eventName, locationName *string) (models.Channel, error) { + mutation := bson.M{} + + if logged != nil && *logged != channel.Logged { + mutation["logged"] = *logged + } + if hub != nil && *hub != channel.Hub { + mutation["hub"] = *hub + } + if eventName != nil && *eventName != channel.EventName { + mutation["eventName"] = *eventName + } + if locationName != nil && *locationName != channel.LocationName { + mutation["locationName"] = *locationName + } + + if len(mutation) == 0 { + return channel, nil + } + + err := collection.UpdateId(channel.Name, bson.M{"$set": mutation}) + if err != nil { + return models.Channel{}, err + } + + return channel, nil +}