article.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.ArticleRepository -o ./mocks/ -s "_minimock.go"
  5. import (
  6. "context"
  7. "database/sql"
  8. sq "github.com/Masterminds/squirrel"
  9. "github.com/dmitriygnatenko/internal/interfaces"
  10. "github.com/dmitriygnatenko/internal/models"
  11. )
  12. const articleTableName = "article"
  13. type articleRepository struct {
  14. db *sql.DB
  15. }
  16. func InitArticleRepository(db *sql.DB) interfaces.ArticleRepository {
  17. return articleRepository{db: db}
  18. }
  19. func (a articleRepository) GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error) {
  20. var res []models.ArticlePreview
  21. query, args, err := sq.Select("id", "url", "publish_time", "title", "preview_text", "image").
  22. From(articleTableName).
  23. PlaceholderFormat(sq.Dollar).
  24. Where(sq.Eq{"is_active": true}).
  25. OrderBy("publish_time DESC").
  26. ToSql()
  27. if err != nil {
  28. return nil, err
  29. }
  30. rows, err := a.db.QueryContext(ctx, query, args...)
  31. if err != nil {
  32. return nil, err
  33. }
  34. defer rows.Close()
  35. for rows.Next() {
  36. row := models.ArticlePreview{}
  37. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.PreviewText, &row.Image)
  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, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  51. From(articleTableName).
  52. OrderBy("publish_time DESC").
  53. ToSql()
  54. rows, err := a.db.QueryContext(ctx, query, args...)
  55. if err != nil {
  56. return nil, err
  57. }
  58. defer rows.Close()
  59. for rows.Next() {
  60. row := models.Article{}
  61. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.Image, &row.Text, &row.PreviewText, &row.MetaKeywords, &row.MetaDescription, &row.IsActive)
  62. if err != nil {
  63. return nil, err
  64. }
  65. res = append(res, row)
  66. }
  67. if err = rows.Err(); err != nil {
  68. return nil, err
  69. }
  70. return res, nil
  71. }
  72. func (a articleRepository) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
  73. var res []models.ArticlePreview
  74. query := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
  75. "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
  76. "WHERE a.is_active = true AND at.article_id = a.id AND at.tag_id = $1 " +
  77. "ORDER BY a.publish_time DESC"
  78. rows, err := a.db.QueryContext(ctx, query, tagID)
  79. if err != nil {
  80. return nil, err
  81. }
  82. defer rows.Close()
  83. for rows.Next() {
  84. row := models.ArticlePreview{}
  85. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.PreviewText, &row.Image)
  86. if err != nil {
  87. return nil, err
  88. }
  89. res = append(res, row)
  90. }
  91. if err = rows.Err(); err != nil {
  92. return nil, err
  93. }
  94. return res, nil
  95. }
  96. func (a articleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
  97. var res models.Article
  98. query, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  99. From(articleTableName).
  100. PlaceholderFormat(sq.Dollar).
  101. Where(sq.Eq{"url": url}).
  102. Limit(1).
  103. ToSql()
  104. if err != nil {
  105. return nil, err
  106. }
  107. err = a.db.QueryRowContext(ctx, query, args...).
  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, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  117. From(articleTableName).
  118. PlaceholderFormat(sq.Dollar).
  119. Where(sq.Eq{"id": id}).
  120. Limit(1).
  121. ToSql()
  122. if err != nil {
  123. return nil, err
  124. }
  125. err = a.db.QueryRowContext(ctx, query, args...).
  126. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  127. if err != nil {
  128. return nil, err
  129. }
  130. return &res, nil
  131. }
  132. func (a articleRepository) Add(ctx context.Context, m models.Article) (int, error) {
  133. query, args, err := sq.Insert(articleTableName).
  134. PlaceholderFormat(sq.Dollar).
  135. Columns("url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  136. Values(m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText, m.MetaKeywords, m.MetaDescription, m.IsActive).
  137. Suffix("RETURNING id").
  138. ToSql()
  139. if err != nil {
  140. return 0, err
  141. }
  142. var id int
  143. err = a.db.QueryRowContext(ctx, query, args...).Scan(&id)
  144. if err != nil {
  145. return 0, err
  146. }
  147. return id, nil
  148. }
  149. func (a articleRepository) Update(ctx context.Context, req models.Article) error {
  150. query, args, err := sq.Update(articleTableName).
  151. PlaceholderFormat(sq.Dollar).
  152. Set("url", req.URL).
  153. Set("publish_time", req.PublishTime).
  154. Set("title", req.Title).
  155. Set("image", req.Image).
  156. Set("text", req.Text).
  157. Set("preview_text", req.PreviewText).
  158. Set("meta_keywords", req.MetaKeywords).
  159. Set("meta_desc", req.MetaDescription).
  160. Set("is_active", req.IsActive).
  161. Where(sq.Eq{"id": req.ID}).
  162. ToSql()
  163. if err != nil {
  164. return err
  165. }
  166. _, err = a.db.ExecContext(ctx, query, args...)
  167. return err
  168. }
  169. func (a articleRepository) Delete(ctx context.Context, id int) error {
  170. query, args, err := sq.Delete(articleTableName).
  171. PlaceholderFormat(sq.Dollar).
  172. Where(sq.Eq{"id": id}).
  173. ToSql()
  174. if err != nil {
  175. return err
  176. }
  177. _, err = a.db.ExecContext(ctx, query, args...)
  178. return err
  179. }