diff --git a/graph2/queries/chapter.go b/graph2/queries/chapter.go index 47bf7c3..d703378 100644 --- a/graph2/queries/chapter.go +++ b/graph2/queries/chapter.go @@ -48,7 +48,12 @@ func (r *mutationResolver) AddChapter(ctx context.Context, input input.ChapterAd return models.Chapter{}, errors.New("Story is not open") } - chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate) + commentMode := models.ChapterCommentModeDisabled + if input.CommentMode != nil { + commentMode = *input.CommentMode + } + + chapter, err := chapters.Add(story, input.Title, author, input.Source, time.Now(), input.FictionalDate, commentMode) if err != nil { return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error()) } @@ -117,7 +122,7 @@ func (r *mutationResolver) EditChapter(ctx context.Context, input input.ChapterE input.FictionalDate = &time.Time{} } - chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate) + chapter, err = chapters.Edit(chapter, input.Title, input.Source, input.FictionalDate, input.CommentMode, input.CommentsLocked) if err != nil { return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error()) } diff --git a/graph2/schema/types/Chapter.gql b/graph2/schema/types/Chapter.gql index 36099c9..d82f371 100644 --- a/graph2/schema/types/Chapter.gql +++ b/graph2/schema/types/Chapter.gql @@ -70,6 +70,9 @@ input ChapterAddInput { # Optionally, assign a fictional date for the chapter fictionalDate: Date + + "The comment mode for the chapter." + commentMode: ChapterCommentMode } # Input for editChapter mutation @@ -88,6 +91,12 @@ input ChapterEditInput { # Remove the fictional date for a chapter clearFictionalDate: Boolean + + "Set the comment mode for the chapter" + commentMode: ChapterCommentMode + + "Set whether the chapter comments are locked" + commentsLocked: Boolean } # Input for moveChapter mutation diff --git a/models/chapters/add.go b/models/chapters/add.go index 87e7f12..37c7dfd 100644 --- a/models/chapters/add.go +++ b/models/chapters/add.go @@ -8,15 +8,17 @@ import ( ) // Add adds a new chapter. -func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time) (models.Chapter, error) { +func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time, commentMode models.ChapterCommentMode) (models.Chapter, error) { chapter := models.Chapter{ - ID: makeChapterID(), - StoryID: story.ID, - Title: title, - Author: author, - Source: source, - CreatedDate: createdDate, - EditedDate: createdDate, + ID: makeChapterID(), + StoryID: story.ID, + Title: title, + Author: author, + Source: source, + CreatedDate: createdDate, + EditedDate: createdDate, + CommentMode: commentMode, + CommentsLocked: false, } if finctionalDate != nil { diff --git a/models/chapters/edit.go b/models/chapters/edit.go index f3816ce..fddefb7 100644 --- a/models/chapters/edit.go +++ b/models/chapters/edit.go @@ -9,7 +9,7 @@ import ( // Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to // change, this functill will due to EditedDate. -func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time) (models.Chapter, error) { +func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time, commentMode *models.ChapterCommentMode, commentsLocked *bool) (models.Chapter, error) { now := time.Now() changes := bson.M{"editedDate": now} @@ -28,6 +28,14 @@ func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Tim changes["fictionalDate"] = *fictionalDate edited.FictionalDate = *fictionalDate } + if commentMode != nil && *commentMode != chapter.CommentMode { + changes["commentMode"] = *commentMode + edited.CommentMode = *commentMode + } + if commentsLocked != nil && *commentsLocked != chapter.CommentsLocked { + changes["commentsLocked"] = *commentsLocked + edited.CommentsLocked = *commentsLocked + } err := collection.UpdateId(chapter.ID, bson.M{"$set": changes}) if err != nil {