|
@@ -22,6 +22,7 @@ var articleTableColumns = []string{
|
|
"meta_keywords",
|
|
"meta_keywords",
|
|
"meta_desc",
|
|
"meta_desc",
|
|
"is_active",
|
|
"is_active",
|
|
|
|
+ "language",
|
|
}
|
|
}
|
|
|
|
|
|
type ArticleRepository struct {
|
|
type ArticleRepository struct {
|
|
@@ -32,13 +33,16 @@ func InitArticleRepository(db DB) *ArticleRepository {
|
|
return &ArticleRepository{db: db}
|
|
return &ArticleRepository{db: db}
|
|
}
|
|
}
|
|
|
|
|
|
-func (a ArticleRepository) GetAllPreview(ctx context.Context) ([]models.ArticlePreview, error) {
|
|
|
|
|
|
+func (a ArticleRepository) GetAllPreview(
|
|
|
|
+ ctx context.Context,
|
|
|
|
+ lang models.Language,
|
|
|
|
+) ([]models.ArticlePreview, error) {
|
|
var res []models.ArticlePreview
|
|
var res []models.ArticlePreview
|
|
|
|
|
|
q, v, err := sq.Select("id", "url", "publish_time", "title", "preview_text", "image").
|
|
q, v, err := sq.Select("id", "url", "publish_time", "title", "preview_text", "image").
|
|
From(articleTableName).
|
|
From(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
- Where(sq.Eq{"is_active": true}).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
|
|
+ Where(sq.Eq{"is_active": true, "language": uint64(lang)}).
|
|
OrderBy("publish_time DESC").
|
|
OrderBy("publish_time DESC").
|
|
ToSql()
|
|
ToSql()
|
|
|
|
|
|
@@ -74,15 +78,19 @@ func (a ArticleRepository) GetAll(ctx context.Context) ([]models.Article, error)
|
|
return res, nil
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (a ArticleRepository) GetPreviewByTagID(ctx context.Context, tagID uint64) ([]models.ArticlePreview, error) {
|
|
|
|
|
|
+func (a ArticleRepository) GetPreviewByTagID(
|
|
|
|
+ ctx context.Context,
|
|
|
|
+ tagID uint64,
|
|
|
|
+ lang models.Language,
|
|
|
|
+) ([]models.ArticlePreview, error) {
|
|
var res []models.ArticlePreview
|
|
var res []models.ArticlePreview
|
|
|
|
|
|
q := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
|
|
q := "SELECT a.id, a.url, a.publish_time, a.title, a.preview_text, a.image " +
|
|
"FROM " + articleTableName + " a, " + articleTagTableName + " at " +
|
|
"FROM " + articleTableName + " a, " + articleTagTableName + " at " +
|
|
- "WHERE a.is_active = true AND at.article_id = a.id AND at.tag_id = $1 " +
|
|
|
|
|
|
+ "WHERE a.is_active = true AND a.language = ? AND at.article_id = a.id AND at.tag_id = ? " +
|
|
"ORDER BY a.publish_time DESC"
|
|
"ORDER BY a.publish_time DESC"
|
|
|
|
|
|
- err := a.db.SelectContext(ctx, &res, q, tagID)
|
|
|
|
|
|
+ err := a.db.SelectContext(ctx, &res, q, uint64(lang), tagID)
|
|
if err != nil {
|
|
if err != nil {
|
|
return nil, fmt.Errorf("select: %w", err)
|
|
return nil, fmt.Errorf("select: %w", err)
|
|
}
|
|
}
|
|
@@ -90,13 +98,20 @@ func (a ArticleRepository) GetPreviewByTagID(ctx context.Context, tagID uint64)
|
|
return res, nil
|
|
return res, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (a ArticleRepository) GetByURL(ctx context.Context, url string) (*models.Article, error) {
|
|
|
|
|
|
+func (a ArticleRepository) GetByURL(
|
|
|
|
+ ctx context.Context,
|
|
|
|
+ url string,
|
|
|
|
+ lang models.Language,
|
|
|
|
+) (*models.Article, error) {
|
|
var res models.Article
|
|
var res models.Article
|
|
|
|
|
|
q, v, err := sq.Select(articleTableColumns...).
|
|
q, v, err := sq.Select(articleTableColumns...).
|
|
From(articleTableName).
|
|
From(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
- Where(sq.Eq{"url": url}).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
|
|
+ Where(sq.Eq{
|
|
|
|
+ "url": url,
|
|
|
|
+ "language": uint64(lang),
|
|
|
|
+ }).
|
|
Limit(1).
|
|
Limit(1).
|
|
ToSql()
|
|
ToSql()
|
|
|
|
|
|
@@ -112,12 +127,15 @@ func (a ArticleRepository) GetByURL(ctx context.Context, url string) (*models.Ar
|
|
return &res, nil
|
|
return &res, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (a ArticleRepository) GetByID(ctx context.Context, id uint64) (*models.Article, error) {
|
|
|
|
|
|
+func (a ArticleRepository) GetByID(
|
|
|
|
+ ctx context.Context,
|
|
|
|
+ id uint64,
|
|
|
|
+) (*models.Article, error) {
|
|
var res models.Article
|
|
var res models.Article
|
|
|
|
|
|
q, v, err := sq.Select(articleTableColumns...).
|
|
q, v, err := sq.Select(articleTableColumns...).
|
|
From(articleTableName).
|
|
From(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
Where(sq.Eq{"id": id}).
|
|
Where(sq.Eq{"id": id}).
|
|
Limit(1).
|
|
Limit(1).
|
|
ToSql()
|
|
ToSql()
|
|
@@ -134,9 +152,12 @@ func (a ArticleRepository) GetByID(ctx context.Context, id uint64) (*models.Arti
|
|
return &res, nil
|
|
return &res, nil
|
|
}
|
|
}
|
|
|
|
|
|
-func (a ArticleRepository) Add(ctx context.Context, m models.Article) (uint64, error) {
|
|
|
|
|
|
+func (a ArticleRepository) Add(
|
|
|
|
+ ctx context.Context,
|
|
|
|
+ m models.Article,
|
|
|
|
+) (uint64, error) {
|
|
q, v, err := sq.Insert(articleTableName).
|
|
q, v, err := sq.Insert(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
Columns(
|
|
Columns(
|
|
"url",
|
|
"url",
|
|
"publish_time",
|
|
"publish_time",
|
|
@@ -147,6 +168,7 @@ func (a ArticleRepository) Add(ctx context.Context, m models.Article) (uint64, e
|
|
"meta_keywords",
|
|
"meta_keywords",
|
|
"meta_desc",
|
|
"meta_desc",
|
|
"is_active",
|
|
"is_active",
|
|
|
|
+ "language",
|
|
).Values(
|
|
).Values(
|
|
m.URL,
|
|
m.URL,
|
|
m.PublishTime,
|
|
m.PublishTime,
|
|
@@ -157,6 +179,7 @@ func (a ArticleRepository) Add(ctx context.Context, m models.Article) (uint64, e
|
|
m.MetaKeywords,
|
|
m.MetaKeywords,
|
|
m.MetaDescription,
|
|
m.MetaDescription,
|
|
m.IsActive,
|
|
m.IsActive,
|
|
|
|
+ m.Language,
|
|
).
|
|
).
|
|
Suffix("RETURNING id").
|
|
Suffix("RETURNING id").
|
|
ToSql()
|
|
ToSql()
|
|
@@ -176,7 +199,7 @@ func (a ArticleRepository) Add(ctx context.Context, m models.Article) (uint64, e
|
|
|
|
|
|
func (a ArticleRepository) Update(ctx context.Context, req models.Article) error {
|
|
func (a ArticleRepository) Update(ctx context.Context, req models.Article) error {
|
|
q, v, err := sq.Update(articleTableName).
|
|
q, v, err := sq.Update(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
Set("url", req.URL).
|
|
Set("url", req.URL).
|
|
Set("publish_time", req.PublishTime).
|
|
Set("publish_time", req.PublishTime).
|
|
Set("title", req.Title).
|
|
Set("title", req.Title).
|
|
@@ -203,7 +226,7 @@ func (a ArticleRepository) Update(ctx context.Context, req models.Article) error
|
|
|
|
|
|
func (a ArticleRepository) Delete(ctx context.Context, id uint64) error {
|
|
func (a ArticleRepository) Delete(ctx context.Context, id uint64) error {
|
|
q, v, err := sq.Delete(articleTableName).
|
|
q, v, err := sq.Delete(articleTableName).
|
|
- PlaceholderFormat(sq.Dollar).
|
|
|
|
|
|
+ PlaceholderFormat(sq.Question).
|
|
Where(sq.Eq{"id": id}).
|
|
Where(sq.Eq{"id": id}).
|
|
ToSql()
|
|
ToSql()
|
|
|
|
|