Files
main-tbbank/backend/internal/admin/static/db/postgresql.go
2026-08-18 00:59:32 +05:00

157 lines
3.4 KiB
Go

package db
import (
"bankline/internal/admin/static"
"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) static.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) (static.DataStaticDTO, error) {
ns := []static.StaticListItemDTO{}
var (
dt static.DataStaticDTO
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 static` + where + `;`
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
// List: skip content_* (large HTML). Full body is loaded via GetData.
q := ` select
id, logo, title,
created_at
from static` +
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 static.StaticListItemDTO
errN := rows.Scan(
&n.Id, &n.Logo, &n.Title,
&n.CreatedAt,
)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
}
ns = append(ns, n)
}
dt.Statics = ns
return dt, nil
}
func (r repository) GetData(ctx context.Context, id int) (static.StaticDTO, error) {
var (
ns static.StaticDTO
)
qC := `select id, logo, title, content_tm, content_ru, content_en, created_at from static where id = $1 ;`
err := r.client.QueryRow(ctx, qC, id).Scan(
&ns.Id, &ns.Logo, &ns.Title,
&ns.ContentTm, &ns.ContentRu, &ns.ContentEn,
&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 static.AddStaticDTO) (static.StaticIdDTO, error) {
var (
n static.StaticIdDTO
)
q := `INSERT INTO static (logo, title, content_tm, content_ru, content_en) VALUES ($1, $2, $3, $4, $5) RETURNING id, logo, title;`
err := r.client.QueryRow(ctx, q, req.Logo, req.Title, req.ContentTm, req.ContentRu, req.ContentEn).Scan(
&n.Id, &n.Logo, &n.Title,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) UpdateData(ctx context.Context, req static.UpdateStaticDTO, id int) (static.StaticIdDTO, error) {
var (
q string
n static.StaticIdDTO
)
q = ` update static set logo = $1, title = $2, content_tm = $3, content_ru = $4, content_en = $5 where id = $6 RETURNING id, logo, title;`
err := r.client.QueryRow(ctx, q, req.Logo, req.Title, req.ContentTm, req.ContentRu, req.ContentEn, id).Scan(
&n.Id, &n.Logo, &n.Title,
)
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 static where id = $1 ;`
_, err := r.client.Exec(ctx, q, id)
if err != nil && err.Error() != "no rows in result set" {
return err
}
return nil
}