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.

40 lines
983 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, commentMode models.ChapterCommentMode) (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. CommentMode: commentMode,
  18. CommentsLocked: false,
  19. }
  20. if finctionalDate != nil {
  21. chapter.FictionalDate = *finctionalDate
  22. }
  23. err := collection.Insert(chapter)
  24. if err != nil {
  25. return models.Chapter{}, err
  26. }
  27. if createdDate.After(story.UpdatedDate) {
  28. if err := storyCollection.UpdateId(story.ID, bson.M{"$set": bson.M{"updatedDate": createdDate}}); err == nil {
  29. story.UpdatedDate = createdDate
  30. }
  31. }
  32. return chapter, nil
  33. }