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.

114 lines
3.1 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.CharacterName,
  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. EditedDate: comment.EditedDate.UTC(),
  65. CharacterName: comment.CharacterName,
  66. CharacterID: comment.CharacterID,
  67. ID: comment.ID,
  68. })
  69. if err != nil {
  70. return nil, err
  71. }
  72. return &comment, nil
  73. }
  74. func (r *commentRepository) Delete(ctx context.Context, comment models.Comment) error {
  75. return psqlcore.New(r.db).DeleteComment(ctx, comment.ID)
  76. }
  77. func (r *commentRepository) comment(comment psqlcore.StoryComment) *models.Comment {
  78. return &models.Comment{
  79. ID: comment.ID,
  80. ChapterID: comment.ChapterID,
  81. Subject: comment.Subject,
  82. Author: comment.Author,
  83. CharacterName: comment.CharacterName,
  84. CharacterID: comment.CharacterID,
  85. FictionalDate: comment.FictionalDate,
  86. CreatedDate: comment.CreatedDate,
  87. EditedDate: comment.EditedDate,
  88. Source: comment.Source,
  89. }
  90. }
  91. func (r *commentRepository) comments(comments []psqlcore.StoryComment) []*models.Comment {
  92. results := make([]*models.Comment, 0, len(comments))
  93. for _, comment := range comments {
  94. results = append(results, r.comment(comment))
  95. }
  96. return results
  97. }