From 9affdbd20ec961a60d1fe83b43e01abfc8cdde69 Mon Sep 17 00:00:00 2001 From: Gisle Aune Date: Sat, 27 Oct 2018 14:59:47 +0200 Subject: [PATCH] graph2: Added move mutation to chapter. --- graph2/queries/chapter.go | 23 +++++++++++++++++++++++ graph2/schema/root.gql | 3 +++ graph2/schema/types/Chapter.gql | 9 +++++++++ models/chapters/move.go | 28 ++++++++++++++++++++++++++++ 4 files changed, 63 insertions(+) create mode 100644 models/chapters/move.go diff --git a/graph2/queries/chapter.go b/graph2/queries/chapter.go index d2e81fe..e018b0b 100644 --- a/graph2/queries/chapter.go +++ b/graph2/queries/chapter.go @@ -48,6 +48,29 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd return chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate) } +func (r *mutationResolver) MoveChapter(ctx context.Context, input input.ChapterMoveInput) (models.Chapter, error) { + chapter, err := chapters.FindID(input.ID) + if err != nil { + return models.Chapter{}, errors.New("Chapter not found") + } + + token := auth.TokenFromContext(ctx) + if !token.Authenticated() || !token.PermittedUser(chapter.Author, "member", "chapter.move") { + return models.Chapter{}, errors.New("You are not allowed to move this chapter") + } + + target, err := stories.FindID(input.StoryID) + if err != nil { + return models.Chapter{}, errors.New("Target story not found") + } + + if !target.Open && !token.PermittedUser(target.Author, "member", "chapter.move") { + return models.Chapter{}, errors.New("You are not permitted to move chapters to this story") + } + + return chapters.Move(chapter, target) +} + func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterEditInput) (models.Chapter, error) { chapter, err := chapters.FindID(input.ID) if err != nil { diff --git a/graph2/schema/root.gql b/graph2/schema/root.gql index 0e8cebc..e08c4c6 100644 --- a/graph2/schema/root.gql +++ b/graph2/schema/root.gql @@ -81,6 +81,9 @@ type Mutation { # Edit a chapter editChapter(input: ChapterEditInput!): Chapter! + # Move a chapter + moveChapter(input: ChapterMoveInput!): Chapter! + # Remove a chapter removeChapter(input: ChapterRemoveInput!): Chapter! diff --git a/graph2/schema/types/Chapter.gql b/graph2/schema/types/Chapter.gql index 50eaa27..e4a1be6 100644 --- a/graph2/schema/types/Chapter.gql +++ b/graph2/schema/types/Chapter.gql @@ -61,6 +61,15 @@ input ChapterEditInput { clearFictionalDate: Boolean } +# Input for moveChapter mutation +input ChapterMoveInput { + # The chapter to move + id: String! + + # The story to move it to + storyId: String! +} + # Input for removeChapter mutation input ChapterRemoveInput { # The chapter to remove diff --git a/models/chapters/move.go b/models/chapters/move.go new file mode 100644 index 0000000..c60328f --- /dev/null +++ b/models/chapters/move.go @@ -0,0 +1,28 @@ +package chapters + +import ( + "time" + + "git.aiterp.net/rpdata/api/models" + "github.com/globalsign/mgo/bson" +) + +// Move updates the chapter, moving it to the given story. +func Move(chapter models.Chapter, story models.Story) (models.Chapter, error) { + now := time.Now() + + err := collection.UpdateId(chapter.ID, bson.M{"$set": bson.M{"editedDate": now, "storyId": story.ID}}) + if err != nil { + return models.Chapter{}, err + } + + chapter.EditedDate = now + + if chapter.CreatedDate.After(story.UpdatedDate) { + if err := storyCollection.UpdateId(story.ID, bson.M{"$set": bson.M{"updatedDate": chapter.CreatedDate}}); err == nil { + story.UpdatedDate = chapter.CreatedDate + } + } + + return chapter, nil +}