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) (models.Chapter, error) { chapter := models.Chapter{ ID: makeChapterID(), StoryID: story.ID, Title: title, Author: author, Source: source, CreatedDate: createdDate, EditedDate: createdDate, } 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 }