178 lines
5.0 KiB
Go
178 lines
5.0 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/admin/reasonscard"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) reasonscard.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
const reasonColumns = `r.id, r.title, r.price, r.content, r.created_at, r.updated_at`
|
|
|
|
func (r *repository) List(ctx context.Context, filter reasonscard.ListFilter) (reasonscard.ListReasonCardDTO, error) {
|
|
fromClause, whereClause, args := buildListQuery(filter)
|
|
|
|
var count int64
|
|
if err := r.client.QueryRow(ctx, `SELECT COUNT(*) `+fromClause+whereClause, args...).Scan(&count); err != nil {
|
|
return reasonscard.ListReasonCardDTO{}, err
|
|
}
|
|
|
|
offset := (filter.Page - 1) * filter.Limit
|
|
limitPlaceholder := len(args) + 1
|
|
offsetPlaceholder := len(args) + 2
|
|
listQuery := fmt.Sprintf(
|
|
`SELECT %s%s%s ORDER BY r.id LIMIT $%d OFFSET $%d`,
|
|
reasonColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
|
)
|
|
|
|
rows, err := r.client.Query(ctx, listQuery, append(args, filter.Limit, offset)...)
|
|
if err != nil {
|
|
return reasonscard.ListReasonCardDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []reasonscard.ReasonCardDTO
|
|
for rows.Next() {
|
|
item, err := scanReasonCard(rows)
|
|
if err != nil {
|
|
return reasonscard.ListReasonCardDTO{}, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return reasonscard.ListReasonCardDTO{}, err
|
|
}
|
|
|
|
return reasonscard.ListReasonCardDTO{Count: count, ReasonCards: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter reasonscard.ListFilter) (fromClause, whereClause string, args []interface{}) {
|
|
fromClause = ` FROM reasons_cards r`
|
|
|
|
var conditions []string
|
|
if filter.ReasonName != "" {
|
|
args = append(args, likePattern(filter.ReasonName))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
r.title->>'en' ILIKE $%[1]d OR r.title->>'ru' ILIKE $%[1]d OR r.title->>'tm' ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if len(conditions) == 0 {
|
|
return fromClause, "", args
|
|
}
|
|
|
|
return fromClause, " 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) {
|
|
query := fmt.Sprintf(`SELECT %s FROM reasons_cards r WHERE r.id = $1`, reasonColumns)
|
|
item, err := scanReasonCard(r.client.QueryRow(ctx, query, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto reasonscard.CreateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
|
|
title, content, err := marshalJSON(dto.Title, dto.Content)
|
|
if err != nil {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
INSERT INTO reasons_cards (title, price, content)
|
|
VALUES ($1, $2, $3)
|
|
RETURNING %s`, reasonColumns)
|
|
|
|
return scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content))
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto reasonscard.UpdateReasonCardDTO) (reasonscard.ReasonCardDTO, error) {
|
|
title, content, err := marshalJSON(dto.Title, dto.Content)
|
|
if err != nil {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
|
|
query := fmt.Sprintf(`
|
|
UPDATE reasons_cards SET
|
|
title = $1, price = $2, content = $3,
|
|
updated_at = (CURRENT_TIMESTAMP AT TIME ZONE 'UTC')
|
|
WHERE id = $4
|
|
RETURNING %s`, reasonColumns)
|
|
|
|
item, err := scanReasonCard(r.client.QueryRow(ctx, query, title, dto.Price, content, id))
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM reasons_cards WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return pgx.ErrNoRows
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func marshalJSON(title, content reasonscard.LocalizedText) ([]byte, []byte, error) {
|
|
titleJSON, err := json.Marshal(title)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
contentJSON, err := json.Marshal(content)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return titleJSON, contentJSON, nil
|
|
}
|
|
|
|
type scannable interface {
|
|
Scan(dest ...interface{}) error
|
|
}
|
|
|
|
func scanReasonCard(row scannable) (reasonscard.ReasonCardDTO, error) {
|
|
var item reasonscard.ReasonCardDTO
|
|
var titleJSON, contentJSON []byte
|
|
|
|
if err := row.Scan(
|
|
&item.Id, &titleJSON, &item.Price, &contentJSON, &item.CreatedAt, &item.UpdatedAt,
|
|
); err != nil {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(contentJSON, &item.Content); err != nil {
|
|
return reasonscard.ReasonCardDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|