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.

46 lines
1.3 KiB

  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, commentMode *models.ChapterCommentMode, commentsLocked *bool) (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. if commentMode != nil && *commentMode != chapter.CommentMode {
  27. changes["commentMode"] = *commentMode
  28. edited.CommentMode = *commentMode
  29. }
  30. if commentsLocked != nil && *commentsLocked != chapter.CommentsLocked {
  31. changes["commentsLocked"] = *commentsLocked
  32. edited.CommentsLocked = *commentsLocked
  33. }
  34. err := collection.UpdateId(chapter.ID, bson.M{"$set": changes})
  35. if err != nil {
  36. return chapter, err
  37. }
  38. return edited, nil
  39. }