article.go 4.9 KB

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