tag.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. package repositories
  2. import (
  3. "context"
  4. "fmt"
  5. sq "github.com/Masterminds/squirrel"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/models"
  7. )
  8. const tagTableName = "tag"
  9. type TagRepository struct {
  10. db DB
  11. }
  12. func InitTagRepository(db DB) *TagRepository {
  13. return &TagRepository{db: db}
  14. }
  15. func (t TagRepository) GetAllUsed(ctx context.Context) ([]models.Tag, error) {
  16. var res []models.Tag
  17. q := "SELECT t.id, t.url, t.tag " +
  18. "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
  19. "WHERE t.id = at.tag_id AND at.article_id IN " +
  20. "(SELECT id FROM " + articleTableName + " " + "WHERE is_active = true) " +
  21. "GROUP BY t.id"
  22. err := t.db.SelectContext(ctx, &res, q)
  23. if err != nil {
  24. return nil, fmt.Errorf("select: %w", err)
  25. }
  26. return res, nil
  27. }
  28. func (t TagRepository) GetByURL(ctx context.Context, url string) (*models.Tag, error) {
  29. var res models.Tag
  30. q, v, err := sq.Select("id", "url", "tag").
  31. From(tagTableName).
  32. PlaceholderFormat(sq.Dollar).
  33. Where(sq.Eq{"url": url}).
  34. Limit(1).
  35. ToSql()
  36. if err != nil {
  37. return nil, fmt.Errorf("build query: %w", err)
  38. }
  39. err = t.db.GetContext(ctx, &res, q, v...)
  40. if err != nil {
  41. return nil, fmt.Errorf("get: %w", err)
  42. }
  43. return &res, nil
  44. }
  45. func (t TagRepository) GetByID(ctx context.Context, id uint64) (*models.Tag, error) {
  46. var res models.Tag
  47. q, v, err := sq.Select("id", "url", "tag").
  48. From(tagTableName).
  49. PlaceholderFormat(sq.Dollar).
  50. Where(sq.Eq{"id": id}).
  51. Limit(1).
  52. ToSql()
  53. if err != nil {
  54. return nil, fmt.Errorf("build query: %w", err)
  55. }
  56. err = t.db.GetContext(ctx, &res, q, v...)
  57. if err != nil {
  58. return nil, fmt.Errorf("get: %w", err)
  59. }
  60. return &res, nil
  61. }
  62. func (t TagRepository) GetAll(ctx context.Context) ([]models.Tag, error) {
  63. var res []models.Tag
  64. q, v, err := sq.Select("id", "url", "tag").
  65. From(tagTableName).
  66. ToSql()
  67. if err != nil {
  68. return nil, fmt.Errorf("build query: %w", err)
  69. }
  70. err = t.db.SelectContext(ctx, &res, q, v...)
  71. if err != nil {
  72. return nil, fmt.Errorf("select: %w", err)
  73. }
  74. return res, nil
  75. }
  76. func (t TagRepository) GetByArticleID(ctx context.Context, id uint64) ([]models.Tag, error) {
  77. var res []models.Tag
  78. q := "SELECT t.id, t.url, t.tag " +
  79. "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
  80. "WHERE t.id = at.tag_id AND at.article_id = $1"
  81. err := t.db.SelectContext(ctx, &res, q, id)
  82. if err != nil {
  83. return nil, fmt.Errorf("select: %w", err)
  84. }
  85. return res, nil
  86. }
  87. func (t TagRepository) IsUsed(ctx context.Context, id uint64) (bool, error) {
  88. query, args, err := sq.Select("COUNT(tag_id)").
  89. From(articleTagTableName).
  90. PlaceholderFormat(sq.Dollar).
  91. Where(sq.Eq{"tag_id": id}).
  92. ToSql()
  93. if err != nil {
  94. return false, fmt.Errorf("build query: %w", err)
  95. }
  96. var count int
  97. err = t.db.QueryRowContext(ctx, query, args...).Scan(&count)
  98. if err != nil {
  99. return false, err
  100. }
  101. return count > 0, nil
  102. }
  103. func (t TagRepository) Add(ctx context.Context, req models.Tag) error {
  104. q, v, err := sq.Insert(tagTableName).
  105. PlaceholderFormat(sq.Dollar).
  106. Columns("tag", "url").
  107. Values(req.Tag, req.URL).
  108. ToSql()
  109. if err != nil {
  110. return fmt.Errorf("build query: %w", err)
  111. }
  112. _, err = t.db.ExecContext(ctx, q, v...)
  113. if err != nil {
  114. return fmt.Errorf("exec: %w", err)
  115. }
  116. return nil
  117. }
  118. func (t TagRepository) Update(ctx context.Context, req models.Tag) error {
  119. q, v, err := sq.Update(tagTableName).
  120. PlaceholderFormat(sq.Dollar).
  121. Set("tag", req.Tag).
  122. Set("url", req.URL).
  123. Where(sq.Eq{"id": req.ID}).
  124. ToSql()
  125. if err != nil {
  126. return fmt.Errorf("build query: %w", err)
  127. }
  128. _, err = t.db.ExecContext(ctx, q, v...)
  129. if err != nil {
  130. return fmt.Errorf("exec: %w", err)
  131. }
  132. return nil
  133. }
  134. func (t TagRepository) Delete(ctx context.Context, id uint64) error {
  135. q, v, err := sq.Delete(tagTableName).
  136. PlaceholderFormat(sq.Dollar).
  137. Where(sq.Eq{"id": id}).
  138. ToSql()
  139. if err != nil {
  140. return fmt.Errorf("build query: %w", err)
  141. }
  142. _, err = t.db.ExecContext(ctx, q, v...)
  143. if err != nil {
  144. return fmt.Errorf("exec: %w", err)
  145. }
  146. return nil
  147. }