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.

129 lines
3.3 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/models"
  7. )
  8. type storyRepository struct {
  9. insertWithIDs bool
  10. db *sql.DB
  11. }
  12. func (r *storyRepository) Find(ctx context.Context, id string) (*models.Story, error) {
  13. q := psqlcore.New(r.db)
  14. story, err := q.SelectStory(ctx, id)
  15. if err != nil {
  16. return nil, err
  17. }
  18. tags, err := q.SelectTagsByTarget(ctx, psqlcore.SelectTagsByTargetParams{
  19. TargetKind: "Story",
  20. TargetID: story.ID,
  21. })
  22. if err != nil && err != sql.ErrNoRows {
  23. return nil, err
  24. }
  25. return r.story(story, tags), nil
  26. }
  27. func (r *storyRepository) List(ctx context.Context, filter models.StoryFilter) ([]*models.Story, error) {
  28. q := psqlcore.New(r.db)
  29. params := psqlcore.SelectStoriesParams{LimitSize: 100}
  30. if len(filter.Tags) > 0 {
  31. params.FilterID = true
  32. if len(filter.Tags) == 1 {
  33. tags, err := q.SelectTagsByKindName(ctx, psqlcore.SelectTagsByKindNameParams{
  34. Kind: string(filter.Tags[0].Kind),
  35. Name: filter.Tags[0].Name,
  36. })
  37. if err != nil && err != sql.ErrNoRows {
  38. return nil, err
  39. }
  40. for _, tag := range tags {
  41. params.Ids = append(params.Ids, tag.TargetID)
  42. }
  43. } else {
  44. }
  45. if len(params.Ids) == 0 {
  46. return []*models.Story{}, nil
  47. }
  48. }
  49. if filter.Limit > 0 {
  50. params.LimitSize = 1000000
  51. }
  52. panic("implement me")
  53. }
  54. func (r *storyRepository) Insert(ctx context.Context, story models.Story) (*models.Story, error) {
  55. panic("implement me")
  56. }
  57. func (r *storyRepository) Update(ctx context.Context, story models.Story, update models.StoryUpdate) (*models.Story, error) {
  58. panic("implement me")
  59. }
  60. func (r *storyRepository) AddTag(ctx context.Context, story models.Story, tag models.Tag) error {
  61. return psqlcore.New(r.db).SetTag(ctx, psqlcore.SetTagParams{
  62. Kind: string(tag.Kind),
  63. Name: tag.Name,
  64. TargetKind: "Story",
  65. TargetID: story.ID,
  66. })
  67. }
  68. func (r *storyRepository) RemoveTag(ctx context.Context, story models.Story, tag models.Tag) error {
  69. return psqlcore.New(r.db).ClearTag(ctx, psqlcore.ClearTagParams{
  70. Kind: string(tag.Kind),
  71. Name: tag.Name,
  72. TargetKind: "Story",
  73. TargetID: story.ID,
  74. })
  75. }
  76. func (r *storyRepository) Delete(ctx context.Context, story models.Story) error {
  77. panic("implement me")
  78. }
  79. func (r *storyRepository) story(story psqlcore.Story, tags []psqlcore.CommonTag) *models.Story {
  80. tags2 := make([]models.Tag, 0, 8)
  81. for _, tag := range tags {
  82. if tag.TargetKind == "Story" && tag.TargetID == story.ID {
  83. tags2 = append(tags2, models.Tag{
  84. Kind: models.TagKind(tag.Kind),
  85. Name: tag.Name,
  86. })
  87. }
  88. }
  89. return &models.Story{
  90. ID: story.ID,
  91. Author: story.Author,
  92. Name: story.Name,
  93. Category: models.StoryCategory(story.Category),
  94. Open: story.Open,
  95. Listed: story.Listed,
  96. Tags: tags2,
  97. CreatedDate: story.CreatedDate,
  98. FictionalDate: story.FictionalDate,
  99. UpdatedDate: story.UpdatedDate,
  100. SortByFictionalDate: story.SortByFictionalDate,
  101. }
  102. }
  103. func (r *storyRepository) stories(stories []psqlcore.Story, tags []psqlcore.CommonTag) []*models.Story {
  104. results := make([]*models.Story, 0, len(stories))
  105. for _, story := range stories {
  106. results = append(results, r.story(story, tags))
  107. }
  108. return results
  109. }