Files
api/models/chapters/edit.go
T
2018-09-17 20:11:57 +02:00

39 lines
974 B
Go

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) (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
}
err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
if err != nil {
return chapter, err
}
return edited, nil
}