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