Files
main-tbbank/backend/internal/admin/category/db/postgresql.go
2026-08-04 12:50:20 +05:00

170 lines
3.6 KiB
Go
Executable File

package db
import (
"bankline/internal/admin/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) category.Repository {
return &repository{
client: client,
logger: logger,
}
}
var sortColumns = map[string]string{
"id": "cs.id",
"status": "cs.status",
"createdAt": "cs.created_at",
}
func (r repository) GetAllData(ctx context.Context, params listing.Params) (category.DataCategoryDTO, error) {
ns := []category.CategoryDTO{}
var (
dt category.DataCategoryDTO
err error
rows pgx.Rows
)
where := ""
args := []interface{}{}
if params.Search != "" {
where = ` where cs.name::text ILIKE $1 `
args = append(args, params.SearchPattern())
}
qC := `select count(*) from categories cs` + where + `;`
r.client.QueryRow(ctx, qC, args...).Scan(&dt.Count)
q := ` select
cs.Id, cs.name,
jsonb_agg(ss.name) as sub_category,
cs.status, cs.created_at
from categories cs
left join sub_categories ss on ss.category_id = cs.Id ` +
where +
` group by cs.Id ` +
params.OrderClause(sortColumns, "cs.created_at ASC")
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 category.CategoryDTO
errN := rows.Scan(
&n.Id, &n.Name, &n.SubName,
&n.Status, &n.CreatedAt,
)
if errN != nil {
r.logger.Error("errN :::", errN)
continue
}
ns = append(ns, n)
}
dt.Categories = ns
return dt, nil
}
func (r repository) GetData(ctx context.Context, id int) (category.CategoryDTO, error) {
var (
ns category.CategoryDTO
)
qC := `select id, name, status, created_at from categories where id = $1 ;`
err := r.client.QueryRow(ctx, qC, id).Scan(&ns.Id, &ns.Name, &ns.Status, &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 category.AddCategoryDTO) (category.CategoryIdDTO, error) {
var (
n category.CategoryIdDTO
)
q := `INSERT INTO categories (name) VALUES ($1) RETURNING id, name, status;`
err := r.client.QueryRow(ctx, q, req.Name).Scan(
&n.Id, &n.Name, &n.Status,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) UpdateData(ctx context.Context, req category.UpdateCategoryDTO, id int) (category.CategoryIdDTO, error) {
var (
q string
n category.CategoryIdDTO
)
q = ` update categories set name = $1 where Id = $2 RETURNING id, name, status;`
err := r.client.QueryRow(ctx, q, req.Name, id).Scan(
&n.Id, &n.Name, &n.Status,
)
if err != nil && err.Error() != "no rows in result set" {
return n, err
}
return n, nil
}
func (r repository) UpdateStatusData(ctx context.Context, status string, id int) (category.CategoryDTO, error) {
q := ` update 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 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
}