tag.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package repositories
  2. import (
  3. "context"
  4. "database/sql"
  5. "github.com/dmitriygnatenko/internal/interfaces"
  6. "github.com/dmitriygnatenko/internal/models"
  7. )
  8. type tagRepository struct {
  9. db *sql.DB
  10. }
  11. func InitTagRepository(db *sql.DB) interfaces.ITagRepository {
  12. return tagRepository{db: db}
  13. }
  14. func (t tagRepository) GetAllUsed(ctx context.Context) ([]models.Tag, error) {
  15. var res []models.Tag
  16. query := "SELECT t.id, t.url, t.tag " +
  17. "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
  18. "WHERE t.id = at.tag_id AND at.article_id IN" +
  19. "(SELECT id FROM " + articleTableName + " " + "WHERE is_active = 1) " +
  20. "GROUP BY t.id"
  21. rows, err := t.db.QueryContext(ctx, query)
  22. if err != nil {
  23. return nil, err
  24. }
  25. defer rows.Close()
  26. for rows.Next() {
  27. row := models.Tag{}
  28. err = rows.Scan(
  29. &row.ID,
  30. &row.URL,
  31. &row.Tag,
  32. )
  33. if err != nil {
  34. return nil, err
  35. }
  36. res = append(res, row)
  37. }
  38. err = rows.Err()
  39. if err != nil {
  40. return nil, err
  41. }
  42. return res, nil
  43. }
  44. func (t tagRepository) GetByURL(ctx context.Context, tag string) (*models.Tag, error) {
  45. var res models.Tag
  46. query := "SELECT id, url, tag " +
  47. "FROM " + tagTableName + " " +
  48. "WHERE url = ? " +
  49. "LIMIT 1"
  50. err := t.db.QueryRowContext(ctx, query, tag).
  51. Scan(&res.ID, &res.URL, &res.Tag)
  52. if err != nil {
  53. return nil, err
  54. }
  55. return &res, nil
  56. }
  57. func (t tagRepository) GetByID(ctx context.Context, ID int) (*models.Tag, error) {
  58. var res models.Tag
  59. query := "SELECT id, url, tag " +
  60. "FROM " + tagTableName + " " +
  61. "WHERE id = ? " +
  62. "LIMIT 1"
  63. err := t.db.QueryRowContext(ctx, query, ID).
  64. Scan(&res.ID, &res.URL, &res.Tag)
  65. if err != nil {
  66. return nil, err
  67. }
  68. return &res, nil
  69. }
  70. func (t tagRepository) GetAll(ctx context.Context) ([]models.Tag, error) {
  71. var res []models.Tag
  72. query := "SELECT id, url, tag FROM " + tagTableName
  73. rows, err := t.db.QueryContext(ctx, query)
  74. if err != nil {
  75. return nil, err
  76. }
  77. defer rows.Close()
  78. for rows.Next() {
  79. row := models.Tag{}
  80. err = rows.Scan(
  81. &row.ID,
  82. &row.URL,
  83. &row.Tag,
  84. )
  85. if err != nil {
  86. return nil, err
  87. }
  88. res = append(res, row)
  89. }
  90. err = rows.Err()
  91. if err != nil {
  92. return nil, err
  93. }
  94. return res, nil
  95. }
  96. func (t tagRepository) GetByArticleID(ctx context.Context, ID int) ([]models.Tag, error) {
  97. var res []models.Tag
  98. query := "SELECT t.id, t.url, t.tag " +
  99. "FROM " + articleTagTableName + " at, " + tagTableName + " t " +
  100. "WHERE t.id = at.tag_id AND at.article_id = ?"
  101. rows, err := t.db.QueryContext(ctx, query, ID)
  102. if err != nil {
  103. return nil, err
  104. }
  105. defer rows.Close()
  106. for rows.Next() {
  107. row := models.Tag{}
  108. err = rows.Scan(
  109. &row.ID,
  110. &row.URL,
  111. &row.Tag,
  112. )
  113. if err != nil {
  114. return nil, err
  115. }
  116. res = append(res, row)
  117. }
  118. err = rows.Err()
  119. if err != nil {
  120. return nil, err
  121. }
  122. return res, nil
  123. }
  124. func (t tagRepository) IsUsed(ctx context.Context, ID int) (bool, error) {
  125. var count int
  126. query := "SELECT COUNT(tag_id) FROM " + articleTagTableName + " WHERE tag_id = ?"
  127. if err := t.db.QueryRowContext(ctx, query, ID).Scan(&count); err != nil {
  128. return false, err
  129. }
  130. return count > 0, nil
  131. }
  132. func (t tagRepository) Add(ctx context.Context, m models.Tag) error {
  133. query := "INSERT INTO " + tagTableName + " (tag, url) VALUES (?, ?)"
  134. _, err := t.db.ExecContext(ctx, query, m.Tag, m.URL)
  135. return err
  136. }
  137. func (t tagRepository) Update(ctx context.Context, m models.Tag) error {
  138. query := "UPDATE " + tagTableName + " SET tag = ?, url = ? WHERE id = ?"
  139. _, err := t.db.ExecContext(ctx, query, m.Tag, m.URL, m.ID)
  140. return err
  141. }
  142. func (t tagRepository) Delete(ctx context.Context, ID int) error {
  143. query := "DELETE FROM " + tagTableName + " WHERE id = ?"
  144. _, err := t.db.ExecContext(ctx, query, ID)
  145. return err
  146. }