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.
46 lines
1.3 KiB
46 lines
1.3 KiB
package chapters
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"github.com/globalsign/mgo/bson"
|
|
)
|
|
|
|
// 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, commentMode *models.ChapterCommentMode, commentsLocked *bool) (models.Chapter, error) {
|
|
now := time.Now()
|
|
changes := bson.M{"editedDate": now}
|
|
|
|
edited := chapter
|
|
edited.EditedDate = now
|
|
|
|
if title != nil && *title != chapter.Title {
|
|
changes["title"] = *title
|
|
edited.Title = *title
|
|
}
|
|
if source != nil && *source != chapter.Source {
|
|
changes["source"] = *source
|
|
edited.Source = *source
|
|
}
|
|
if fictionalDate != nil && !fictionalDate.Equal(chapter.FictionalDate) {
|
|
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 {
|
|
return chapter, err
|
|
}
|
|
|
|
return edited, nil
|
|
}
|