152 lines
4.1 KiB
Go
152 lines
4.1 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"online-back/internal/org/province"
|
|
"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[province.ProvinceDTO]
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) province.Repository {
|
|
return &repository{
|
|
client: client,
|
|
generic: genericrepo.New(client, "provinces", provinceSelectColumns, scanProvince),
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
const provinceSelectColumns = `id, name, region, active`
|
|
|
|
func (r *repository) List(ctx context.Context, filter province.ListFilter) (province.ListProvinceDTO, error) {
|
|
whereClause, args := buildListQuery(filter)
|
|
|
|
items, count, err := r.generic.List(ctx, whereClause, args, filter.Page, filter.Limit, "id")
|
|
if err != nil {
|
|
return province.ListProvinceDTO{}, err
|
|
}
|
|
|
|
return province.ListProvinceDTO{Count: count, Provinces: items}, nil
|
|
}
|
|
|
|
func buildListQuery(filter province.ListFilter) (whereClause string, args []interface{}) {
|
|
var conditions []string
|
|
|
|
if filter.Name != "" {
|
|
args = append(args, likePattern(filter.Name))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
name->>'en' ILIKE $%[1]d OR name->>'ru' ILIKE $%[1]d OR name->>'tm' ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if filter.Region != "" {
|
|
args = append(args, likePattern(filter.Region))
|
|
n := len(args)
|
|
conditions = append(conditions, fmt.Sprintf(`(
|
|
region->>'en' ILIKE $%[1]d OR region->>'ru' ILIKE $%[1]d OR region->>'tm' ILIKE $%[1]d
|
|
)`, n))
|
|
}
|
|
|
|
if filter.Active != nil {
|
|
args = append(args, *filter.Active)
|
|
conditions = append(conditions, fmt.Sprintf("active = $%d", len(args)))
|
|
}
|
|
|
|
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) (province.ProvinceDTO, error) {
|
|
return r.generic.GetByID(ctx, id)
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto province.CreateProvinceDTO) (province.ProvinceDTO, error) {
|
|
name, region, err := marshalProvinceJSON(dto.Name, dto.Region)
|
|
if err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
|
|
active := true
|
|
if dto.Active != nil {
|
|
active = *dto.Active
|
|
}
|
|
|
|
row := r.client.QueryRow(ctx,
|
|
`INSERT INTO provinces (name, region, active) VALUES ($1, $2, $3) RETURNING `+provinceSelectColumns,
|
|
name, region, active,
|
|
)
|
|
return scanProvince(row)
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto province.UpdateProvinceDTO) (province.ProvinceDTO, error) {
|
|
name, region, err := marshalProvinceJSON(dto.Name, dto.Region)
|
|
if err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
|
|
row := r.client.QueryRow(ctx,
|
|
`UPDATE provinces SET name = $1, region = $2, active = $3 WHERE id = $4 RETURNING `+provinceSelectColumns,
|
|
name, region, dto.Active, id,
|
|
)
|
|
item, err := scanProvince(row)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Delete(ctx context.Context, id int64) error {
|
|
return r.generic.Delete(ctx, id)
|
|
}
|
|
|
|
func marshalProvinceJSON(name, region province.LocalizedName) ([]byte, []byte, error) {
|
|
nameJSON, err := json.Marshal(name)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
regionJSON, err := json.Marshal(region)
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
return nameJSON, regionJSON, nil
|
|
}
|
|
|
|
func scanProvince(row genericrepo.Scanner) (province.ProvinceDTO, error) {
|
|
var item province.ProvinceDTO
|
|
var nameJSON, regionJSON []byte
|
|
if err := row.Scan(&item.Id, &nameJSON, ®ionJSON, &item.Active); err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(nameJSON, &item.Name); err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(regionJSON, &item.Region); err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|