Localize client API responses by lang query param.

Client endpoints now return a single-language string (default tm) so the frontend can pass ?lang= and skip picking from nested tm/ru/en objects.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-06 12:27:10 +05:00
parent 76848f4982
commit ddd898078c
5 changed files with 202 additions and 110 deletions

View File

@@ -22,9 +22,10 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) users.Repos
}
}
func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNewsDTO, error) {
func (r repository) GetNews(ctx context.Context, limit, page int, lang string) (users.DataNewsDTO, error) {
var dt users.DataNewsDTO
lang = users.NormalizeLang(lang)
// List endpoint: skip news_content (large HTML). Full body is loaded via GetNewsById.
// Default to latest 6 when limit/page not provided.
@@ -51,11 +52,13 @@ func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNew
ns := []users.NewsDTO{}
for rows.Next() {
var n users.NewsDTO
errN := rows.Scan(&n.Id, &n.Title, &n.Image)
var title users.Language
errN := rows.Scan(&n.Id, &title, &n.Image)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
}
n.Title = title.Pick(lang)
ns = append(ns, n)
}
@@ -64,16 +67,17 @@ func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNew
return dt, nil
}
func (r repository) GetCategories(ctx context.Context) ([]users.CategoryDTO, error) {
func (r repository) GetCategories(ctx context.Context, lang string) ([]users.CategoryDTO, error) {
lang = users.NormalizeLang(lang)
// Menu only needs id/name; page content is loaded via GetSubCategoryById.
q := ` SELECT
c.id, c.name,
c.id, COALESCE(c.name->>$1, ''),
COALESCE(
jsonb_agg(
jsonb_build_object(
'id', sc.id, 'categoryId', sc.category_id,
'name', sc.name
'name', COALESCE(sc.name->>$1, '')
)
ORDER BY sc.created_at ASC
) FILTER (WHERE sc.id IS NOT NULL),
@@ -85,7 +89,7 @@ func (r repository) GetCategories(ctx context.Context) ([]users.CategoryDTO, err
GROUP BY c.id
ORDER BY c.created_at DESC;`
rows, err := r.client.Query(ctx, q)
rows, err := r.client.Query(ctx, q, lang)
if err != nil {
r.logger.Error("get categories :::", err)
return nil, err
@@ -153,8 +157,12 @@ func (r repository) GetBanners(ctx context.Context, limit, page int) (users.Data
return dt, nil
}
func (r repository) GetBannerById(ctx context.Context, id int) (users.BannerDTO, error) {
func (r repository) GetBannerById(ctx context.Context, id int, lang string) (users.BannerDTO, error) {
var b users.BannerDTO
lang = users.NormalizeLang(lang)
var title users.Language
var contentTm, contentRu, contentEn string
q := ` SELECT
id, title,
@@ -164,19 +172,22 @@ func (r repository) GetBannerById(ctx context.Context, id int) (users.BannerDTO,
WHERE id = $1;`
err := r.client.QueryRow(ctx, q, id).Scan(
&b.Id, &b.Title,
&b.ContentTm, &b.ContentRu, &b.ContentEn,
&b.Id, &title,
&contentTm, &contentRu, &contentEn,
&b.Image, &b.CreatedAt,
)
if err != nil {
return b, err
}
b.Title = title.Pick(lang)
b.Content = users.PickContent(lang, contentTm, contentRu, contentEn)
return b, nil
}
func (r repository) GetStatics(ctx context.Context, limit, page int) (users.DataStaticDTO, error) {
func (r repository) GetStatics(ctx context.Context, limit, page int, lang string) (users.DataStaticDTO, error) {
var dt users.DataStaticDTO
lang = users.NormalizeLang(lang)
// Grid only needs id/logo/title. Full body is loaded via GetStaticById.
q := ` SELECT
@@ -204,11 +215,13 @@ func (r repository) GetStatics(ctx context.Context, limit, page int) (users.Data
ss := []users.StaticDTO{}
for rows.Next() {
var s users.StaticDTO
errS := rows.Scan(&s.Id, &s.Logo, &s.Title)
var title users.Language
errS := rows.Scan(&s.Id, &s.Logo, &title)
if errS != nil {
r.logger.Error("errS :::", errS)
continue
}
s.Title = title.Pick(lang)
ss = append(ss, s)
}
@@ -217,8 +230,12 @@ func (r repository) GetStatics(ctx context.Context, limit, page int) (users.Data
return dt, nil
}
func (r repository) GetStaticById(ctx context.Context, id int) (users.StaticDTO, error) {
func (r repository) GetStaticById(ctx context.Context, id int, lang string) (users.StaticDTO, error) {
var s users.StaticDTO
lang = users.NormalizeLang(lang)
var title users.Language
var contentTm, contentRu, contentEn string
q := ` SELECT
id, logo, title,
@@ -228,18 +245,24 @@ func (r repository) GetStaticById(ctx context.Context, id int) (users.StaticDTO,
WHERE id = $1;`
err := r.client.QueryRow(ctx, q, id).Scan(
&s.Id, &s.Logo, &s.Title,
&s.ContentTm, &s.ContentRu, &s.ContentEn,
&s.Id, &s.Logo, &title,
&contentTm, &contentRu, &contentEn,
&s.CreatedAt,
)
if err != nil {
return s, err
}
s.Title = title.Pick(lang)
s.Content = users.PickContent(lang, contentTm, contentRu, contentEn)
return s, nil
}
func (r repository) GetCurrency(ctx context.Context) (users.CurrencyDTO, error) {
func (r repository) GetCurrency(ctx context.Context, lang string) (users.CurrencyDTO, error) {
var cu users.CurrencyDTO
lang = users.NormalizeLang(lang)
var title users.Language
var contentTm, contentRu, contentEn string
q := ` SELECT
id, title,
@@ -250,13 +273,15 @@ func (r repository) GetCurrency(ctx context.Context) (users.CurrencyDTO, error)
LIMIT 1;`
err := r.client.QueryRow(ctx, q).Scan(
&cu.Id, &cu.Title,
&cu.Content.Tm, &cu.Content.Ru, &cu.Content.En,
&cu.Id, &title,
&contentTm, &contentRu, &contentEn,
&cu.Path, &cu.CreatedAt,
)
if err != nil {
return cu, err
}
cu.Title = title.Pick(lang)
cu.Content = users.PickContent(lang, contentTm, contentRu, contentEn)
return cu, nil
}
@@ -278,8 +303,12 @@ func (r repository) GetSetting(ctx context.Context) (users.SettingDTO, error) {
return s, nil
}
func (r repository) GetSubCategoryById(ctx context.Context, id int) (users.SubCategoryDTO, error) {
func (r repository) GetSubCategoryById(ctx context.Context, id int, lang string) (users.SubCategoryDTO, error) {
var sc users.SubCategoryDTO
lang = users.NormalizeLang(lang)
var name users.Language
var contentEn, contentTm, contentRu string
q := ` SELECT
sc.id, sc.category_id,
@@ -291,33 +320,41 @@ func (r repository) GetSubCategoryById(ctx context.Context, id int) (users.SubCa
err := r.client.QueryRow(ctx, q, id).Scan(
&sc.Id, &sc.CategoryId,
&sc.Name, &sc.Count,
&sc.Content.En, &sc.Content.Tm, &sc.Content.Ru,
&name, &sc.Count,
&contentEn, &contentTm, &contentRu,
)
if err != nil {
return sc, err
}
sc.Name = name.Pick(lang)
sc.Content = users.PickContent(lang, contentTm, contentRu, contentEn)
return sc, nil
}
func (r repository) GetNewsById(ctx context.Context, id int) (users.NewsDTO, error) {
func (r repository) GetNewsById(ctx context.Context, id int, lang string) (users.NewsDTO, error) {
var n users.NewsDTO
lang = users.NormalizeLang(lang)
var title users.Language
var contentEn, contentTm, contentRu string
q := ` SELECT
n.id, n.title,
n.date_release, n.img,
nc.en, nc.tm, nc.ru
COALESCE(nc.en, ''), COALESCE(nc.tm, ''), COALESCE(nc.ru, '')
FROM news n
LEFT JOIN news_content nc ON n.id = nc.news_id
WHERE n.id = $1;`
err := r.client.QueryRow(ctx, q, id).Scan(
&n.Id, &n.Title,
&n.Id, &title,
&n.DataRelease, &n.Image,
&n.Content.En, &n.Content.Tm, &n.Content.Ru,
&contentEn, &contentTm, &contentRu,
)
if err != nil {
return n, err
}
n.Title = title.Pick(lang)
n.Content = users.PickContent(lang, contentTm, contentRu, contentEn)
return n, nil
}