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.

134 lines
3.1 KiB

  1. package mongodb
  2. import (
  3. "context"
  4. "git.aiterp.net/rpdata/api/internal/generate"
  5. "git.aiterp.net/rpdata/api/models"
  6. "git.aiterp.net/rpdata/api/repositories"
  7. "github.com/globalsign/mgo"
  8. "github.com/globalsign/mgo/bson"
  9. "log"
  10. )
  11. type commentRepository struct {
  12. comments *mgo.Collection
  13. }
  14. func newCommentRepository(db *mgo.Database) (repositories.CommentRepository, error) {
  15. collection := db.C("story.comments")
  16. err := collection.EnsureIndexKey("chapterId")
  17. if err != nil {
  18. return nil, err
  19. }
  20. err = collection.EnsureIndexKey("author")
  21. if err != nil {
  22. return nil, err
  23. }
  24. err = collection.EnsureIndexKey("createdDate")
  25. if err != nil {
  26. return nil, err
  27. }
  28. r := &commentRepository{
  29. comments: collection,
  30. }
  31. go r.fixFieldTypo()
  32. return r, nil
  33. }
  34. func (r *commentRepository) Find(ctx context.Context, id string) (*models.Comment, error) {
  35. comment := new(models.Comment)
  36. err := r.comments.FindId(id).One(comment)
  37. if err != nil {
  38. return nil, err
  39. }
  40. return comment, nil
  41. }
  42. func (r *commentRepository) List(ctx context.Context, filter models.CommentFilter) ([]*models.Comment, error) {
  43. query := bson.M{}
  44. if filter.ChapterID != nil {
  45. query["chapterId"] = *filter.ChapterID
  46. }
  47. comments := make([]*models.Comment, 0, 32)
  48. err := r.comments.Find(query).Sort("createdDate").Limit(filter.Limit).All(&comments)
  49. if err != nil {
  50. if err == mgo.ErrNotFound {
  51. return comments, nil
  52. }
  53. return nil, err
  54. }
  55. return comments, nil
  56. }
  57. func (r *commentRepository) Insert(ctx context.Context, comment models.Comment) (*models.Comment, error) {
  58. comment.ID = generate.CommentID()
  59. err := r.comments.Insert(comment)
  60. if err != nil {
  61. return nil, err
  62. }
  63. return &comment, nil
  64. }
  65. func (r *commentRepository) Update(ctx context.Context, comment models.Comment, update models.CommentUpdate) (*models.Comment, error) {
  66. updateBson := bson.M{}
  67. if update.Subject != nil {
  68. updateBson["subject"] = *update.Subject
  69. comment.Subject = *update.Subject
  70. }
  71. if update.Source != nil {
  72. updateBson["source"] = *update.Source
  73. comment.Source = *update.Source
  74. }
  75. if update.FictionalDate != nil {
  76. updateBson["fictionalDate"] = *update.FictionalDate
  77. comment.FictionalDate = *update.FictionalDate
  78. }
  79. if update.CharacterID != nil {
  80. updateBson["characterId"] = *update.CharacterID
  81. comment.CharacterID = *update.CharacterID
  82. }
  83. if update.CharacterName != nil {
  84. updateBson["characterName"] = *update.CharacterName
  85. comment.CharacterName = *update.CharacterName
  86. }
  87. err := r.comments.UpdateId(comment.ID, bson.M{"$set": updateBson})
  88. if err != nil {
  89. return nil, err
  90. }
  91. return &comment, nil
  92. }
  93. func (r *commentRepository) Delete(ctx context.Context, comment models.Comment) error {
  94. return r.comments.RemoveId(comment.ID)
  95. }
  96. func (r *commentRepository) fixFieldTypo() {
  97. c, err := r.comments.UpdateAll(bson.M{
  98. "editeddDate": bson.M{"$ne": nil},
  99. }, bson.M{
  100. "$rename": bson.M{"editeddDate": "editedDate"},
  101. })
  102. if err != nil {
  103. if err == mgo.ErrNotFound {
  104. return
  105. }
  106. log.Println("Failed to run name typo fix:", err)
  107. return
  108. }
  109. if c.Updated > 0 {
  110. log.Println("Fixed editeddDate field name typo in", c.Updated, "comments")
  111. }
  112. }