article.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. "language",
  21. }
  22. type ArticleRepository struct {
  23. db DB
  24. }
  25. func InitArticleRepository(db DB) *ArticleRepository {
  26. return &ArticleRepository{db: db}
  27. }
  28. func (a ArticleRepository) GetAllPreview(
  29. ctx context.Context,
  30. lang models.Language,
  31. ) ([]models.ArticlePreview, error) {
  32. var res []models.ArticlePreview
  33. q, v, err := sq.Select("id", "url", "publish_time", "title", "preview_text", "image").
  34. From(articleTableName).
  35. PlaceholderFormat(sq.Question).
  36. Where(sq.Eq{"is_active": true, "language": uint64(lang)}).
  37. OrderBy("publish_time DESC").
  38. ToSql()
  39. if err != nil {
  40. return nil, fmt.Errorf("build query: %w", err)
  41. }
  42. err = a.db.SelectContext(ctx, &res, q, v...)
  43. if err != nil {
  44. return nil, fmt.Errorf("select: %w", err)
  45. }
  46. return res, nil
  47. }
  48. func (a ArticleRepository) GetAll(ctx context.Context) ([]models.Article, error) {
  49. var res []models.Article
  50. q, v, err := sq.Select(articleTableColumns...).
  51. From(articleTableName).
  52. OrderBy("publish_time DESC").
  53. ToSql()
  54. if err != nil {
  55. return nil, fmt.Errorf("build query: %w", err)
  56. }
  57. err = a.db.SelectContext(ctx, &res, q, v...)
  58. if err != nil {
  59. return nil, fmt.Errorf("select: %w", err)
  60. }
  61. return res, nil
  62. }
  63. func (a ArticleRepository) GetPreviewByTagID(
  64. ctx context.Context,
  65. tagID uint64,
  66. lang models.Language,
  67. ) ([]models.ArticlePreview, error) {
  68. var res []models.ArticlePreview
  69. q := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
  70. "FROM " + articleTableName + " a, " + articleTagTableName + " at " +
  71. "WHERE a.is_active = true AND a.language = ? AND at.article_id = a.id AND at.tag_id = ? " +
  72. "ORDER BY a.publish_time DESC"
  73. err := a.db.SelectContext(ctx, &res, q, uint64(lang), tagID)
  74. if err != nil {
  75. return nil, fmt.Errorf("select: %w", err)
  76. }
  77. return res, nil
  78. }
  79. func (a ArticleRepository) GetByURL(
  80. ctx context.Context,
  81. url string,
  82. lang models.Language,
  83. ) (*models.Article, error) {
  84. var res models.Article
  85. q, v, err := sq.Select(articleTableColumns...).
  86. From(articleTableName).
  87. PlaceholderFormat(sq.Question).
  88. Where(sq.Eq{
  89. "url": url,
  90. "language": uint64(lang),
  91. }).
  92. Limit(1).
  93. ToSql()
  94. if err != nil {
  95. return nil, fmt.Errorf("build query: %w", err)
  96. }
  97. err = a.db.GetContext(ctx, &res, q, v...)
  98. if err != nil {
  99. return nil, fmt.Errorf("get: %w", err)
  100. }
  101. return &res, nil
  102. }
  103. func (a ArticleRepository) GetByID(
  104. ctx context.Context,
  105. id uint64,
  106. ) (*models.Article, error) {
  107. var res models.Article
  108. q, v, err := sq.Select(articleTableColumns...).
  109. From(articleTableName).
  110. PlaceholderFormat(sq.Question).
  111. Where(sq.Eq{"id": id}).
  112. Limit(1).
  113. ToSql()
  114. if err != nil {
  115. return nil, fmt.Errorf("build query: %w", err)
  116. }
  117. err = a.db.GetContext(ctx, &res, q, v...)
  118. if err != nil {
  119. return nil, fmt.Errorf("get: %w", err)
  120. }
  121. return &res, nil
  122. }
  123. func (a ArticleRepository) Add(
  124. ctx context.Context,
  125. m models.Article,
  126. ) (uint64, error) {
  127. q, v, err := sq.Insert(articleTableName).
  128. PlaceholderFormat(sq.Question).
  129. Columns(
  130. "url",
  131. "publish_time",
  132. "title",
  133. "image",
  134. "text",
  135. "preview_text",
  136. "meta_keywords",
  137. "meta_desc",
  138. "is_active",
  139. "language",
  140. ).Values(
  141. m.URL,
  142. m.PublishTime,
  143. m.Title,
  144. m.Image,
  145. m.Text,
  146. m.PreviewText,
  147. m.MetaKeywords,
  148. m.MetaDescription,
  149. m.IsActive,
  150. m.Language,
  151. ).
  152. Suffix("RETURNING id").
  153. ToSql()
  154. if err != nil {
  155. return 0, fmt.Errorf("build query: %w", err)
  156. }
  157. var id uint64
  158. err = a.db.QueryRowContext(ctx, q, v...).Scan(&id)
  159. if err != nil {
  160. return 0, fmt.Errorf("query row: %w", err)
  161. }
  162. return id, nil
  163. }
  164. func (a ArticleRepository) Update(ctx context.Context, req models.Article) error {
  165. q, v, err := sq.Update(articleTableName).
  166. PlaceholderFormat(sq.Question).
  167. Set("url", req.URL).
  168. Set("publish_time", req.PublishTime).
  169. Set("title", req.Title).
  170. Set("image", req.Image).
  171. Set("text", req.Text).
  172. Set("preview_text", req.PreviewText).
  173. Set("meta_keywords", req.MetaKeywords).
  174. Set("meta_desc", req.MetaDescription).
  175. Set("is_active", req.IsActive).
  176. Where(sq.Eq{"id": req.ID}).
  177. ToSql()
  178. if err != nil {
  179. return fmt.Errorf("build query: %w", err)
  180. }
  181. _, err = a.db.ExecContext(ctx, q, v...)
  182. if err != nil {
  183. return fmt.Errorf("exec: %w", err)
  184. }
  185. return nil
  186. }
  187. func (a ArticleRepository) Delete(ctx context.Context, id uint64) error {
  188. q, v, err := sq.Delete(articleTableName).
  189. PlaceholderFormat(sq.Question).
  190. Where(sq.Eq{"id": id}).
  191. ToSql()
  192. if err != nil {
  193. return fmt.Errorf("build query: %w", err)
  194. }
  195. _, err = a.db.ExecContext(ctx, q, v...)
  196. if err != nil {
  197. return fmt.Errorf("exec: %w", err)
  198. }
  199. return nil
  200. }