tag.go 3.9 KB

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