article.go 4.6 KB

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