article.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
  46. var res []models.ArticlePreview
  47. query := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
  48. "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
  49. "WHERE a.is_active = 1 AND at.article_id = a.id AND at.tag_id = ? " +
  50. "ORDER BY a.publish_time DESC"
  51. rows, err := a.db.QueryContext(ctx, query, tagID)
  52. if err != nil {
  53. return nil, err
  54. }
  55. defer rows.Close()
  56. for rows.Next() {
  57. row := models.ArticlePreview{}
  58. err = rows.Scan(
  59. &row.ID,
  60. &row.URL,
  61. &row.PublishTime,
  62. &row.Title,
  63. &row.PreviewText,
  64. &row.Image,
  65. )
  66. if err != nil {
  67. return nil, err
  68. }
  69. res = append(res, row)
  70. }
  71. if err = rows.Err(); err != nil {
  72. return nil, err
  73. }
  74. return res, nil
  75. }
  76. func (a articleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
  77. var res models.Article
  78. query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
  79. "FROM " + articleTableName + " " +
  80. "WHERE url = ? LIMIT 1"
  81. err := a.db.QueryRowContext(ctx, query, url).
  82. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  83. if err != nil {
  84. return nil, err
  85. }
  86. return &res, nil
  87. }
  88. func (a articleRepository) GetByID(ctx context.Context, id int) (*models.Article, error) {
  89. var res models.Article
  90. query := "SELECT id, url, publish_time, title, image, text, preview_text, meta_keywords, meta_desc, is_active " +
  91. "FROM " + articleTableName + " " +
  92. "WHERE id = ? LIMIT 1"
  93. err := a.db.QueryRowContext(ctx, query, id).
  94. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return &res, nil
  99. }