article.go 5.5 KB

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