tag.go 4.0 KB

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