You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
867 B
38 lines
867 B
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
|
|
}
|