package chapters import ( "time" "git.aiterp.net/rpdata/api/models" "github.com/globalsign/mgo/bson" ) // Add adds a new chapter. 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, CommentMode: commentMode, CommentsLocked: false, } if finctionalDate != nil { chapter.FictionalDate = *finctionalDate } err := collection.Insert(chapter) if err != nil { return models.Chapter{}, err } if createdDate.After(story.UpdatedDate) { if err := storyCollection.UpdateId(story.ID, bson.M{"$set": bson.M{"updatedDate": createdDate}}); err == nil { story.UpdatedDate = createdDate } } return chapter, nil }