Expose distinct regions and etraps-by-region under provinces and branches, and seed real etraps per welayat. Co-authored-by: Cursor <cursoragent@cursor.com>
222 lines
6.1 KiB
Go
222 lines
6.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 (r *repository) ListRegions(ctx context.Context, activeOnly bool) (province.ListRegionsDTO, error) {
|
|
query := `SELECT DISTINCT region FROM provinces`
|
|
var args []interface{}
|
|
if activeOnly {
|
|
query += ` WHERE active = $1`
|
|
args = append(args, true)
|
|
}
|
|
query += ` ORDER BY region->>'tm', region->>'en', region->>'ru'`
|
|
|
|
rows, err := r.client.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return province.ListRegionsDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
regions := make([]province.LocalizedName, 0)
|
|
for rows.Next() {
|
|
var regionJSON []byte
|
|
if err := rows.Scan(®ionJSON); err != nil {
|
|
return province.ListRegionsDTO{}, err
|
|
}
|
|
var region province.LocalizedName
|
|
if err := json.Unmarshal(regionJSON, ®ion); err != nil {
|
|
return province.ListRegionsDTO{}, err
|
|
}
|
|
regions = append(regions, region)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return province.ListRegionsDTO{}, err
|
|
}
|
|
|
|
return province.ListRegionsDTO{Regions: regions}, nil
|
|
}
|
|
|
|
func (r *repository) ListEtrapsByRegion(ctx context.Context, region string, activeOnly bool) (province.ListEtrapsDTO, error) {
|
|
args := []interface{}{region}
|
|
conditions := []string{`(
|
|
LOWER(region->>'en') = LOWER($1)
|
|
OR LOWER(region->>'ru') = LOWER($1)
|
|
OR LOWER(region->>'tm') = LOWER($1)
|
|
)`}
|
|
if activeOnly {
|
|
args = append(args, true)
|
|
conditions = append(conditions, fmt.Sprintf("active = $%d", len(args)))
|
|
}
|
|
|
|
where := " WHERE " + strings.Join(conditions, " AND ")
|
|
query := `SELECT ` + provinceSelectColumns + ` FROM provinces` + where + ` ORDER BY name->>'tm', name->>'en', id`
|
|
|
|
rows, err := r.client.Query(ctx, query, args...)
|
|
if err != nil {
|
|
return province.ListEtrapsDTO{}, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
etraps := make([]province.ProvinceDTO, 0)
|
|
for rows.Next() {
|
|
item, err := scanProvince(rows)
|
|
if err != nil {
|
|
return province.ListEtrapsDTO{}, err
|
|
}
|
|
etraps = append(etraps, item)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return province.ListEtrapsDTO{}, err
|
|
}
|
|
|
|
return province.ListEtrapsDTO{Count: int64(len(etraps)), Etraps: etraps}, 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
|
|
}
|