Optimize list API payloads and replace homepage swiper with a lighter slider.

Skip unused HTML and metadata from client/admin list queries so pages load less data, and load banner detail by id.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-05 15:27:42 +05:00
parent ca0692d190
commit 8d7390894e
8 changed files with 3827 additions and 250 deletions

View File

@@ -34,24 +34,27 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (news
var dt news.DataNewsDTO
where := ""
join := ""
args := []interface{}{}
if params.Search != "" {
// JOIN only for search filter; list still skips large HTML bodies.
join = ` LEFT JOIN news_content nc ON n.id = nc.news_id `
where = ` WHERE n.title::text ILIKE $1 OR nc.tm ILIKE $1 OR nc.ru ILIKE $1 OR nc.en ILIKE $1 `
args = append(args, params.SearchPattern())
}
qC := `SELECT count(*) FROM news n LEFT JOIN news_content nc ON n.id = nc.news_id` + where + `;`
qC := `SELECT count(*) FROM news n` + join + where + `;`
if err := r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count); err != nil {
r.logger.Error("count news :::", err)
return dt, err
}
// List endpoint: skip news_content body. Full HTML is loaded via GetOneData.
q := ` SELECT
n.id, n.title,
n.date_release, n.img,
nc.en, nc.tm, nc.ru
FROM news n
LEFT JOIN news_content nc ON n.id = nc.news_id ` +
n.date_release, n.img
FROM news n` +
join +
where +
params.OrderClause(sortColumns, "n.date_release DESC")
@@ -78,8 +81,6 @@ func (r repository) GetAllData(ctx context.Context, params listing.Params) (news
if err = rows.Scan(
&n.Id, &n.Title,
&n.DataRelease, &n.Image,
&n.Content.En,
&n.Content.Tm, &n.Content.Ru,
); err != nil {
r.logger.Error("scan news :::", err)
continue

View File

@@ -26,31 +26,22 @@ func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNew
var dt users.DataNewsDTO
qC := `select count(*) from news;`
if err := r.client.QueryRow(ctx, qC).Scan(&dt.Count); err != nil {
r.logger.Error("count news :::", err)
return dt, err
// List endpoint: skip news_content (large HTML). Full body is loaded via GetNewsById.
// Default to latest 6 when limit/page not provided.
if limit == 0 {
limit = 6
}
if page == 0 {
page = 1
}
q := ` SELECT
n.id, n.title,
n.date_release, n.img,
nc.en, nc.tm, nc.ru
n.id, n.title, n.img
FROM news n
LEFT JOIN news_content nc ON n.id = nc.news_id
ORDER BY n.date_release DESC `
ORDER BY n.date_release DESC
LIMIT $1 OFFSET $2;`
var (
rows pgx.Rows
err error
)
if limit == 0 || page == 0 {
q += ";"
rows, err = r.client.Query(ctx, q)
} else {
q += "limit $1 offset $2;"
rows, err = r.client.Query(ctx, q, limit, (page-1)*limit)
}
rows, err := r.client.Query(ctx, q, limit, (page-1)*limit)
if err != nil {
r.logger.Error("get news :::", err)
return dt, err
@@ -60,11 +51,7 @@ 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.DataRelease, &n.Image,
&n.Content.En, &n.Content.Tm, &n.Content.Ru,
)
errN := rows.Scan(&n.Id, &n.Title, &n.Image)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
@@ -73,31 +60,27 @@ func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNew
ns = append(ns, n)
}
dt.News = ns
dt.Count = len(ns)
return dt, nil
}
func (r repository) GetCategories(ctx context.Context) ([]users.CategoryDTO, error) {
// Menu only needs id/name; page content is loaded via GetSubCategoryById.
q := ` SELECT
c.id, c.name, c.img,
c.id, c.name,
COALESCE(
jsonb_agg(
jsonb_build_object(
'id', sc.id, 'categoryId', sc.category_id,
'name', sc.name, 'count', sc.count,
'content', jsonb_build_object(
'en', COALESCE(scc.en, ''),
'ru', COALESCE(scc.ru, ''),
'tm', COALESCE(scc.tm, '')
)
'name', sc.name
)
ORDER BY sc.created_at DESC
) FILTER (WHERE sc.id IS NOT NULL AND sc.status = true),
ORDER BY sc.created_at ASC
) FILTER (WHERE sc.id IS NOT NULL),
'[]'
) AS sub_categories
FROM categories c
LEFT JOIN sub_categories sc ON sc.category_id = c.id
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id
LEFT JOIN sub_categories sc ON sc.category_id = c.id AND sc.status = true
WHERE c.status = true
GROUP BY c.id
ORDER BY c.created_at ASC;`
@@ -113,7 +96,7 @@ func (r repository) GetCategories(ctx context.Context) ([]users.CategoryDTO, err
for rows.Next() {
var c users.CategoryDTO
errC := rows.Scan(
&c.Id, &c.Name, &c.Image,
&c.Id, &c.Name,
&c.SubCategories,
)
if errC != nil {
@@ -131,16 +114,9 @@ func (r repository) GetBanners(ctx context.Context, limit, page int) (users.Data
var dt users.DataBannerDTO
qC := `select count(*) from banner;`
if err := r.client.QueryRow(ctx, qC).Scan(&dt.Count); err != nil {
r.logger.Error("count banners :::", err)
return dt, err
}
// List/slider only needs id + image. Full body is loaded via GetBannerById.
q := ` SELECT
id, title,
content_tm, content_ru, content_en,
img, created_at
id, img
FROM banner
ORDER BY created_at DESC `
@@ -164,11 +140,7 @@ func (r repository) GetBanners(ctx context.Context, limit, page int) (users.Data
bs := []users.BannerDTO{}
for rows.Next() {
var b users.BannerDTO
errB := rows.Scan(
&b.Id, &b.Title,
&b.ContentTm, &b.ContentRu, &b.ContentEn,
&b.Image, &b.CreatedAt,
)
errB := rows.Scan(&b.Id, &b.Image)
if errB != nil {
r.logger.Error("errB :::", errB)
continue
@@ -177,6 +149,7 @@ func (r repository) GetBanners(ctx context.Context, limit, page int) (users.Data
bs = append(bs, b)
}
dt.Banners = bs
dt.Count = len(bs)
return dt, nil
}
@@ -205,16 +178,9 @@ func (r repository) GetStatics(ctx context.Context, limit, page int) (users.Data
var dt users.DataStaticDTO
qC := `select count(*) from static;`
if err := r.client.QueryRow(ctx, qC).Scan(&dt.Count); err != nil {
r.logger.Error("count statics :::", err)
return dt, err
}
// Grid only needs id/logo/title. Full body is loaded via GetStaticById.
q := ` SELECT
id, logo, title,
content_tm, content_ru, content_en,
created_at
id, logo, title
FROM static
ORDER BY created_at DESC `
@@ -238,11 +204,7 @@ 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,
&s.ContentTm, &s.ContentRu, &s.ContentEn,
&s.CreatedAt,
)
errS := rows.Scan(&s.Id, &s.Logo, &s.Title)
if errS != nil {
r.logger.Error("errS :::", errS)
continue
@@ -251,6 +213,7 @@ func (r repository) GetStatics(ctx context.Context, limit, page int) (users.Data
ss = append(ss, s)
}
dt.Statics = ss
dt.Count = len(ss)
return dt, nil
}
@@ -280,8 +243,7 @@ func (r repository) GetCurrency(ctx context.Context) (users.CurrencyDTO, error)
q := ` SELECT
id, title,
content_tm, content_ru, content_en,
path, created_at
content_tm, content_ru, content_en
FROM currency
ORDER BY created_at DESC
LIMIT 1;`
@@ -289,7 +251,6 @@ func (r repository) GetCurrency(ctx context.Context) (users.CurrencyDTO, error)
err := r.client.QueryRow(ctx, q).Scan(
&cu.Id, &cu.Title,
&cu.Content.Tm, &cu.Content.Ru, &cu.Content.En,
&cu.Path, &cu.CreatedAt,
)
if err != nil {
return cu, err
@@ -301,13 +262,13 @@ func (r repository) GetSetting(ctx context.Context) (users.SettingDTO, error) {
var s users.SettingDTO
q := ` SELECT
uuid::text, logo_r, phone_number, fax_number, help_number, created_at
logo_r, phone_number, fax_number, help_number
FROM setting
ORDER BY id ASC
LIMIT 1;`
err := r.client.QueryRow(ctx, q).Scan(
&s.UUID, &s.LogoR, &s.PhoneNumber, &s.FaxNumber, &s.HelpNumber, &s.CreatedAt,
&s.LogoR, &s.PhoneNumber, &s.FaxNumber, &s.HelpNumber,
)
if err != nil {
return s, err