article.go 5.0 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.IArticleRepository -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 articleRepository struct {
  12. db *sql.DB
  13. }
  14. func InitArticleRepository(db *sql.DB) interfaces.IArticleRepository {
  15. return articleRepository{db: db}
  16. }
  17. func (a articleRepository) GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error) {
  18. var res []models.ArticlePreview
  19. query := "SELECT id, url, publish_time, title, preview_text, image " +
  20. "FROM " + articleTableName + " " +
  21. "WHERE is_active = 1 " +
  22. "ORDER BY publish_time DESC"
  23. rows, err := a.db.QueryContext(ctx, query)
  24. if err != nil {
  25. return nil, err
  26. }
  27. defer rows.Close()
  28. for rows.Next() {
  29. row := models.ArticlePreview{}
  30. err = rows.Scan(
  31. &row.ID,
  32. &row.URL,
  33. &row.PublishTime,
  34. &row.Title,
  35. &row.PreviewText,
  36. &row.Image,
  37. )
  38. if err != nil {
  39. return nil, err
  40. }
  41. res = append(res, row)
  42. }
  43. if err = rows.Err(); err != nil {
  44. return nil, err
  45. }
  46. return res, nil
  47. }
  48. func (a articleRepository) GetAll(ctx context.Context) ([]models.Article, error) {
  49. var res []models.Article
  50. query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
  51. "FROM " + articleTableName + " " +
  52. "ORDER BY publish_time DESC"
  53. rows, err := a.db.QueryContext(ctx, query)
  54. if err != nil {
  55. return nil, err
  56. }
  57. defer rows.Close()
  58. for rows.Next() {
  59. row := models.Article{}
  60. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.Image, &row.Text, &row.PreviewText, &row.MetaKeywords, &row.MetaDescription, &row.IsActive)
  61. if err != nil {
  62. return nil, err
  63. }
  64. res = append(res, row)
  65. }
  66. if err = rows.Err(); err != nil {
  67. return nil, err
  68. }
  69. return res, nil
  70. }
  71. func (a articleRepository) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
  72. var res []models.ArticlePreview
  73. query := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
  74. "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
  75. "WHERE a.is_active = 1 AND at.article_id = a.id AND at.tag_id = ? " +
  76. "ORDER BY a.publish_time DESC"
  77. rows, err := a.db.QueryContext(ctx, query, tagID)
  78. if err != nil {
  79. return nil, err
  80. }
  81. defer rows.Close()
  82. for rows.Next() {
  83. row := models.ArticlePreview{}
  84. err = rows.Scan(
  85. &row.ID,
  86. &row.URL,
  87. &row.PublishTime,
  88. &row.Title,
  89. &row.PreviewText,
  90. &row.Image,
  91. )
  92. if err != nil {
  93. return nil, err
  94. }
  95. res = append(res, row)
  96. }
  97. if err = rows.Err(); err != nil {
  98. return nil, err
  99. }
  100. return res, nil
  101. }
  102. func (a articleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
  103. var res models.Article
  104. query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
  105. "FROM " + articleTableName + " " +
  106. "WHERE url = ? LIMIT 1"
  107. err := a.db.QueryRowContext(ctx, query, url).
  108. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  109. if err != nil {
  110. return nil, err
  111. }
  112. return &res, nil
  113. }
  114. func (a articleRepository) GetByID(ctx context.Context, id int) (*models.Article, error) {
  115. var res models.Article
  116. query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
  117. "FROM " + articleTableName + " " +
  118. "WHERE id = ? LIMIT 1"
  119. err := a.db.QueryRowContext(ctx, query, id).
  120. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  121. if err != nil {
  122. return nil, err
  123. }
  124. return &res, nil
  125. }
  126. func (a articleRepository) Add(ctx context.Context, m models.Article) (int, error) {
  127. query := "INSERT INTO " + articleTableName + " " +
  128. "(url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active) " +
  129. "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)"
  130. res, err := a.db.ExecContext(ctx, query, m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText, m.MetaKeywords, m.MetaDescription, m.IsActive)
  131. if err != nil {
  132. return 0, err
  133. }
  134. id, err := res.LastInsertId()
  135. if err != nil {
  136. return 0, err
  137. }
  138. return int(id), err
  139. }
  140. func (a articleRepository) Update(ctx context.Context, m models.Article) error {
  141. query := "UPDATE " + articleTableName + " " +
  142. "SET url = ?, publish_time = ?, title = ?, image = ?, text = ?, preview_text = ?, meta_keywords = ?, " +
  143. " meta_desc = ?, is_active = ? WHERE id = ?"
  144. _, err := a.db.ExecContext(ctx, query, m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText,
  145. m.MetaKeywords, m.MetaDescription, m.IsActive, m.ID)
  146. return err
  147. }
  148. func (a articleRepository) Delete(ctx context.Context, id int) error {
  149. query := "DELETE FROM " + articleTableName + " WHERE id = ?"
  150. _, err := a.db.ExecContext(ctx, query, id)
  151. return err
  152. }