Files
2026-08-18 00:59:32 +05:00

167 lines
3.5 KiB
Go

package db
import (
"bankline/internal/admin/currency"
"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) currency.Repository {
return &repository{
client: client,
logger: logger,
}
}
var sortColumns = map[string]string{
"id": "id",
"createdAt": "created_at",
}
func (r repository) GetAllData(ctx context.Context, params listing.Params) (currency.DataCurrencyDTO, error) {
ns := []currency.CurrencyListItemDTO{}
var (
dt currency.DataCurrencyDTO
err error
rows pgx.Rows
)
where := ""
args := []interface{}{}
if params.Search != "" {
where = ` where title::text ILIKE $1 `
args = append(args, params.SearchPattern())
}
qC := `select count(*) from currency` + where + `;`
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
// List: skip content_* (large HTML). Full body is loaded via GetData.
q := ` select
id, title,
path, created_at
from currency` +
where +
params.OrderClause(sortColumns, "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 {
return dt, nil
}
defer rows.Close()
for rows.Next() {
var n currency.CurrencyListItemDTO
errN := rows.Scan(
&n.Id, &n.Title,
&n.Path, &n.CreatedAt,
)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
}
ns = append(ns, n)
}
dt.Currencies = ns
return dt, nil
}
func (r repository) GetData(ctx context.Context, id int) (currency.CurrencyDTO, error) {
var (
ns currency.CurrencyDTO
)
qC := `select id, title, content_tm, content_ru, content_en, path, created_at from currency where id = $1 ;`
err := r.client.QueryRow(ctx, qC, id).Scan(
&ns.Id, &ns.Title,
&ns.ContentTm, &ns.ContentRu, &ns.ContentEn,
&ns.Path, &ns.CreatedAt,
)
if err != nil && err.Error() != "no rows in result set" {
return ns, err
}
return ns, nil
}
func (r repository) AddData(ctx context.Context, req currency.AddCurrencyDTO) (currency.CurrencyIdDTO, error) {
var (
n currency.CurrencyIdDTO
)
q := `INSERT INTO currency (title, content_tm, content_ru, content_en, path)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, title, path;`
err := r.client.QueryRow(ctx, q,
req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Path,
).Scan(
&n.Id, &n.Title, &n.Path,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) UpdateData(ctx context.Context, req currency.UpdateCurrencyDTO, id int) (currency.CurrencyIdDTO, error) {
var (
q string
n currency.CurrencyIdDTO
)
q = ` update currency set
title = $1, content_tm = $2, content_ru = $3, content_en = $4, path = $5
where id = $6
RETURNING id, title, path;`
err := r.client.QueryRow(ctx, q,
req.Title, req.ContentTm, req.ContentRu, req.ContentEn, req.Path,
id,
).Scan(
&n.Id, &n.Title, &n.Path,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) DeleteData(ctx context.Context, id int) error {
var (
q string
)
q = ` delete from currency where id = $1 ;`
_, err := r.client.Exec(ctx, q, id)
if err != nil && err.Error() != "no rows in result set" {
return err
}
return nil
}