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.
45 lines
1.1 KiB
45 lines
1.1 KiB
package stories
|
|
|
|
import (
|
|
"time"
|
|
|
|
"git.aiterp.net/rpdata/api/models"
|
|
"github.com/globalsign/mgo/bson"
|
|
)
|
|
|
|
// Edit edits the story and returns the edited story if it succeeds.
|
|
func Edit(story models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (models.Story, error) {
|
|
changes := bson.M{}
|
|
|
|
if name != nil && *name != story.Name {
|
|
changes["name"] = *name
|
|
story.Name = *name
|
|
}
|
|
if category != nil && *category != story.Category {
|
|
changes["category"] = *category
|
|
story.Category = *category
|
|
}
|
|
if listed != nil && *listed != story.Listed {
|
|
changes["listed"] = *listed
|
|
story.Listed = *listed
|
|
}
|
|
if open != nil && *open != story.Open {
|
|
changes["open"] = *open
|
|
story.Open = *open
|
|
}
|
|
if fictionalDate != nil && !fictionalDate.Equal(story.FictionalDate) {
|
|
changes["fictionalDate"] = *fictionalDate
|
|
story.FictionalDate = *fictionalDate
|
|
}
|
|
|
|
if len(changes) == 0 {
|
|
return story, nil
|
|
}
|
|
|
|
err := collection.UpdateId(story.ID, bson.M{"$set": changes})
|
|
if err != nil {
|
|
return models.Story{}, err
|
|
}
|
|
|
|
return story, nil
|
|
}
|