fixed
This commit is contained in:
339
backend/internal/client/users/db/postgresql.go
Executable file
339
backend/internal/client/users/db/postgresql.go
Executable file
@@ -0,0 +1,339 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/client/users"
|
||||
"bankline/pkg/client/postgresql"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
_ "github.com/lib/pq"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) users.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
func (r repository) GetNews(ctx context.Context, limit, page int) (users.DataNewsDTO, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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
|
||||
ORDER BY n.date_release DESC `
|
||||
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
r.logger.Error("get news :::", err)
|
||||
return dt, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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,
|
||||
)
|
||||
if errN != nil {
|
||||
r.logger.Error("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.News = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetCategories(ctx context.Context) ([]users.CategoryDTO, error) {
|
||||
|
||||
q := ` SELECT
|
||||
c.id, c.name, c.img,
|
||||
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, '')
|
||||
)
|
||||
)
|
||||
ORDER BY sc.created_at DESC
|
||||
) FILTER (WHERE sc.id IS NOT NULL AND sc.status = true),
|
||||
'[]'
|
||||
) 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
|
||||
WHERE c.status = true
|
||||
GROUP BY c.id
|
||||
ORDER BY c.created_at ASC;`
|
||||
|
||||
rows, err := r.client.Query(ctx, q)
|
||||
if err != nil {
|
||||
r.logger.Error("get categories :::", err)
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
cs := []users.CategoryDTO{}
|
||||
for rows.Next() {
|
||||
var c users.CategoryDTO
|
||||
errC := rows.Scan(
|
||||
&c.Id, &c.Name, &c.Image,
|
||||
&c.SubCategories,
|
||||
)
|
||||
if errC != nil {
|
||||
r.logger.Error("errC :::", errC)
|
||||
continue
|
||||
}
|
||||
|
||||
cs = append(cs, c)
|
||||
}
|
||||
|
||||
return cs, nil
|
||||
}
|
||||
|
||||
func (r repository) GetBanners(ctx context.Context, limit, page int) (users.DataBannerDTO, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
q := ` SELECT
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
img, created_at
|
||||
FROM banner
|
||||
ORDER BY created_at DESC `
|
||||
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
r.logger.Error("get banners :::", err)
|
||||
return dt, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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,
|
||||
)
|
||||
if errB != nil {
|
||||
r.logger.Error("errB :::", errB)
|
||||
continue
|
||||
}
|
||||
|
||||
bs = append(bs, b)
|
||||
}
|
||||
dt.Banners = bs
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetStatics(ctx context.Context, limit, page int) (users.DataStaticDTO, error) {
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
q := ` SELECT
|
||||
id, logo, title,
|
||||
content_tm, content_ru, content_en,
|
||||
created_at
|
||||
FROM static
|
||||
ORDER BY created_at DESC `
|
||||
|
||||
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)
|
||||
}
|
||||
if err != nil {
|
||||
r.logger.Error("get statics :::", err)
|
||||
return dt, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
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,
|
||||
)
|
||||
if errS != nil {
|
||||
r.logger.Error("errS :::", errS)
|
||||
continue
|
||||
}
|
||||
|
||||
ss = append(ss, s)
|
||||
}
|
||||
dt.Statics = ss
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetStaticById(ctx context.Context, id int) (users.StaticDTO, error) {
|
||||
var s users.StaticDTO
|
||||
|
||||
q := ` SELECT
|
||||
id, logo, title,
|
||||
content_tm, content_ru, content_en,
|
||||
created_at
|
||||
FROM static
|
||||
WHERE id = $1;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, id).Scan(
|
||||
&s.Id, &s.Logo, &s.Title,
|
||||
&s.ContentTm, &s.ContentRu, &s.ContentEn,
|
||||
&s.CreatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (r repository) GetCurrency(ctx context.Context) (users.CurrencyDTO, error) {
|
||||
var cu users.CurrencyDTO
|
||||
|
||||
q := ` SELECT
|
||||
id, title,
|
||||
content_tm, content_ru, content_en,
|
||||
path, created_at
|
||||
FROM currency
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 1;`
|
||||
|
||||
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
|
||||
}
|
||||
return cu, nil
|
||||
}
|
||||
|
||||
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
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
return s, err
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (r repository) GetSubCategoryById(ctx context.Context, id int) (users.SubCategoryDTO, error) {
|
||||
var sc users.SubCategoryDTO
|
||||
|
||||
q := ` SELECT
|
||||
sc.id, sc.category_id,
|
||||
sc.name, sc.count,
|
||||
COALESCE(scc.en, ''), COALESCE(scc.tm, ''), COALESCE(scc.ru, '')
|
||||
FROM sub_categories sc
|
||||
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id
|
||||
WHERE sc.id = $1 AND sc.status = true;`
|
||||
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
return sc, err
|
||||
}
|
||||
return sc, nil
|
||||
}
|
||||
|
||||
func (r repository) GetNewsById(ctx context.Context, id int) (users.NewsDTO, error) {
|
||||
var n users.NewsDTO
|
||||
|
||||
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
|
||||
WHERE n.id = $1;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, id).Scan(
|
||||
&n.Id, &n.Title,
|
||||
&n.DataRelease, &n.Image,
|
||||
&n.Content.En, &n.Content.Tm, &n.Content.Ru,
|
||||
)
|
||||
if err != nil {
|
||||
return n, err
|
||||
}
|
||||
return n, nil
|
||||
}
|
||||
Reference in New Issue
Block a user