fixed
This commit is contained in:
1 parent
4194cef934
commit
363127fe9e
3350 files changed
+1284220
-1
No files matched your search
+224
@@ -0,0 +1,224 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"bankline/internal/admin/sub_category"
|
||||
"bankline/internal/listing"
|
||||
"bankline/pkg/logging"
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"github.com/jackc/pgx/v4"
|
||||
"github.com/jackc/pgx/v4/pgxpool"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client *pgxpool.Pool
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client *pgxpool.Pool, logger *logging.Logger) sub_category.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
var sortColumns = map[string]string{
|
||||
"id": "sc.id",
|
||||
"status": "sc.status",
|
||||
"createdAt": "sc.created_at",
|
||||
}
|
||||
|
||||
func (r repository) GetAllData(ctx context.Context, params listing.Params, categoryId int) (sub_category.DataDTO, error) {
|
||||
|
||||
ns := []sub_category.SubCategoryDTO{}
|
||||
var (
|
||||
dt sub_category.DataDTO
|
||||
err error
|
||||
rows pgx.Rows
|
||||
)
|
||||
|
||||
where := ` where sc.category_id = $1 `
|
||||
args := []interface{}{categoryId}
|
||||
if params.Search != "" {
|
||||
where += ` and (sc.name::text ILIKE $2 OR scc.tm ILIKE $2 OR scc.ru ILIKE $2 OR scc.en ILIKE $2) `
|
||||
args = append(args, params.SearchPattern())
|
||||
}
|
||||
|
||||
qC := `select count(sc.*) from sub_categories sc
|
||||
LEFT JOIN sub_categories_content scc ON sc.id = scc.sub_category_id` + where + `;`
|
||||
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
|
||||
|
||||
q := `select
|
||||
sc.id, sc.status,
|
||||
sc.name, sc.created_at,
|
||||
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 +
|
||||
params.OrderClause(sortColumns, "sc.created_at DESC")
|
||||
|
||||
if params.Paginated() {
|
||||
q += fmt.Sprintf("limit $%d offset $%d;", len(args)+1, len(args)+2)
|
||||
args = append(args, params.Limit, params.Offset())
|
||||
} else {
|
||||
q += ";"
|
||||
}
|
||||
rows, err = r.client.Query(ctx, q, args...)
|
||||
if err != nil {
|
||||
fmt.Println("=========", err)
|
||||
return dt, nil
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
for rows.Next() {
|
||||
var n sub_category.SubCategoryDTO
|
||||
errN := rows.Scan(
|
||||
&n.Id, &n.Status,
|
||||
&n.Name, &n.CreatedAt,
|
||||
&n.Content.En, &n.Content.Tm, &n.Content.Ru,
|
||||
)
|
||||
if errN != nil {
|
||||
fmt.Println("errN :::", errN)
|
||||
continue
|
||||
}
|
||||
|
||||
ns = append(ns, n)
|
||||
}
|
||||
dt.SubCategories = ns
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) GetData(ctx context.Context, id int) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
var (
|
||||
ns sub_category.SubCategoryDTO
|
||||
)
|
||||
|
||||
qC := `select
|
||||
sc.id, sc.category_id, sc.name, sc.status, sc.created_at,
|
||||
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 ;`
|
||||
err := r.client.QueryRow(ctx, qC, id).Scan(
|
||||
&ns.Id, &ns.CatId, &ns.Name, &ns.Status, &ns.CreatedAt,
|
||||
&ns.Content.En, &ns.Content.Tm, &ns.Content.Ru,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return ns, err
|
||||
}
|
||||
|
||||
return ns, nil
|
||||
}
|
||||
|
||||
func (r repository) AddData(ctx context.Context, req sub_category.AddSubCategoryDTO) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
var (
|
||||
n sub_category.SubCategoryDTO
|
||||
lang sub_category.Language
|
||||
)
|
||||
|
||||
q := `INSERT INTO sub_categories (category_id, name) VALUES ($1, $2) RETURNING id, category_id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.CatId, req.Name).Scan(
|
||||
&n.Id, &n.CatId,
|
||||
&n.Name, &n.Status,
|
||||
)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
|
||||
if n.Id != 0 {
|
||||
q = `INSERT INTO sub_categories_content (sub_category_id, en, ru, tm)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING en, ru, tm;`
|
||||
|
||||
err = r.client.QueryRow(ctx, q, n.Id, req.Content.En, req.Content.Ru, req.Content.Tm).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
if err != nil {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
n.Content = lang
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateData(ctx context.Context, req sub_category.UpdateSubCategoryDTO, id int) (sub_category.SubCategoryDTO, error) {
|
||||
var (
|
||||
q string
|
||||
n sub_category.SubCategoryDTO
|
||||
lang sub_category.Language
|
||||
)
|
||||
|
||||
q = ` update sub_categories set category_id = $1, name = $2 where Id = $3 RETURNING id, category_id, name, status;`
|
||||
|
||||
err := r.client.QueryRow(ctx, q, req.CatId, req.Name, id).Scan(
|
||||
&n.Id, &n.CatId,
|
||||
&n.Name, &n.Status,
|
||||
)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
|
||||
if n.Id != 0 {
|
||||
q = `UPDATE sub_categories_content SET en = $1, ru = $2, tm = $3
|
||||
WHERE sub_category_id = $4 RETURNING en, ru, tm;`
|
||||
|
||||
err = r.client.QueryRow(ctx, q, req.Content.En, req.Content.Ru, req.Content.Tm, n.Id).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
if err != nil {
|
||||
// Row may be missing on older data — insert then.
|
||||
if err.Error() == "no rows in result set" {
|
||||
q = `INSERT INTO sub_categories_content (sub_category_id, en, ru, tm)
|
||||
VALUES ($1, $2, $3, $4) RETURNING en, ru, tm;`
|
||||
err = r.client.QueryRow(ctx, q, n.Id, req.Content.En, req.Content.Ru, req.Content.Tm).Scan(
|
||||
&lang.En, &lang.Ru, &lang.Tm,
|
||||
)
|
||||
}
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
fmt.Println("-----------", err)
|
||||
return n, err
|
||||
}
|
||||
}
|
||||
n.Content = lang
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
||||
func (r repository) UpdateStatusData(ctx context.Context, status string, id int) (sub_category.SubCategoryDTO, error) {
|
||||
|
||||
q := ` update sub_categories set status = $1 where id = $2;`
|
||||
|
||||
_, err := r.client.Exec(ctx, q, status, id)
|
||||
|
||||
dt, err := r.GetData(ctx, id)
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return dt, err
|
||||
}
|
||||
|
||||
return dt, nil
|
||||
}
|
||||
|
||||
func (r repository) DeleteData(ctx context.Context, Id int) error {
|
||||
var (
|
||||
q string
|
||||
)
|
||||
|
||||
q = ` delete from sub_categories where Id = $1 ;`
|
||||
_, err := r.client.Exec(ctx, q, Id)
|
||||
|
||||
if err != nil && err.Error() != "no rows in result set" {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user