From 6f04da49b831fc68c557f88d21f02c5686aa9b25 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sun, 8 Jul 2018 12:10:06 +0200 Subject: [PATCH] Fixed inconsistencies in code and documentation under resolvers, changed log.Remove to be a method instead of a function as well as take the posts with it. --- model/log/log.go | 28 ++++++---- resolver/channel.go | 56 ++++++++++++------- resolver/chapter.go | 64 +++++++++++----------- resolver/character.go | 58 +++++++++++--------- resolver/file.go | 20 ++++--- resolver/log.go | 123 +++++++++++++++++++++++------------------- resolver/post.go | 78 +++++++++++++++------------ resolver/story.go | 116 +++++++++++++++++++++------------------ 8 files changed, 310 insertions(+), 233 deletions(-) diff --git a/model/log/log.go b/model/log/log.go index 1ae6ccf..64451e2 100644 --- a/model/log/log.go +++ b/model/log/log.go @@ -86,16 +86,6 @@ func List(limit int) ([]Log, error) { return listLog(bson.M{}, limit) } -// Remove removes the log post with this ID. Both the long and short ID is accepted -func Remove(id string) error { - return logsCollection.Remove(bson.M{ - "$or": []bson.M{ - bson.M{"_id": id}, - bson.M{"shortId": id}, - }, - }) -} - // ListSearch lists the logs matching the parameters. Empty/zero values means the parameter is ingored when // building the query. This is the old aitelogs2 way, but with the addition of a text search. // @@ -328,6 +318,24 @@ func (log *Log) UpdateCharacters() error { return nil } +// Remove removes the log and all associated posts from the database +func (log *Log) Remove() error { + err := logsCollection.Remove(bson.M{"_id": log.ID}) + if err != nil { + return err + } + + _, err = postCollection.RemoveAll(bson.M{"$or": []bson.M{ + bson.M{"logId": log.ID}, + bson.M{"logId": log.ShortID}, + }}) + if err != nil { + return err + } + + return nil +} + func findLog(query interface{}) (Log, error) { log := Log{} err := logsCollection.Find(query).One(&log) diff --git a/resolver/channel.go b/resolver/channel.go index c80e1c0..2a5755a 100644 --- a/resolver/channel.go +++ b/resolver/channel.go @@ -45,34 +45,41 @@ func (r *QueryResolver) Channels(ctx context.Context, args *ChannelsArgs) ([]*Ch return resolvers, nil } -// ChannelAddEditInput is input for the addChannel mutation -type ChannelAddEditInput struct { - Name string - Logged *bool - Hub *bool - EventName *string - LocationName *string +// ChannelAddArgs is input for the addChannel mutation +type ChannelAddArgs struct { + Input *struct { + Name string + Logged *bool + Hub *bool + EventName *string + LocationName *string + } } // AddChannel resolves the addChannel mutation -func (r *MutationResolver) AddChannel(ctx context.Context, args struct{ Input *ChannelAddEditInput }) (*ChannelResolver, error) { +func (r *MutationResolver) AddChannel(ctx context.Context, args *ChannelAddArgs) (*ChannelResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("channel.add") { return nil, ErrUnauthorized } - logged := args.Input.Logged != nil && *args.Input.Logged - hub := args.Input.Hub != nil && *args.Input.Hub + logged := input.Logged != nil && *input.Logged + + hub := input.Hub != nil && *input.Hub + eventName := "" - if args.Input.EventName != nil { - eventName = *args.Input.EventName + if input.EventName != nil { + eventName = *input.EventName } + locationName := "" - if args.Input.LocationName != nil { - locationName = *args.Input.LocationName + if input.LocationName != nil { + locationName = *input.LocationName } - channel, err := channel.New(args.Input.Name, logged, hub, eventName, locationName) + channel, err := channel.New(input.Name, logged, hub, eventName, locationName) if err != nil { return nil, err } @@ -80,19 +87,32 @@ func (r *MutationResolver) AddChannel(ctx context.Context, args struct{ Input *C return &ChannelResolver{C: channel}, nil } +// ChannelEditArgs is input for the editChannel mutation +type ChannelEditArgs struct { + Input *struct { + Name string + Logged *bool + Hub *bool + EventName *string + LocationName *string + } +} + // EditChannel resolves the editChannel mutation -func (r *MutationResolver) EditChannel(ctx context.Context, args struct{ Input *ChannelAddEditInput }) (*ChannelResolver, error) { +func (r *MutationResolver) EditChannel(ctx context.Context, args *ChannelEditArgs) (*ChannelResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("channel.edit") { return nil, ErrUnauthorized } - channel, err := channel.FindName(args.Input.Name) + channel, err := channel.FindName(input.Name) if err != nil { return nil, err } - err = channel.Edit(args.Input.Logged, args.Input.Hub, args.Input.EventName, args.Input.LocationName) + err = channel.Edit(input.Logged, input.Hub, input.EventName, input.LocationName) if err != nil { return nil, err } diff --git a/resolver/chapter.go b/resolver/chapter.go index 94b86f9..d9aa8f1 100644 --- a/resolver/chapter.go +++ b/resolver/chapter.go @@ -29,33 +29,32 @@ func (r *QueryResolver) Chapter(ctx context.Context, args *ChapterArgs) (*Chapte // AddChapterArgs is args for the addChapter mutation type AddChapterArgs struct { - Input *AddChapterInput -} - -// AddChapterInput is input for the addChapter mutation -type AddChapterInput struct { - StoryID string - Title string - Author *string - Source string - FictionalDate *string + Input *struct { + StoryID string + Title string + Author *string + Source string + FictionalDate *string + } } // AddChapter implements the addChapter mutation func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) (*ChapterResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "chapter.add") { return nil, ErrUnauthorized } - story, err := story.FindID(args.Input.StoryID) + story, err := story.FindID(input.StoryID) if err != nil { return nil, err } author := user.ID - if args.Input.Author != nil { - author = *args.Input.Author + if input.Author != nil { + author = *input.Author if user.ID != author && !user.Permitted("chapter.add") { return nil, ErrPermissionDenied @@ -63,20 +62,20 @@ func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) } fictionalDate := time.Time{} - if args.Input.FictionalDate != nil { - fictionalDate, err = time.Parse(time.RFC3339Nano, *args.Input.FictionalDate) + if input.FictionalDate != nil { + fictionalDate, err = time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } } - chapter, err := story.AddChapter(args.Input.Title, author, args.Input.Source, time.Now(), fictionalDate) + chapter, err := story.AddChapter(input.Title, author, input.Source, time.Now(), fictionalDate) if err != nil { return nil, err } change.Submit("Chapter", "add", user.ID, chapter.ID, map[string]interface{}{ - "title": args.Input.Title, + "title": input.Title, "author": author, "fictionalDate": fictionalDate, }) @@ -84,27 +83,26 @@ func (r *MutationResolver) AddChapter(ctx context.Context, args *AddChapterArgs) return &ChapterResolver{C: chapter}, nil } -// EditChapterArgs is args for the addChapter mutation +// EditChapterArgs is args for the editChapter mutation type EditChapterArgs struct { - Input *EditChapterInput -} - -// EditChapterInput is input for the addChapter mutation -type EditChapterInput struct { - ID string - Title *string - Source *string - FictionalDate *string + Input *struct { + ID string + Title *string + Source *string + FictionalDate *string + } } // EditChapter implements the editChapter mutation func (r *MutationResolver) EditChapter(ctx context.Context, args *EditChapterArgs) (*ChapterResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "chapter.edit") { return nil, ErrUnauthorized } - chapter, err := story.FindChapterID(args.Input.ID) + chapter, err := story.FindChapterID(input.ID) if err != nil { return nil, err } @@ -114,8 +112,8 @@ func (r *MutationResolver) EditChapter(ctx context.Context, args *EditChapterArg } var fictionalDate *time.Time - if args.Input.FictionalDate != nil { - date, err := time.Parse(time.RFC3339Nano, *args.Input.FictionalDate) + if input.FictionalDate != nil { + date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } @@ -123,14 +121,14 @@ func (r *MutationResolver) EditChapter(ctx context.Context, args *EditChapterArg fictionalDate = &date } - err = chapter.Edit(args.Input.Title, args.Input.Source, fictionalDate) + err = chapter.Edit(input.Title, input.Source, fictionalDate) if err != nil { return nil, err } change.Submit("Chapter", "edit", user.ID, chapter.ID, map[string]interface{}{ - "title": args.Input.Title, - "source": args.Input.Source, + "title": input.Title, + "source": input.Source, "fictionalDate": fictionalDate, }) diff --git a/resolver/character.go b/resolver/character.go index d655698..ad6c4e2 100644 --- a/resolver/character.go +++ b/resolver/character.go @@ -54,7 +54,7 @@ type CharactersArgs struct { Author *string } -// Characters resolves +// Characters resolves the characters query func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([]*CharacterResolver, error) { var chars []character.Character var err error @@ -91,7 +91,7 @@ func (r *QueryResolver) Characters(ctx context.Context, args *CharactersArgs) ([ return resolvers, nil } -// AddCharacterInput is args for mutation addCharacter +// AddCharacterInput is args for the addCharacter mutation type AddCharacterInput struct { Nick string Name string @@ -102,24 +102,26 @@ type AddCharacterInput struct { // AddCharacter resolves the addCharacter mutation func (r *MutationResolver) AddCharacter(ctx context.Context, args struct{ Input *AddCharacterInput }) (*CharacterResolver, error) { + input := args.Input user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "character.add") { return nil, ErrUnauthorized } - nick := args.Input.Nick - name := args.Input.Name + nick := input.Nick + + name := input.Name shortName := "" - if args.Input.ShortName != nil { - shortName = *args.Input.ShortName + if input.ShortName != nil { + shortName = *input.ShortName } else { - shortName = strings.SplitN(args.Input.Name, " ", 2)[0] + shortName = strings.SplitN(input.Name, " ", 2)[0] } author := user.ID - if args.Input.Author != nil { - author = *args.Input.Author + if input.Author != nil { + author = *input.Author if author != user.ID && !user.Permitted("character.add") { return nil, ErrPermissionDenied @@ -127,8 +129,8 @@ func (r *MutationResolver) AddCharacter(ctx context.Context, args struct{ Input } description := "" - if args.Input.Description != nil { - description = *args.Input.Description + if input.Description != nil { + description = *input.Description } character, err := character.New(nick, name, shortName, author, description) @@ -155,12 +157,14 @@ type CharacterNickInput struct { // AddCharacterNick resolves the addCharacterNick mutation func (r *MutationResolver) AddCharacterNick(ctx context.Context, args struct{ Input *CharacterNickInput }) (*CharacterResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } - character, err := character.FindID(args.Input.ID) + character, err := character.FindID(input.ID) if err != nil { return nil, err } @@ -168,13 +172,13 @@ func (r *MutationResolver) AddCharacterNick(ctx context.Context, args struct{ In return nil, ErrPermissionDenied } - err = character.AddNick(args.Input.Nick) + err = character.AddNick(input.Nick) if err != nil { return nil, err } go change.Submit("Character", "add.nick", user.ID, character.ID, map[string]interface{}{ - "nick": args.Input.Nick, + "nick": input.Nick, }) log.ScheduleCharacterUpdate() @@ -184,12 +188,14 @@ func (r *MutationResolver) AddCharacterNick(ctx context.Context, args struct{ In // RemoveCharacterNick resolves the removeCharacterNick mutation func (r *MutationResolver) RemoveCharacterNick(ctx context.Context, args struct{ Input *CharacterNickInput }) (*CharacterResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } - character, err := character.FindID(args.Input.ID) + character, err := character.FindID(input.ID) if err != nil { return nil, err } @@ -197,13 +203,13 @@ func (r *MutationResolver) RemoveCharacterNick(ctx context.Context, args struct{ return nil, ErrPermissionDenied } - err = character.RemoveNick(args.Input.Nick) + err = character.RemoveNick(input.Nick) if err != nil { return nil, err } go change.Submit("Character", "remove.nick", user.ID, character.ID, map[string]interface{}{ - "nick": args.Input.Nick, + "nick": input.Nick, }) log.ScheduleCharacterUpdate() @@ -221,12 +227,14 @@ type CharacterEditInput struct { // EditCharacter resolves the editCharacter mutation func (r *MutationResolver) EditCharacter(ctx context.Context, args struct{ Input *CharacterEditInput }) (*CharacterResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } - character, err := character.FindID(args.Input.ID) + character, err := character.FindID(input.ID) if err != nil { return nil, err } @@ -235,16 +243,18 @@ func (r *MutationResolver) EditCharacter(ctx context.Context, args struct{ Input } name := "" - if args.Input.Name != nil { - name = *args.Input.Name + if input.Name != nil { + name = *input.Name } + shortName := "" - if args.Input.ShortName != nil { - shortName = *args.Input.ShortName + if input.ShortName != nil { + shortName = *input.ShortName } + description := "" - if args.Input.Description != nil { - description = *args.Input.Description + if input.Description != nil { + description = *input.Description } err = character.Edit(name, shortName, description) diff --git a/resolver/file.go b/resolver/file.go index 410923a..bb59de4 100644 --- a/resolver/file.go +++ b/resolver/file.go @@ -60,21 +60,25 @@ func (r *QueryResolver) Files(ctx context.Context, args *FilesArgs) ([]*FileReso return resolvers, nil } -// FileEditInput is an input for the editFile mutation -type FileEditInput struct { - ID string - Name *string - Public *bool +// FileEditArgs is args for the editFile mutation +type FileEditArgs struct { + Input *struct { + ID string + Name *string + Public *bool + } } // EditFile resolves the editFile mutation -func (r *MutationResolver) EditFile(ctx context.Context, args *struct{ Input FileEditInput }) (*FileResolver, error) { +func (r *MutationResolver) EditFile(ctx context.Context, args *FileEditArgs) (*FileResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member") { return nil, ErrUnauthorized } - file, err := file.FindID(args.Input.ID) + file, err := file.FindID(input.ID) if err != nil { return nil, err } @@ -82,7 +86,7 @@ func (r *MutationResolver) EditFile(ctx context.Context, args *struct{ Input Fil return nil, ErrUnauthorized } - err = file.Edit(args.Input.Name, args.Input.Public) + err = file.Edit(input.Name, input.Public) if err != nil { return nil, err } diff --git a/resolver/log.go b/resolver/log.go index 5570c2b..8c915e3 100644 --- a/resolver/log.go +++ b/resolver/log.go @@ -44,18 +44,20 @@ func (r *QueryResolver) Log(ctx context.Context, args *LogArgs) (*LogResolver, e return &LogResolver{L: l}, nil } -// LogQueryInput is an input -type LogQueryInput struct { - Search *string - Characters *[]string - Channels *[]string - Events *[]string - Open *bool - Limit *int32 +// LogQueryArgs is args for the logs query +type LogQueryArgs struct { + Input *struct { + Search *string + Characters *[]string + Channels *[]string + Events *[]string + Open *bool + Limit *int32 + } } -// Logs lists logs -func (r *QueryResolver) Logs(ctx context.Context, args *struct{ Input *LogQueryInput }) ([]*LogResolver, error) { +// Logs resolves the logs query +func (r *QueryResolver) Logs(ctx context.Context, args *LogQueryArgs) ([]*LogResolver, error) { var logs []log.Log var err error @@ -65,28 +67,34 @@ func (r *QueryResolver) Logs(ctx context.Context, args *struct{ Input *LogQueryI // Parse input limit := 100 search := "" + if input.Search != nil { search = *input.Search limit = 0 } + channels := []string(nil) if input.Channels != nil { channels = *input.Channels limit = 0 } + characters := []string(nil) if input.Characters != nil { characters = *input.Characters limit = 0 } + events := []string(nil) if input.Events != nil { events = *input.Events limit = 0 } + if input.Limit != nil { limit = int(*input.Limit) } + open := input.Open != nil && *input.Open == true logs, err = log.ListSearch(search, channels, characters, events, open, limit) @@ -108,18 +116,22 @@ func (r *QueryResolver) Logs(ctx context.Context, args *struct{ Input *LogQueryI return resolvers, nil } -// LogAddInput is an input -type LogAddInput struct { - Date string - Channel string - Title *string - Open *bool - Event *string - Description *string +// LogAddArgs is args for the addLog mutation +type LogAddArgs struct { + Input *struct { + Date string + Channel string + Title *string + Open *bool + Event *string + Description *string + } } // AddLog resolves the addLog mutation -func (r *MutationResolver) AddLog(ctx context.Context, args *struct{ Input LogAddInput }) (*LogResolver, error) { +func (r *MutationResolver) AddLog(ctx context.Context, args *LogAddArgs) (*LogResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("log.add") { return nil, ErrUnauthorized @@ -131,20 +143,23 @@ func (r *MutationResolver) AddLog(ctx context.Context, args *struct{ Input LogAd } title := "" - if args.Input.Title != nil { - title = *args.Input.Title + if input.Title != nil { + title = *input.Title } + event := "" - if args.Input.Event != nil { - event = *args.Input.Event + if input.Event != nil { + event = *input.Event } + description := "" - if args.Input.Description != nil { - description = *args.Input.Description + if input.Description != nil { + description = *input.Description } - open := args.Input.Open != nil && *args.Input.Open == true - log, err := log.New(date, args.Input.Channel, title, event, description, open) + open := input.Open != nil && *input.Open == true + + log, err := log.New(date, input.Channel, title, event, description, open) if err != nil { return nil, err } @@ -160,41 +175,45 @@ func (r *MutationResolver) AddLog(ctx context.Context, args *struct{ Input LogAd return &LogResolver{L: log}, nil } -// LogEditInput is an input -type LogEditInput struct { - ID string - Title *string - Event *string - Description *string - Open *bool +// LogEditArgs is an input +type LogEditArgs struct { + Input *struct { + ID string + Title *string + Event *string + Description *string + Open *bool + } } -// EditLog resolves the addLog mutation -func (r *MutationResolver) EditLog(ctx context.Context, args *struct{ Input LogEditInput }) (*LogResolver, error) { +// EditLog resolves the editLog mutation +func (r *MutationResolver) EditLog(ctx context.Context, args *LogEditArgs) (*LogResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("log.edit") { return nil, ErrUnauthorized } - l, err := log.FindID(args.Input.ID) + log, err := log.FindID(input.ID) if err != nil { return nil, err } - err = l.Edit(args.Input.Title, args.Input.Event, args.Input.Description, args.Input.Open) + err = log.Edit(input.Title, input.Event, input.Description, input.Open) if err != nil { return nil, err } - change.Submit("Log", "edit", user.ID, l.ID, map[string]interface{}{ - "channel": l.Channel, - "title": args.Input.Title, - "event": args.Input.Event, - "description": args.Input.Description, - "open": args.Input.Open, + change.Submit("Log", "edit", user.ID, log.ID, map[string]interface{}{ + "channel": log.Channel, + "title": input.Title, + "event": input.Event, + "description": input.Description, + "open": input.Open, }) - return &LogResolver{L: l}, nil + return &LogResolver{L: log}, nil } // RemoveLog resolves the removeLog mutation @@ -204,25 +223,19 @@ func (r *MutationResolver) RemoveLog(ctx context.Context, args *struct{ ID strin return nil, ErrUnauthorized } - l, err := log.FindID(args.ID) + log, err := log.FindID(args.ID) if err != nil { return nil, err } - err = log.Remove(args.ID) + err = log.Remove() if err != nil { return nil, err } - change.Submit("Log", "add", user.ID, l.ID, map[string]interface{}{ - "channel": l.Channel, - "title": l.Title, - "event": l.Event, - "description": l.Description, - "open": l.Open, - }) + change.Submit("Log", "remove", user.ID, log.ID, nil) - return &LogResolver{L: l}, nil + return &LogResolver{L: log}, nil } // ID resolves Log.id diff --git a/resolver/post.go b/resolver/post.go index 62f8929..9840ef6 100644 --- a/resolver/post.go +++ b/resolver/post.go @@ -47,33 +47,37 @@ func (r *QueryResolver) Posts(ctx context.Context, args *PostsArgs) ([]*PostReso return resolvers, nil } -// PostAddInput is an input -type PostAddInput struct { - LogID string - Time string - Kind string - Nick string - Text string +// PostAddArgs is args for addPost mutation +type PostAddArgs struct { + Input *struct { + LogID string + Time string + Kind string + Nick string + Text string + } } // AddPost resolves the addPost mutation -func (r *MutationResolver) AddPost(ctx context.Context, args struct{ Input *PostAddInput }) (*PostResolver, error) { +func (r *MutationResolver) AddPost(ctx context.Context, args *PostAddArgs) (*PostResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.add") { return nil, ErrUnauthorized } - postTime, err := time.Parse(time.RFC3339Nano, args.Input.Time) + postTime, err := time.Parse(time.RFC3339Nano, input.Time) if err != nil { return nil, err } - log, err := log.FindID(args.Input.LogID) + log, err := log.FindID(input.LogID) if err != nil { return nil, err } - post, err := log.NewPost(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text) + post, err := log.NewPost(postTime, input.Kind, input.Nick, input.Text) if err != nil { return nil, err } @@ -92,17 +96,21 @@ func (r *MutationResolver) AddPost(ctx context.Context, args struct{ Input *Post return &PostResolver{P: post}, nil } -// PostEditInput is an input -type PostEditInput struct { - ID string - Time *string - Kind *string - Nick *string - Text *string +// PostEditArgs is args for the editPost mutation +type PostEditArgs struct { + Input *struct { + ID string + Time *string + Kind *string + Nick *string + Text *string + } } // EditPost resolves the editPost mutation -func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *PostEditInput }) (*PostResolver, error) { +func (r *MutationResolver) EditPost(ctx context.Context, args *PostEditArgs) (*PostResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.edit") { return nil, ErrUnauthorized @@ -110,7 +118,7 @@ func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *Pos postTime := (*time.Time)(nil) if args.Input.Time != nil { - t, err := time.Parse(time.RFC3339Nano, *args.Input.Time) + t, err := time.Parse(time.RFC3339Nano, *input.Time) if err != nil { return nil, err } @@ -118,21 +126,21 @@ func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *Pos postTime = &t } - post, err := log.FindPostID(args.Input.ID) + post, err := log.FindPostID(input.ID) if err != nil { return nil, err } - err = post.Edit(postTime, args.Input.Kind, args.Input.Nick, args.Input.Text) + err = post.Edit(postTime, input.Kind, input.Nick, input.Text) if err != nil { return nil, err } change.Submit("Post", "edit", user.ID, post.ID, map[string]interface{}{ "time": postTime, - "kind": args.Input.Kind, - "nick": args.Input.Nick, - "text": args.Input.Text, + "kind": input.Kind, + "nick": input.Nick, + "text": input.Text, }) go log.UpdateCharacters(post.LogID) @@ -140,32 +148,36 @@ func (r *MutationResolver) EditPost(ctx context.Context, args struct{ Input *Pos return &PostResolver{P: post}, nil } -// PostMoveInput is an input -type PostMoveInput struct { - ID string - ToPosition int32 +// PostMoveArgs is args for movePost mutation +type PostMoveArgs struct { + Input *struct { + ID string + ToPosition int32 + } } // MovePost resolves the movePost mutation -func (r *MutationResolver) MovePost(ctx context.Context, args struct{ Input *PostMoveInput }) (*PostResolver, error) { +func (r *MutationResolver) MovePost(ctx context.Context, args *PostMoveArgs) (*PostResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("post.move") { return nil, ErrUnauthorized } - post, err := log.FindPostID(args.Input.ID) + post, err := log.FindPostID(input.ID) if err != nil { return nil, err } - err = post.Move(int(args.Input.ToPosition)) + err = post.Move(int(input.ToPosition)) if err != nil { return nil, err } change.Submit("Post", "move", user.ID, post.ID, map[string]interface{}{ "logId": post.LogID, - "targetIndex": args.Input.ToPosition, + "targetIndex": input.ToPosition, }) return &PostResolver{P: post}, nil diff --git a/resolver/story.go b/resolver/story.go index 65eb40d..bb945e0 100644 --- a/resolver/story.go +++ b/resolver/story.go @@ -29,28 +29,27 @@ func (r *QueryResolver) Story(ctx context.Context, args *StoryArgs) (*StoryResol // StoriesArg is args for stories query type StoriesArg struct { - Input *StoriesInput -} - -// StoriesInput resolves the TagInput input -type StoriesInput struct { - Author *string - Tags *[]TagInput - EarliestFictionalDate *string - LatestFictionalDate *string - Limit *int32 + Input *struct { + Author *string + Tags *[]TagInput + EarliestFictionalDate *string + LatestFictionalDate *string + Limit *int32 + } } // Stories implements the stories query func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*StoryResolver, error) { + input := args.Input + author := "" - if args.Input.Author != nil { - author = *args.Input.Author + if input.Author != nil { + author = *input.Author } tags := make([]story.Tag, 0, 8) - if args.Input.Tags != nil { - for _, tagInput := range *args.Input.Tags { + if input.Tags != nil { + for _, tagInput := range *input.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, @@ -60,24 +59,24 @@ func (r *QueryResolver) Stories(ctx context.Context, args *StoriesArg) ([]*Story earliest := time.Time{} err := error(nil) - if args.Input.EarliestFictionalDate != nil { - earliest, err = time.Parse(time.RFC3339Nano, *args.Input.EarliestFictionalDate) + if input.EarliestFictionalDate != nil { + earliest, err = time.Parse(time.RFC3339Nano, *input.EarliestFictionalDate) if err != nil { return nil, err } } latest := time.Time{} - if args.Input.LatestFictionalDate != nil { - latest, err = time.Parse(time.RFC3339Nano, *args.Input.LatestFictionalDate) + if input.LatestFictionalDate != nil { + latest, err = time.Parse(time.RFC3339Nano, *input.LatestFictionalDate) if err != nil { return nil, err } } limit := 30 - if args.Input.Limit != nil { - limit = int(*args.Input.Limit) + if input.Limit != nil { + limit = int(*input.Limit) } stories, err := story.List(author, tags, earliest, latest, limit) @@ -108,27 +107,29 @@ type StoryAddArgs struct { // AddStory implements the addStory mutation func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*StoryResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.add") { return nil, ErrUnauthorized } author := user.ID - if args.Input.Author != nil { - author = *args.Input.Author + if input.Author != nil { + author = *input.Author if user.ID != author && !user.Permitted("story.add") { return nil, ErrPermissionDenied } } - listed := (args.Input.Listed != nil && *args.Input.Listed == true) + listed := (input.Listed != nil && *input.Listed == true) - open := (args.Input.Open != nil && *args.Input.Open == true) + open := (input.Open != nil && *input.Open == true) tags := make([]story.Tag, 0, 8) - if args.Input.Tags != nil { - for _, tagInput := range *args.Input.Tags { + if input.Tags != nil { + for _, tagInput := range *input.Tags { tags = append(tags, story.Tag{ Kind: tagInput.Kind, Name: tagInput.Name, @@ -137,8 +138,8 @@ func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*S } fictionalDate := time.Time{} - if args.Input.FictionalDate != nil { - date, err := time.Parse(time.RFC3339Nano, *args.Input.FictionalDate) + if input.FictionalDate != nil { + date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } @@ -146,22 +147,22 @@ func (r *MutationResolver) AddStory(ctx context.Context, args *StoryAddArgs) (*S fictionalDate = date } - story, err := story.New(args.Input.Name, author, args.Input.Category, listed, open, tags, time.Now(), fictionalDate) + story, err := story.New(input.Name, author, input.Category, listed, open, tags, time.Now(), fictionalDate) if err != nil { return nil, err } change.Submit("Story", "add", user.ID, story.ID, map[string]interface{}{ - "name": args.Input.Name, - "category": args.Input.Category, - "author": args.Input.Author, + "name": input.Name, + "category": input.Category, + "author": input.Author, }) return &StoryResolver{S: story}, nil } -// StoryTagArgs is args for the addStory mutation -type StoryTagArgs struct { +// StoryTagAddArgs is args for the addStoryTag mutation +type StoryTagAddArgs struct { Input *struct { ID string Tag TagInput @@ -169,15 +170,16 @@ type StoryTagArgs struct { } // AddStoryTag implements the addStoryTag mutation -func (r *MutationResolver) AddStoryTag(ctx context.Context, args *StoryTagArgs) (*StoryResolver, error) { +func (r *MutationResolver) AddStoryTag(ctx context.Context, args *StoryTagAddArgs) (*StoryResolver, error) { + input := args.Input + tag := story.Tag{Kind: input.Tag.Kind, Name: input.Tag.Name} + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } - tag := story.Tag{Kind: args.Input.Tag.Kind, Name: args.Input.Tag.Name} - - story, err := story.FindID(args.Input.ID) + story, err := story.FindID(input.ID) if err != nil { return nil, err } @@ -199,16 +201,25 @@ func (r *MutationResolver) AddStoryTag(ctx context.Context, args *StoryTagArgs) return &StoryResolver{S: story}, nil } +// StoryTagRemoveArgs is args for the removeStoryTag mutation +type StoryTagRemoveArgs struct { + Input *struct { + ID string + Tag TagInput + } +} + // RemoveStoryTag implements the removeStoryTag mutation -func (r *MutationResolver) RemoveStoryTag(ctx context.Context, args *StoryTagArgs) (*StoryResolver, error) { +func (r *MutationResolver) RemoveStoryTag(ctx context.Context, args *StoryTagRemoveArgs) (*StoryResolver, error) { + input := args.Input + tag := story.Tag{Kind: input.Tag.Kind, Name: input.Tag.Name} + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } - tag := story.Tag{Kind: args.Input.Tag.Kind, Name: args.Input.Tag.Name} - - story, err := story.FindID(args.Input.ID) + story, err := story.FindID(input.ID) if err != nil { return nil, err } @@ -245,12 +256,14 @@ type StoryEditArgs struct { // EditStory implements the editStory mutation func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) (*StoryResolver, error) { + input := args.Input + user := session.FromContext(ctx).User() if user == nil || !user.Permitted("member", "story.edit") { return nil, ErrUnauthorized } - story, err := story.FindID(args.Input.ID) + story, err := story.FindID(input.ID) if err != nil { return nil, err } @@ -260,8 +273,8 @@ func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) ( } var fictionalDate *time.Time - if args.Input.FictionalDate != nil { - date, err := time.Parse(time.RFC3339Nano, *args.Input.FictionalDate) + if input.FictionalDate != nil { + date, err := time.Parse(time.RFC3339Nano, *input.FictionalDate) if err != nil { return nil, err } @@ -269,19 +282,18 @@ func (r *MutationResolver) EditStory(ctx context.Context, args *StoryEditArgs) ( fictionalDate = &date } - input := args.Input err = story.Edit(input.Name, input.Category, input.Listed, input.Open, fictionalDate) if err != nil { return nil, err } change.Submit("Story", "edit", user.ID, story.ID, map[string]interface{}{ - "name": args.Input.Name, - "category": args.Input.Category, - "author": args.Input.Author, - "open": args.Input.Open, - "listed": args.Input.Listed, - "fictionalDate": args.Input.FictionalDate, + "name": input.Name, + "category": input.Category, + "author": input.Author, + "open": input.Open, + "listed": input.Listed, + "fictionalDate": input.FictionalDate, }) return &StoryResolver{S: story}, nil