Browse Source

models, graph2: Added comment options to chapter mutations.

module-madness-pointers
Gisle Aune 6 years ago
parent
commit
f9fe0bb5f8
  1. 9
      graph2/queries/chapter.go
  2. 9
      graph2/schema/types/Chapter.gql
  3. 4
      models/chapters/add.go
  4. 10
      models/chapters/edit.go

9
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") 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 { if err != nil {
return models.Chapter{}, errors.New("Failed to create chapter: " + err.Error()) 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{} 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 { if err != nil {
return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error()) return models.Chapter{}, errors.New("Failed to edit chapter: " + err.Error())
} }

9
graph2/schema/types/Chapter.gql

@ -70,6 +70,9 @@ input ChapterAddInput {
# Optionally, assign a fictional date for the chapter # Optionally, assign a fictional date for the chapter
fictionalDate: Date fictionalDate: Date
"The comment mode for the chapter."
commentMode: ChapterCommentMode
} }
# Input for editChapter mutation # Input for editChapter mutation
@ -88,6 +91,12 @@ input ChapterEditInput {
# Remove the fictional date for a chapter # Remove the fictional date for a chapter
clearFictionalDate: Boolean clearFictionalDate: Boolean
"Set the comment mode for the chapter"
commentMode: ChapterCommentMode
"Set whether the chapter comments are locked"
commentsLocked: Boolean
} }
# Input for moveChapter mutation # Input for moveChapter mutation

4
models/chapters/add.go

@ -8,7 +8,7 @@ import (
) )
// Add adds a new chapter. // 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{ chapter := models.Chapter{
ID: makeChapterID(), ID: makeChapterID(),
StoryID: story.ID, StoryID: story.ID,
@ -17,6 +17,8 @@ func Add(story models.Story, title, author, source string, createdDate time.Time
Source: source, Source: source,
CreatedDate: createdDate, CreatedDate: createdDate,
EditedDate: createdDate, EditedDate: createdDate,
CommentMode: commentMode,
CommentsLocked: false,
} }
if finctionalDate != nil { if finctionalDate != nil {

10
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 // Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to
// change, this functill will due to EditedDate. // 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() now := time.Now()
changes := bson.M{"editedDate": now} changes := bson.M{"editedDate": now}
@ -28,6 +28,14 @@ func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Tim
changes["fictionalDate"] = *fictionalDate changes["fictionalDate"] = *fictionalDate
edited.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}) err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
if err != nil { if err != nil {

Loading…
Cancel
Save