Browse Source

graph2: Added Channel type and queries

1.0
Gisle Aune 6 years ago
parent
commit
1be4088a8f
  1. 4
      graph2/gqlgen.yml
  2. 15
      graph2/queries/channel.go
  3. 7
      graph2/schema/root.gql
  4. 63
      graph2/schema/types/Channel.gql
  5. 55
      model/channel/channel.go

4
graph2/gqlgen.yml

@ -17,3 +17,7 @@ models:
model: git.aiterp.net/rpdata/api/model/character.Character model: git.aiterp.net/rpdata/api/model/character.Character
CharactersFilter: CharactersFilter:
model: git.aiterp.net/rpdata/api/model/character.Filter 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

15
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)
}

7
graph2/schema/root.gql

@ -8,6 +8,13 @@ type Query {
# Find characters # Find characters
characters(filter: CharactersFilter): [Character!]! 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 # Find all distinct tags used in stories

63
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
}

55
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 // A Channel represents information abount an IRC RP channel, and whether it should be logged
type Channel struct { 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 // Edit edits the channel
@ -36,13 +43,13 @@ func (channel *Channel) Edit(logged, hub *bool, event, location *string) error {
changes["hub"] = *hub changes["hub"] = *hub
changed.Hub = *hub changed.Hub = *hub
} }
if event != nil && channel.Event != *event {
if event != nil && channel.EventName != *event {
changes["event"] = *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 changes["location"] = *location
changed.Location = *location
changed.LocationName = *location
} }
if len(changes) == 0 { if len(changes) == 0 {
@ -84,11 +91,11 @@ func New(name string, logged, hub bool, event, location string) (Channel, error)
} }
channel := Channel{ 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) 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 // List finds channels, if logged is true it will be limited to logged
// channels // channels
func List(logged *bool, eventName *string, locationName *string) ([]Channel, error) {
func List(filter *Filter) ([]Channel, error) {
query := bson.M{} 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) channels := make([]Channel, 0, 128)

Loading…
Cancel
Save