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
867 B

  1. package chapters
  2. import (
  3. "time"
  4. "git.aiterp.net/rpdata/api/models"
  5. "github.com/globalsign/mgo/bson"
  6. )
  7. // Add adds a new chapter.
  8. func Add(story models.Story, title, author, source string, createdDate time.Time, finctionalDate *time.Time) (models.Chapter, error) {
  9. chapter := models.Chapter{
  10. ID: makeChapterID(),
  11. StoryID: story.ID,
  12. Title: title,
  13. Author: author,
  14. Source: source,
  15. CreatedDate: createdDate,
  16. EditedDate: createdDate,
  17. }
  18. if finctionalDate != nil {
  19. chapter.FictionalDate = *finctionalDate
  20. }
  21. err := collection.Insert(chapter)
  22. if err != nil {
  23. return models.Chapter{}, err
  24. }
  25. if createdDate.After(story.UpdatedDate) {
  26. if err := storyCollection.UpdateId(story.ID, bson.M{"$set": bson.M{"updatedDate": createdDate}}); err == nil {
  27. story.UpdatedDate = createdDate
  28. }
  29. }
  30. return chapter, nil
  31. }