GraphQL API and utilities for the rpdata project
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

  1. package stories
  2. import (
  3. "time"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Edit edits the story and returns the edited story if it succeeds.
  8. func Edit(story models.Story, name *string, category *models.StoryCategory, listed, open *bool, fictionalDate *time.Time) (models.Story, error) {
  9. changes := bson.M{}
  10. if name != nil && *name != story.Name {
  11. changes["name"] = *name
  12. story.Name = *name
  13. }
  14. if category != nil && *category != story.Category {
  15. changes["category"] = *category
  16. story.Category = *category
  17. }
  18. if listed != nil && *listed != story.Listed {
  19. changes["listed"] = *listed
  20. story.Listed = *listed
  21. }
  22. if open != nil && *open != story.Open {
  23. changes["open"] = *open
  24. story.Open = *open
  25. }
  26. if fictionalDate != nil && !fictionalDate.Equal(story.FictionalDate) {
  27. changes["fictionalDate"] = *fictionalDate
  28. story.FictionalDate = *fictionalDate
  29. }
  30. if len(changes) == 0 {
  31. return story, nil
  32. }
  33. err := collection.UpdateId(story.ID, bson.M{"$set": changes})
  34. if err != nil {
  35. return models.Story{}, err
  36. }
  37. return story, nil
  38. }