article.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. // nolint
  51. query, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  52. From(articleTableName).
  53. OrderBy("publish_time DESC").
  54. ToSql()
  55. rows, err := a.db.QueryContext(ctx, query, args...)
  56. if err != nil {
  57. return nil, err
  58. }
  59. defer rows.Close()
  60. for rows.Next() {
  61. row := models.Article{}
  62. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.Image, &row.Text, &row.PreviewText, &row.MetaKeywords, &row.MetaDescription, &row.IsActive)
  63. if err != nil {
  64. return nil, err
  65. }
  66. res = append(res, row)
  67. }
  68. if err = rows.Err(); err != nil {
  69. return nil, err
  70. }
  71. return res, nil
  72. }
  73. func (a articleRepository) GetPreviewByTagID(ctx context.Context, tagID int) ([]models.ArticlePreview, error) {
  74. var res []models.ArticlePreview
  75. query := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
  76. "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
  77. "WHERE a.is_active = true AND at.article_id = a.id AND at.tag_id = $1 " +
  78. "ORDER BY a.publish_time DESC"
  79. rows, err := a.db.QueryContext(ctx, query, tagID)
  80. if err != nil {
  81. return nil, err
  82. }
  83. defer rows.Close()
  84. for rows.Next() {
  85. row := models.ArticlePreview{}
  86. err = rows.Scan(&row.ID, &row.URL, &row.PublishTime, &row.Title, &row.PreviewText, &row.Image)
  87. if err != nil {
  88. return nil, err
  89. }
  90. res = append(res, row)
  91. }
  92. if err = rows.Err(); err != nil {
  93. return nil, err
  94. }
  95. return res, nil
  96. }
  97. func (a articleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
  98. var res models.Article
  99. query, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  100. From(articleTableName).
  101. PlaceholderFormat(sq.Dollar).
  102. Where(sq.Eq{"url": url}).
  103. Limit(1).
  104. ToSql()
  105. if err != nil {
  106. return nil, err
  107. }
  108. err = a.db.QueryRowContext(ctx, query, args...).
  109. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  110. if err != nil {
  111. return nil, err
  112. }
  113. return &res, nil
  114. }
  115. func (a articleRepository) GetByID(ctx context.Context, id int) (*models.Article, error) {
  116. var res models.Article
  117. query, args, err := sq.Select("id", "url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  118. From(articleTableName).
  119. PlaceholderFormat(sq.Dollar).
  120. Where(sq.Eq{"id": id}).
  121. Limit(1).
  122. ToSql()
  123. if err != nil {
  124. return nil, err
  125. }
  126. err = a.db.QueryRowContext(ctx, query, args...).
  127. Scan(&res.ID, &res.URL, &res.PublishTime, &res.Title, &res.Image, &res.Text, &res.PreviewText, &res.MetaKeywords, &res.MetaDescription, &res.IsActive)
  128. if err != nil {
  129. return nil, err
  130. }
  131. return &res, nil
  132. }
  133. func (a articleRepository) Add(ctx context.Context, m models.Article) (int, error) {
  134. query, args, err := sq.Insert(articleTableName).
  135. PlaceholderFormat(sq.Dollar).
  136. Columns("url", "publish_time", "title", "image", "text", "preview_text", "meta_keywords", "meta_desc", "is_active").
  137. Values(m.URL, m.PublishTime, m.Title, m.Image, m.Text, m.PreviewText, m.MetaKeywords, m.MetaDescription, m.IsActive).
  138. Suffix("RETURNING id").
  139. ToSql()
  140. if err != nil {
  141. return 0, err
  142. }
  143. var id int
  144. err = a.db.QueryRowContext(ctx, query, args...).Scan(&id)
  145. if err != nil {
  146. return 0, err
  147. }
  148. return id, nil
  149. }
  150. func (a articleRepository) Update(ctx context.Context, req models.Article) error {
  151. query, args, err := sq.Update(articleTableName).
  152. PlaceholderFormat(sq.Dollar).
  153. Set("url", req.URL).
  154. Set("publish_time", req.PublishTime).
  155. Set("title", req.Title).
  156. Set("image", req.Image).
  157. Set("text", req.Text).
  158. Set("preview_text", req.PreviewText).
  159. Set("meta_keywords", req.MetaKeywords).
  160. Set("meta_desc", req.MetaDescription).
  161. Set("is_active", req.IsActive).
  162. Where(sq.Eq{"id": req.ID}).
  163. ToSql()
  164. if err != nil {
  165. return err
  166. }
  167. _, err = a.db.ExecContext(ctx, query, args...)
  168. return err
  169. }
  170. func (a articleRepository) Delete(ctx context.Context, id int) error {
  171. query, args, err := sq.Delete(articleTableName).
  172. PlaceholderFormat(sq.Dollar).
  173. Where(sq.Eq{"id": id}).
  174. ToSql()
  175. if err != nil {
  176. return err
  177. }
  178. _, err = a.db.ExecContext(ctx, query, args...)
  179. return err
  180. }