Files
online-backend/internal/cards/reasonscard/db/postgresql.go
2026-07-15 18:08:19 +05:00

136 lines
3.8 KiB
Go

package db
import (
"context"
"encoding/json"
"errors"
"fmt"
"strings"
"online-back/internal/cards/reasonscard"
"online-back/pkg/client/postgresql"
"online-back/pkg/logging"
"online-back/pkg/repository/genericrepo"
"github.com/jackc/pgx/v4"
)
type repository struct {
client postgresql.Client
generic *genericrepo.Repository[reasonscard.ReasonCardDTO]
logger *logging.Logger
}
func NewRepository(client postgresql.Client, logger *logging.Logger) reasonscard.Repository {
return &repository{
client: client,
generic: genericrepo.New(client, "card_states r", reasonColumns, scanReasonCard),
logger: logger,
}
}
const reasonColumns = `r.id, r.price, r.name, r.notes, r.active, r.created_at, r.updated_at`
const reasonReturningColumns = `id, price, name, notes, active, created_at, updated_at`
func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (reasonscard.ListReasonCardDTO, error) {
whereClause, args := buildListQuery(filter)
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
if err != nil {
return reasonscard.ListReasonCardDTO{}, err
}
return reasonscard.ListReasonCardDTO{Count: count, ReasonCards: items}, nil
}
func buildListQuery(filter reasonscard.ListFilter) (whereClause string, args []interface{}) {
var conditions []string
if filter.ReasonName != "" {
args = append(args, likePattern(filter.ReasonName))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`(
r.name->>'en' ILIKE $%[1]d OR r.name->>'ru' ILIKE $%[1]d OR r.name->>'tm' ILIKE $%[1]d
)`, n))
}
if len(conditions) == 0 {
return "", args
}
return " WHERE " + strings.Join(conditions, " AND "), args
}
func likePattern(term string) string {
term = strings.ReplaceAll(term, `\`, `\\`)
term = strings.ReplaceAll(term, `%`, `\%`)
term = strings.ReplaceAll(term, `_`, `\_`)
return "%" + term + "%"
}
func (r *repository) GetByID(ctx context.Context, id int64) (reasonscard.ReasonCardDTO, error) {
return r.generic.GetByID(ctx, id)
}
func (r *repository) Create(ctx context.Context, dto reasonscard.CreateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
name, err := marshalName(dto.Title)
if err != nil {
return reasonscard.ReasonCardDTO{}, err
}
active := true
if dto.Active != nil {
active = *dto.Active
}
query := fmt.Sprintf(`
INSERT INTO card_states (price, name, notes, active)
VALUES ($1, $2, $3, $4)
RETURNING %s`, reasonReturningColumns)
return scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, active))
}
func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.UpdateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
name, err := marshalName(dto.Title)
if err != nil {
return reasonscard.ReasonCardDTO{}, err
}
query := fmt.Sprintf(`
UPDATE card_states SET
price = $1, name = $2, notes = $3, active = $4,
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
WHERE id = $5
RETURNING %s`, reasonReturningColumns)
item, err := scanReasonCard(r.client.QueryRow(ctx, query, dto.Price, name, dto.Notes, dto.Active, id))
if errors.Is(err, pgx.ErrNoRows) {
return reasonscard.ReasonCardDTO{}, err
}
return item, err
}
func (r *repository) Delete(ctx context.Context, id int64) error {
return r.generic.Delete(ctx, id)
}
func marshalName(title reasonscard.LocalizedText) ([]byte, error) {
return json.Marshal(title)
}
func scanReasonCard(row genericrepo.Scanner) (reasonscard.ReasonCardDTO, error) {
var item reasonscard.ReasonCardDTO
var nameJSON []byte
if err := row.Scan(
&item.Id, &item.Price, &nameJSON, &item.Notes, &item.Active, &item.CreatedAt, &item.UpdatedAt,
); err != nil {
return reasonscard.ReasonCardDTO{}, err
}
if err := json.Unmarshal(nameJSON, &item.Title); err != nil {
return reasonscard.ReasonCardDTO{}, err
}
return item, nil
}