diff --git a/graph2/gqlgen.yml b/graph2/gqlgen.yml index 1ebbd3d..630a38f 100644 --- a/graph2/gqlgen.yml +++ b/graph2/gqlgen.yml @@ -17,3 +17,7 @@ models: model: git.aiterp.net/rpdata/api/model/character.Character CharactersFilter: model: git.aiterp.net/rpdata/api/model/character.Filter + Channel: + model: git.aiterp.net/rpdata/api/model/channel.Channel + ChannelsFilter: + model: git.aiterp.net/rpdata/api/model/channel.Filter \ No newline at end of file diff --git a/graph2/queries/channel.go b/graph2/queries/channel.go new file mode 100644 index 0000000..68b116a --- /dev/null +++ b/graph2/queries/channel.go @@ -0,0 +1,15 @@ +package queries + +import ( + "context" + + "git.aiterp.net/rpdata/api/model/channel" +) + +func (r *resolver) Channel(ctx context.Context, name string) (channel.Channel, error) { + return channel.FindName(name) +} + +func (r *resolver) Channels(ctx context.Context, filter *channel.Filter) ([]channel.Channel, error) { + return channel.List(filter) +} diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index a45ba5b..c19fea7 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -8,6 +8,13 @@ type Query { # Find characters characters(filter: CharactersFilter): [Character!]! + + + # Find channel by name + channel(name: String!): Channel! + + # Find channels + channels(filter: ChannelsFilter): [Channel!]! # Find all distinct tags used in stories diff --git a/graph2/schema/types/Channel.gql b/graph2/schema/types/Channel.gql new file mode 100644 index 0000000..19f8131 --- /dev/null +++ b/graph2/schema/types/Channel.gql @@ -0,0 +1,63 @@ +# Information about an IRC channel +type Channel { + # The channel's name + name: String! + + # Whether the channel should be logged + logged: Boolean! + + # Whether the channel is a hub channel + hub: Boolean! + + # The event name, or `null` if none is specified + eventName: String + + # The location name, or `null` if none is specified + locationName: String +} + +# Filters for the channels query +input ChannelsFilter { + # Filter to either logged or unlogged channels + logged: Boolean + + # Filter by event name + eventName: String + + # Filter by location name + locationName: String +} + +input ChannelAddInput { + # The channel's name + name: String! + + # Whether the channel should be logged + logged: Boolean + + # Whether the channel is a hub channel + hub: Boolean + + # The event name, or `null` if none is specified + eventName: String + + # The location name, or `null` if none is specified + locationName: String +} + +input ChannelEditInput { + # The channel's name + name: String! + + # Whether the channel should be logged + logged: Boolean + + # Whether the channel is a hub channel + hub: Boolean + + # The event name, or `null` if none is specified + eventName: String + + # The location name, or `null` if none is specified + locationName: String +} \ No newline at end of file diff --git a/model/channel/channel.go b/model/channel/channel.go index c61362f..093d660 100644 --- a/model/channel/channel.go +++ b/model/channel/channel.go @@ -16,11 +16,18 @@ var ErrInvalidName = errors.New("Invalid channel name") // A Channel represents information abount an IRC RP channel, and whether it should be logged type Channel struct { - Name string `bson:"_id"` - Logged bool `bson:"logged"` - Hub bool `bson:"hub"` - Event string `bson:"event,omitempty"` - Location string `bson:"location,omitempty"` + Name string `bson:"_id"` + Logged bool `bson:"logged"` + Hub bool `bson:"hub"` + EventName string `bson:"event,omitempty"` + LocationName string `bson:"location,omitempty"` +} + +// Filter for searching +type Filter struct { + Logged *bool `json:"logged"` + EventName string `json:"eventName"` + LocationName string `json:"locationName"` } // Edit edits the channel @@ -36,13 +43,13 @@ func (channel *Channel) Edit(logged, hub *bool, event, location *string) error { changes["hub"] = *hub changed.Hub = *hub } - if event != nil && channel.Event != *event { + if event != nil && channel.EventName != *event { changes["event"] = *event - changed.Event = *event + changed.EventName = *event } - if location != nil && channel.Event != *location { + if location != nil && channel.LocationName != *location { changes["location"] = *location - changed.Location = *location + changed.LocationName = *location } if len(changes) == 0 { @@ -84,11 +91,11 @@ func New(name string, logged, hub bool, event, location string) (Channel, error) } channel := Channel{ - Name: name, - Logged: logged, - Hub: hub, - Event: event, - Location: location, + Name: name, + Logged: logged, + Hub: hub, + EventName: event, + LocationName: location, } err := collection.Insert(channel) @@ -109,17 +116,19 @@ func FindName(name string) (Channel, error) { // List finds channels, if logged is true it will be limited to logged // channels -func List(logged *bool, eventName *string, locationName *string) ([]Channel, error) { +func List(filter *Filter) ([]Channel, error) { query := bson.M{} - if logged != nil { - query["logged"] = *logged - } - if eventName != nil { - query["eventName"] = *eventName - } - if locationName != nil { - query["locationName"] = *locationName + if filter != nil { + if filter.Logged != nil { + query["logged"] = *filter.Logged + } + if filter.EventName != "" { + query["eventName"] = filter.EventName + } + if filter.LocationName != "" { + query["locationName"] = filter.LocationName + } } channels := make([]Channel, 0, 128)