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.

113 lines
3.0 KiB

  1. package postgres
  2. import (
  3. "context"
  4. "database/sql"
  5. "git.aiterp.net/rpdata/api/database/postgres/psqlcore"
  6. "git.aiterp.net/rpdata/api/internal/generate"
  7. "git.aiterp.net/rpdata/api/models"
  8. )
  9. type commentRepository struct {
  10. insertWithIDs bool
  11. db *sql.DB
  12. }
  13. func (r *commentRepository) Find(ctx context.Context, id string) (*models.Comment, error) {
  14. comment, err := psqlcore.New(r.db).SelectComment(ctx, id)
  15. if err != nil {
  16. return nil, err
  17. }
  18. return r.comment(comment), nil
  19. }
  20. func (r *commentRepository) List(ctx context.Context, filter models.CommentFilter) ([]*models.Comment, error) {
  21. params := psqlcore.SelectCommentsParams{
  22. ChapterID: "",
  23. LimitSize: 0,
  24. }
  25. if filter.ChapterID != nil {
  26. params.ChapterID = *filter.ChapterID
  27. }
  28. if filter.Limit > 0 {
  29. params.LimitSize = int32(filter.Limit)
  30. }
  31. comments, err := psqlcore.New(r.db).SelectComments(ctx, params)
  32. if err != nil {
  33. return nil, err
  34. }
  35. return r.comments(comments), nil
  36. }
  37. func (r *commentRepository) Insert(ctx context.Context, comment models.Comment) (*models.Comment, error) {
  38. if !r.insertWithIDs || len(comment.ID) < 8 {
  39. comment.ID = generate.CommentID()
  40. }
  41. err := psqlcore.New(r.db).InsertComment(ctx, psqlcore.InsertCommentParams{
  42. ID: comment.ID,
  43. ChapterID: comment.ChapterID,
  44. CharacterID: comment.CharacterID,
  45. Subject: comment.Subject,
  46. Author: comment.Author,
  47. CharacterName: comment.CharacterID,
  48. Source: comment.Source,
  49. CreatedDate: comment.CreatedDate.UTC(),
  50. FictionalDate: comment.FictionalDate.UTC(),
  51. EditedDate: comment.EditedDate.UTC(),
  52. })
  53. if err != nil {
  54. return nil, err
  55. }
  56. return &comment, nil
  57. }
  58. func (r *commentRepository) Update(ctx context.Context, comment models.Comment, update models.CommentUpdate) (*models.Comment, error) {
  59. comment.ApplyUpdate(update)
  60. err := psqlcore.New(r.db).UpdateComment(ctx, psqlcore.UpdateCommentParams{
  61. Subject: comment.Subject,
  62. Source: comment.Source,
  63. FictionalDate: comment.FictionalDate.UTC(),
  64. CharacterName: comment.ChapterID,
  65. CharacterID: comment.CharacterID,
  66. ID: comment.ID,
  67. })
  68. if err != nil {
  69. return nil, err
  70. }
  71. return &comment, nil
  72. }
  73. func (r *commentRepository) Delete(ctx context.Context, comment models.Comment) error {
  74. return psqlcore.New(r.db).DeleteComment(ctx, comment.ID)
  75. }
  76. func (r *commentRepository) comment(comment psqlcore.StoryComment) *models.Comment {
  77. return &models.Comment{
  78. ID: comment.ID,
  79. ChapterID: comment.ChapterID,
  80. Subject: comment.Subject,
  81. Author: comment.Author,
  82. CharacterName: comment.CharacterName,
  83. CharacterID: comment.CharacterID,
  84. FictionalDate: comment.FictionalDate,
  85. CreatedDate: comment.CreatedDate,
  86. EditedDate: comment.EditedDate,
  87. Source: comment.Source,
  88. }
  89. }
  90. func (r *commentRepository) comments(comments []psqlcore.StoryComment) []*models.Comment {
  91. results := make([]*models.Comment, 0, len(comments))
  92. for _, comment := range comments {
  93. results = append(results, r.comment(comment))
  94. }
  95. return results
  96. }