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.

38 lines
974 B

  1. package chapters
  2. import (
  3. "time"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Edit edits a chapter, and updates EditedDate. While many Edit functions cheat if there's nothing to
  8. // change, this functill will due to EditedDate.
  9. func Edit(chapter models.Chapter, title, source *string, fictionalDate *time.Time) (models.Chapter, error) {
  10. now := time.Now()
  11. changes := bson.M{"editedDate": now}
  12. edited := chapter
  13. edited.EditedDate = now
  14. if title != nil && *title != chapter.Title {
  15. changes["title"] = *title
  16. edited.Title = *title
  17. }
  18. if source != nil && *source != chapter.Source {
  19. changes["source"] = *source
  20. edited.Source = *source
  21. }
  22. if fictionalDate != nil && !fictionalDate.Equal(chapter.FictionalDate) {
  23. changes["fictionalDate"] = *fictionalDate
  24. edited.FictionalDate = *fictionalDate
  25. }
  26. err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
  27. if err != nil {
  28. return chapter, err
  29. }
  30. return edited, nil
  31. }