article.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. package repository
  2. import (
  3. "context"
  4. sq "github.com/Masterminds/squirrel"
  5. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/helper/errors"
  6. "git.dmitriygnatenko.ru/dima/dmitriygnatenko-v2/internal/model"
  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 model.LanguageID,
  31. ) ([]model.ArticlePreview, error) {
  32. var res []model.ArticlePreview
  33. q, v, err := sq.Select("id", "url", "publish_time", "title", "preview_text", "image").
  34. From(articleTableName).
  35. PlaceholderFormat(placeholder).
  36. Where(sq.Eq{"is_active": true, "language": uint64(lang)}).
  37. OrderBy("publish_time DESC").
  38. ToSql()
  39. if err != nil {
  40. return nil, errors.Wrap(err, "build query")
  41. }
  42. err = a.db.SelectContext(ctx, &res, q, v...)
  43. if err != nil {
  44. return nil, errors.Wrap(err, "select")
  45. }
  46. return res, nil
  47. }
  48. func (a ArticleRepository) GetAll(ctx context.Context) ([]model.Article, error) {
  49. var res []model.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, errors.Wrap(err, "build query")
  56. }
  57. err = a.db.SelectContext(ctx, &res, q, v...)
  58. if err != nil {
  59. return nil, errors.Wrap(err, "select")
  60. }
  61. return res, nil
  62. }
  63. func (a ArticleRepository) GetPreviewByTagID(
  64. ctx context.Context,
  65. tagID uint64,
  66. lang model.LanguageID,
  67. ) ([]model.ArticlePreview, error) {
  68. var res []model.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, errors.Wrap(err, "select")
  76. }
  77. return res, nil
  78. }
  79. func (a ArticleRepository) GetByURL(
  80. ctx context.Context,
  81. url string,
  82. lang model.LanguageID,
  83. ) (*model.Article, error) {
  84. var res model.Article
  85. q, v, err := sq.Select(articleTableColumns...).
  86. From(articleTableName).
  87. PlaceholderFormat(placeholder).
  88. Where(sq.Eq{"url": url, "language": lang}).
  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(
  101. ctx context.Context,
  102. id uint64,
  103. ) (*model.Article, error) {
  104. var res model.Article
  105. q, v, err := sq.Select(articleTableColumns...).
  106. From(articleTableName).
  107. PlaceholderFormat(placeholder).
  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 model.Article,
  123. ) (uint64, error) {
  124. q, v, err := sq.Insert(articleTableName).
  125. PlaceholderFormat(placeholder).
  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. ToSql()
  150. if err != nil {
  151. return 0, errors.Wrap(err, "build query")
  152. }
  153. res, err := a.db.ExecContext(ctx, q, v...)
  154. if err != nil {
  155. return 0, errors.Wrap(err, "exec")
  156. }
  157. id, err := res.LastInsertId()
  158. if err != nil {
  159. return 0, errors.Wrap(err, "can't get last insert id")
  160. }
  161. return uint64(id), nil
  162. }
  163. func (a ArticleRepository) Update(
  164. ctx context.Context,
  165. req model.Article,
  166. ) error {
  167. q, v, err := sq.Update(articleTableName).
  168. PlaceholderFormat(placeholder).
  169. Set("url", req.URL).
  170. Set("publish_time", req.PublishTime).
  171. Set("title", req.Title).
  172. Set("image", req.Image).
  173. Set("text", req.Text).
  174. Set("preview_text", req.PreviewText).
  175. Set("meta_keywords", req.MetaKeywords).
  176. Set("meta_desc", req.MetaDescription).
  177. Set("is_active", req.IsActive).
  178. Set("language", req.Language).
  179. Where(sq.Eq{"id": req.ID}).
  180. ToSql()
  181. if err != nil {
  182. return errors.Wrap(err, "build query")
  183. }
  184. _, err = a.db.ExecContext(ctx, q, v...)
  185. if err != nil {
  186. return errors.Wrap(err, "exec")
  187. }
  188. return nil
  189. }
  190. func (a ArticleRepository) Delete(
  191. ctx context.Context,
  192. id uint64,
  193. ) error {
  194. q, v, err := sq.Delete(articleTableName).
  195. PlaceholderFormat(placeholder).
  196. Where(sq.Eq{"id": id}).
  197. ToSql()
  198. if err != nil {
  199. return errors.Wrap(err, "build query")
  200. }
  201. _, err = a.db.ExecContext(ctx, q, v...)
  202. if err != nil {
  203. return errors.Wrap(err, "exec")
  204. }
  205. return nil
  206. }