Browse Source

Moved LogResolver and PostResolver into resolvers/types package, updated references to it.

1.0
Gisle Aune 7 years ago
parent
commit
dced5282b1
  1. 131
      resolver/log.go
  2. 65
      resolver/post.go
  3. 117
      resolver/types/log.go
  4. 45
      resolver/types/post.go

131
resolver/log.go

@ -2,10 +2,8 @@ package resolver
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/loader"
"git.aiterp.net/rpdata/api/model/change"
"git.aiterp.net/rpdata/api/resolver/types"
@ -13,21 +11,13 @@ import (
"git.aiterp.net/rpdata/api/model/log"
)
// LogResolver for the Log graphql type
type LogResolver struct{ L log.Log }
// LogArgs is an arg
type LogArgs struct {
ID *string
}
// LogPostArgs is an arg
type LogPostArgs struct {
Kinds *[]string
}
// Log finds log
func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*LogResolver, error) {
func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*types.LogResolver, error) {
var l log.Log
var err error
@ -42,7 +32,7 @@ func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*LogResolver, e
return nil, err
}
return &LogResolver{L: l}, nil
return &types.LogResolver{L: l}, nil
}
// LogQueryArgs is args for the logs query
@ -58,7 +48,7 @@ type LogQueryArgs struct {
}
// Logs resolves the logs query
func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*LogResolver, error) {
func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*types.LogResolver, error) {
var logs []log.Log
var err error
@ -108,9 +98,9 @@ func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*LogRes
}
}
resolvers := make([]*LogResolver, len(logs))
resolvers := make([]*types.LogResolver, len(logs))
for i := range logs {
resolvers[i] = &LogResolver{L: logs[i]}
resolvers[i] = &types.LogResolver{L: logs[i]}
}
return resolvers, nil
@ -129,7 +119,7 @@ type LogAddArgs struct {
}
// AddLog resolves the addLog mutation
func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*LogResolver, error) {
func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*types.LogResolver, error) {
input := args.Input
user := session.FromContext(ctx).User()
@ -172,7 +162,7 @@ func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*LogRe
"open": log.Open,
})
return &LogResolver{L: log}, nil
return &types.LogResolver{L: log}, nil
}
// LogEditArgs is an input
@ -187,7 +177,7 @@ type LogEditArgs struct {
}
// EditLog resolves the editLog mutation
func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*LogResolver, error) {
func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*types.LogResolver, error) {
input := args.Input
user := session.FromContext(ctx).User()
@ -213,11 +203,11 @@ func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*Log
"open": input.Open,
})
return &LogResolver{L: log}, nil
return &types.LogResolver{L: log}, nil
}
// RemoveLog resolves the removeLog mutation
func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*LogResolver, error) {
func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID string }) (*types.LogResolver, error) {
user := session.FromContext(ctx).User()
if user == nil || !user.Permitted("log.remove") {
return nil, ErrUnauthorized
@ -235,104 +225,5 @@ func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID strin
go change.Submit("Log", "remove", user.ID, log.ID, nil)
return &LogResolver{L: log}, nil
}
// ID resolves Log.id
func (r *LogResolver) ID() string {
return r.L.ID
}
// ShortID resolves Log.shortId
func (r *LogResolver) ShortID() string {
return r.L.ShortID
}
// Date resolves Log.date
func (r *LogResolver) Date() string {
return r.L.Date.Format(time.RFC3339Nano)
}
// ChannelName resolves Log.channelName
func (r *LogResolver) ChannelName() string {
return r.L.Channel
}
// Channel resolves Log.channe
func (r *LogResolver) Channel(ctx context.Context) (*types.ChannelResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
channel, err := loader.Channel("name", r.L.Channel)
if err != nil {
return nil, err
}
return &types.ChannelResolver{C: channel}, nil
}
// Title resolves Log.title
func (r *LogResolver) Title() string {
return r.L.Title
}
// Event resolves Log.event
func (r *LogResolver) Event() string {
return r.L.Event
}
// Description resolves Log.description
func (r *LogResolver) Description() string {
return r.L.Description
}
// Open resolves Log.open
func (r *LogResolver) Open() bool {
return r.L.Open
}
// Characters resolves Log.characters
func (r *LogResolver) Characters(ctx context.Context) ([]*types.CharacterResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
chars, err := loader.Characters("id", r.L.CharacterIDs...)
if err != nil {
return nil, err
}
resolvers := make([]*types.CharacterResolver, 0, len(chars))
for i := range chars {
if chars[i].ID == "" {
continue
}
resolvers = append(resolvers, &types.CharacterResolver{C: chars[i]})
}
return resolvers, nil
}
// Posts resolves Log.posts
func (r *LogResolver) Posts(ctx context.Context, args *LogPostArgs) ([]*PostResolver, error) {
var kinds []string
if args.Kinds != nil {
kinds = *args.Kinds
}
posts, err := r.L.Posts(kinds...)
if err != nil {
return nil, err
}
resolvers := make([]*PostResolver, len(posts))
for i := range posts {
resolvers[i] = &PostResolver{posts[i]}
}
return resolvers, nil
return &types.LogResolver{L: log}, nil
}

65
resolver/post.go

@ -7,24 +7,22 @@ import (
"git.aiterp.net/rpdata/api/internal/session"
"git.aiterp.net/rpdata/api/model/change"
"git.aiterp.net/rpdata/api/model/log"
"git.aiterp.net/rpdata/api/resolver/types"
)
// PostResolver for the Post graphql type
type PostResolver struct{ P log.Post }
// PostArgs is an arg
type PostArgs struct {
ID string
}
// Post implements the post query
func (r *QueryResolver) Post(ctx context.Context, args *PostArgs) (*PostResolver, error) {
func (r *QueryResolver) Post(ctx context.Context, args *PostArgs) (*types.PostResolver, error) {
post, err := log.FindPostID(args.ID)
if err != nil {
return nil, err
}
return &PostResolver{P: post}, nil
return &types.PostResolver{P: post}, nil
}
// PostsArgs is an arg
@ -33,15 +31,15 @@ type PostsArgs struct {
}
// Posts implements the posts query
func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*PostResolver, error) {
func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*types.PostResolver, error) {
posts, err := log.ListPostIDs(args.IDs...)
if err != nil {
return nil, err
}
resolvers := make([]*PostResolver, len(posts))
resolvers := make([]*types.PostResolver, len(posts))
for i := range resolvers {
resolvers[i] = &PostResolver{P: posts[i]}
resolvers[i] = &types.PostResolver{P: posts[i]}
}
return resolvers, nil
@ -59,7 +57,7 @@ type PostAddArgs struct {
}
// AddPost resolves the addPost mutation
func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*PostResolver, error) {
func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*types.PostResolver, error) {
input := args.Input
user := session.FromContext(ctx).User()
@ -93,7 +91,7 @@ func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*Pos
go log.UpdateCharacters()
return &PostResolver{P: post}, nil
return &types.PostResolver{P: post}, nil
}
// PostEditArgs is args for the editPost mutation
@ -108,7 +106,7 @@ type PostEditArgs struct {
}
// EditPost resolves the editPost mutation
func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*PostResolver, error) {
func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*types.PostResolver, error) {
input := args.Input
user := session.FromContext(ctx).User()
@ -145,7 +143,7 @@ func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*P
go log.UpdateCharacters(post.LogID)
return &PostResolver{P: post}, nil
return &types.PostResolver{P: post}, nil
}
// PostMoveArgs is args for movePost mutation
@ -157,7 +155,7 @@ type PostMoveArgs struct {
}
// MovePost resolves the movePost mutation
func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*PostResolver, error) {
func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*types.PostResolver, error) {
input := args.Input
user := session.FromContext(ctx).User()
@ -180,7 +178,7 @@ func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*P
"targetIndex": input.ToPosition,
})
return &PostResolver{P: post}, nil
return &types.PostResolver{P: post}, nil
}
// PostRemoveArgs is an arg
@ -189,7 +187,7 @@ type PostRemoveArgs struct {
}
// RemovePost resolves the removePost mutation
func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*PostResolver, error) {
func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs) (*types.PostResolver, error) {
user := session.FromContext(ctx).User()
if user == nil || !user.Permitted("post.remove") {
return nil, ErrUnauthorized
@ -206,40 +204,5 @@ func (r *MutationResolver) RemovePost(ctx context.Context, args PostRemoveArgs)
go log.UpdateCharacters(post.LogID)
return &PostResolver{P: post}, nil
}
// ID resolves Post.id
func (r *PostResolver) ID() string {
return r.P.ID
}
// LogID resolves Post.logId
func (r *PostResolver) LogID() string {
return r.P.LogID
}
// Time resolves Post.time
func (r *PostResolver) Time() string {
return r.P.Time.Format(time.RFC3339Nano)
}
// Kind resolves Post.logId
func (r *PostResolver) Kind() string {
return r.P.Kind
}
// Nick resolves Post.nick
func (r *PostResolver) Nick() string {
return r.P.Nick
}
// Text resolves Post.text
func (r *PostResolver) Text() string {
return r.P.Text
}
// Position resolves Post.text
func (r *PostResolver) Position() int32 {
return int32(r.P.Position)
return &types.PostResolver{P: post}, nil
}

117
resolver/types/log.go

@ -0,0 +1,117 @@
package types
import (
"context"
"errors"
"time"
"git.aiterp.net/rpdata/api/loader"
"git.aiterp.net/rpdata/api/model/log"
)
// LogResolver for the Log graphql type
type LogResolver struct{ L log.Log }
// ID resolves Log.id
func (r *LogResolver) ID() string {
return r.L.ID
}
// ShortID resolves Log.shortId
func (r *LogResolver) ShortID() string {
return r.L.ShortID
}
// Date resolves Log.date
func (r *LogResolver) Date() string {
return r.L.Date.Format(time.RFC3339Nano)
}
// ChannelName resolves Log.channelName
func (r *LogResolver) ChannelName() string {
return r.L.Channel
}
// Channel resolves Log.channe
func (r *LogResolver) Channel(ctx context.Context) (*ChannelResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
channel, err := loader.Channel("name", r.L.Channel)
if err != nil {
return nil, err
}
return &ChannelResolver{C: channel}, nil
}
// Title resolves Log.title
func (r *LogResolver) Title() string {
return r.L.Title
}
// Event resolves Log.event
func (r *LogResolver) Event() string {
return r.L.Event
}
// Description resolves Log.description
func (r *LogResolver) Description() string {
return r.L.Description
}
// Open resolves Log.open
func (r *LogResolver) Open() bool {
return r.L.Open
}
// Characters resolves Log.characters
func (r *LogResolver) Characters(ctx context.Context) ([]*CharacterResolver, error) {
loader := loader.FromContext(ctx)
if loader == nil {
return nil, errors.New("no loader")
}
chars, err := loader.Characters("id", r.L.CharacterIDs...)
if err != nil {
return nil, err
}
resolvers := make([]*CharacterResolver, 0, len(chars))
for i := range chars {
if chars[i].ID == "" {
continue
}
resolvers = append(resolvers, &CharacterResolver{C: chars[i]})
}
return resolvers, nil
}
// Posts resolves Log.posts
func (r *LogResolver) Posts(ctx context.Context, args *LogPostArgs) ([]*PostResolver, error) {
var kinds []string
if args.Kinds != nil {
kinds = *args.Kinds
}
posts, err := r.L.Posts(kinds...)
if err != nil {
return nil, err
}
resolvers := make([]*PostResolver, len(posts))
for i := range posts {
resolvers[i] = &PostResolver{posts[i]}
}
return resolvers, nil
}
// LogPostArgs is an arg
type LogPostArgs struct {
Kinds *[]string
}

45
resolver/types/post.go

@ -0,0 +1,45 @@
package types
import (
"time"
"git.aiterp.net/rpdata/api/model/log"
)
// PostResolver for the Post graphql type
type PostResolver struct{ P log.Post }
// ID resolves Post.id
func (r *PostResolver) ID() string {
return r.P.ID
}
// LogID resolves Post.logId
func (r *PostResolver) LogID() string {
return r.P.LogID
}
// Time resolves Post.time
func (r *PostResolver) Time() string {
return r.P.Time.Format(time.RFC3339Nano)
}
// Kind resolves Post.logId
func (r *PostResolver) Kind() string {
return r.P.Kind
}
// Nick resolves Post.nick
func (r *PostResolver) Nick() string {
return r.P.Nick
}
// Text resolves Post.text
func (r *PostResolver) Text() string {
return r.P.Text
}
// Position resolves Post.text
func (r *PostResolver) Position() int32 {
return int32(r.P.Position)
}
Loading…
Cancel
Save