@@ -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)
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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
|
||||
}
|
||||
Reference in New Issue
Block a user