changed column name

This commit is contained in:
2026-07-06 19:13:54 +05:00
parent 3ba5597a51
commit 08be61cbcd
33 changed files with 544 additions and 2129 deletions

View File

@@ -24,16 +24,37 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) branches.Re
return &repository{client: client, logger: logger}
}
const branchColumns = `b.id, b.title, b.province_id, b.location_id, b.unikal_nomer, b.address,
b.billing_username, b.billing_password, b.billing_username_sber, b.billing_password_sber,
b.billing_username_visa, b.billing_password_visa,
b.billing_username_swift, b.billing_password_swift`
const branchFromClause = ` FROM branches b
LEFT JOIN provinces p ON p.id = b.province_id`
const branchSelectColumns = `b.id, b.unique_code, b.name, b.address, b.region, b.province_id, p.name,
b.phone_numbers, b.billing_username, b.billing_password,
b.billing_sber_username, b.billing_sber_password,
b.billing_visa_master_username, b.billing_visa_master_password,
b.billing_swift_username, b.billing_swift_password, b.active`
const branchReturningColumns = `id, unique_code, name, address, region, province_id, phone_numbers,
billing_username, billing_password, billing_sber_username, billing_sber_password,
billing_visa_master_username, billing_visa_master_password,
billing_swift_username, billing_swift_password, active`
const branchReturnedSelectColumns = `i.id, i.unique_code, i.name, i.address, i.region, i.province_id, p.name,
i.phone_numbers, i.billing_username, i.billing_password,
i.billing_sber_username, i.billing_sber_password,
i.billing_visa_master_username, i.billing_visa_master_password,
i.billing_swift_username, i.billing_swift_password, i.active`
const branchUpdatedSelectColumns = `u.id, u.unique_code, u.name, u.address, u.region, u.province_id, p.name,
u.phone_numbers, u.billing_username, u.billing_password,
u.billing_sber_username, u.billing_sber_password,
u.billing_visa_master_username, u.billing_visa_master_password,
u.billing_swift_username, u.billing_swift_password, u.active`
func (r *repository) List(ctx context.Context, filter branches.ListFilter) (branches.ListBranchDTO, error) {
fromClause, whereClause, args := buildListQuery(filter)
whereClause, args := buildListQuery(filter)
var count int64
countQuery := `SELECT COUNT(*) ` + fromClause + whereClause
countQuery := `SELECT COUNT(*)` + branchFromClause + whereClause
if err := r.client.QueryRow(ctx, countQuery, args...).Scan(&count); err != nil {
return branches.ListBranchDTO{}, err
}
@@ -43,7 +64,7 @@ func (r *repository) List(ctx context.Context, filter branches.ListFilter) (bran
offsetPlaceholder := len(args) + 2
listQuery := fmt.Sprintf(
`SELECT %s%s%s ORDER BY b.id LIMIT $%d OFFSET $%d`,
branchColumns, fromClause, whereClause, limitPlaceholder, offsetPlaceholder,
branchSelectColumns, branchFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
)
listArgs := append(args, filter.Limit, offset)
@@ -68,13 +89,7 @@ func (r *repository) List(ctx context.Context, filter branches.ListFilter) (bran
return branches.ListBranchDTO{Count: count, Branches: items}, nil
}
func buildListQuery(filter branches.ListFilter) (fromClause, whereClause string, args []interface{}) {
fromClause = ` FROM branches b`
if filter.ProvinceName != "" {
fromClause += ` JOIN provinces p ON p.id = b.province_id`
}
func buildListQuery(filter branches.ListFilter) (whereClause string, args []interface{}) {
var conditions []string
if filter.ProvinceID != nil {
@@ -82,16 +97,11 @@ func buildListQuery(filter branches.ListFilter) (fromClause, whereClause string,
conditions = append(conditions, fmt.Sprintf("b.province_id = $%d", len(args)))
}
if filter.LocationID != nil {
args = append(args, *filter.LocationID)
conditions = append(conditions, fmt.Sprintf("b.location_id = $%d", len(args)))
}
if filter.ProvinceName != "" {
args = append(args, likePattern(filter.ProvinceName))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`(
p.title->>'en' ILIKE $%[1]d OR p.title->>'ru' ILIKE $%[1]d OR p.title->>'tm' ILIKE $%[1]d
p.name->>'en' ILIKE $%[1]d OR p.name->>'ru' ILIKE $%[1]d OR p.name->>'tm' ILIKE $%[1]d
)`, n))
}
@@ -99,21 +109,21 @@ func buildListQuery(filter branches.ListFilter) (fromClause, whereClause string,
args = append(args, likePattern(filter.BranchName))
n := len(args)
conditions = append(conditions, fmt.Sprintf(`(
b.title->>'en' ILIKE $%[1]d OR b.title->>'ru' ILIKE $%[1]d OR b.title->>'tm' ILIKE $%[1]d
b.name->>'en' ILIKE $%[1]d OR b.name->>'ru' ILIKE $%[1]d OR b.name->>'tm' ILIKE $%[1]d
)`, n))
}
if filter.UnikalNomer != "" {
args = append(args, likePattern(filter.UnikalNomer))
if filter.UniqueCode != "" {
args = append(args, likePattern(filter.UniqueCode))
n := len(args)
conditions = append(conditions, fmt.Sprintf("b.unikal_nomer ILIKE $%d", n))
conditions = append(conditions, fmt.Sprintf("b.unique_code ILIKE $%d", n))
}
if len(conditions) == 0 {
return fromClause, "", args
return "", args
}
return fromClause, " WHERE " + strings.Join(conditions, " AND "), args
return " WHERE " + strings.Join(conditions, " AND "), args
}
func likePattern(term string) string {
@@ -124,7 +134,7 @@ func likePattern(term string) string {
}
func (r *repository) GetByID(ctx context.Context, id int64) (branches.BranchDTO, error) {
query := fmt.Sprintf(`SELECT %s FROM branches b WHERE b.id = $1`, branchColumns)
query := fmt.Sprintf(`SELECT %s%s WHERE b.id = $1`, branchSelectColumns, branchFromClause)
row := r.client.QueryRow(ctx, query, id)
item, err := scanBranch(row)
if errors.Is(err, pgx.ErrNoRows) {
@@ -134,68 +144,84 @@ func (r *repository) GetByID(ctx context.Context, id int64) (branches.BranchDTO,
}
func (r *repository) Create(ctx context.Context, dto branches.CreateBranchDTO) (branches.BranchDTO, error) {
if err := r.ensureRelations(ctx, dto.ProvinceId, dto.LocationId); err != nil {
if err := r.ensureProvince(ctx, dto.ProvinceId); err != nil {
return branches.BranchDTO{}, err
}
title, address, err := marshalBranchJSON(dto.Title, dto.Address)
name, address, phoneNumbers, err := marshalBranchJSON(dto.Name, dto.Address, dto.PhoneNumbers)
if err != nil {
return branches.BranchDTO{}, err
}
query := fmt.Sprintf(`
INSERT INTO branches (
title, province_id, location_id, unikal_nomer, address,
billing_username, billing_password, billing_username_sber, billing_password_sber,
billing_username_visa, billing_password_visa,
billing_username_swift, billing_password_swift
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13)
RETURNING %s`, branchColumns)
active := true
if dto.Active != nil {
active = *dto.Active
}
query := `
WITH inserted AS (
INSERT INTO branches (
unique_code, name, address, region, province_id, phone_numbers,
billing_username, billing_password, billing_sber_username, billing_sber_password,
billing_visa_master_username, billing_visa_master_password,
billing_swift_username, billing_swift_password, active
) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16)
RETURNING ` + branchReturningColumns + `
)
SELECT ` + branchReturnedSelectColumns + `
FROM inserted i
LEFT JOIN provinces p ON p.id = i.province_id`
row := r.client.QueryRow(ctx, query,
title, dto.ProvinceId, dto.LocationId, dto.UnikalNomer, address,
dto.BillingUsername, dto.BillingPassword, dto.BillingUsernameSber, dto.BillingPasswordSber,
dto.BillingUsernameVisa, dto.BillingPasswordVisa,
dto.BillingUsernameSwift, dto.BillingPasswordSwift,
nullIfEmpty(dto.UniqueCode), name, address, dto.Region, dto.ProvinceId, phoneNumbers,
dto.BillingUsername, dto.BillingPassword, dto.BillingSberUsername, dto.BillingSberPassword,
dto.BillingVisaMasterUsername, dto.BillingVisaMasterPassword,
dto.BillingSwiftUsername, dto.BillingSwiftPassword, active,
)
item, err := scanBranch(row)
if isUniqueViolation(err) {
return branches.BranchDTO{}, fmt.Errorf("unikal nomer already exists")
return branches.BranchDTO{}, fmt.Errorf("unique code already exists")
}
return item, err
}
func (r *repository) Update(ctx context.Context, id int64, dto branches.UpdateBranchDTO) (branches.BranchDTO, error) {
if err := r.ensureRelations(ctx, dto.ProvinceId, dto.LocationId); err != nil {
if err := r.ensureProvince(ctx, dto.ProvinceId); err != nil {
return branches.BranchDTO{}, err
}
title, address, err := marshalBranchJSON(dto.Title, dto.Address)
name, address, phoneNumbers, err := marshalBranchJSON(dto.Name, dto.Address, dto.PhoneNumbers)
if err != nil {
return branches.BranchDTO{}, err
}
query := fmt.Sprintf(`
UPDATE branches SET
title = $1, province_id = $2, location_id = $3, unikal_nomer = $4, address = $5,
billing_username = $6, billing_password = $7, billing_username_sber = $8,
billing_password_sber = $9, billing_username_visa = $10, billing_password_visa = $11,
billing_username_swift = $12, billing_password_swift = $13
WHERE id = $14
RETURNING %s`, branchColumns)
query := `
WITH updated AS (
UPDATE branches SET
unique_code = $1, name = $2, address = $3, region = $4, province_id = $5,
phone_numbers = $6, billing_username = $7, billing_password = $8,
billing_sber_username = $9, billing_sber_password = $10,
billing_visa_master_username = $11, billing_visa_master_password = $12,
billing_swift_username = $13, billing_swift_password = $14, active = $15
WHERE id = $16
RETURNING ` + branchReturningColumns + `
)
SELECT ` + branchUpdatedSelectColumns + `
FROM updated u
LEFT JOIN provinces p ON p.id = u.province_id`
row := r.client.QueryRow(ctx, query,
title, dto.ProvinceId, dto.LocationId, dto.UnikalNomer, address,
dto.BillingUsername, dto.BillingPassword, dto.BillingUsernameSber, dto.BillingPasswordSber,
dto.BillingUsernameVisa, dto.BillingPasswordVisa,
dto.BillingUsernameSwift, dto.BillingPasswordSwift, id,
nullIfEmpty(dto.UniqueCode), name, address, dto.Region, dto.ProvinceId, phoneNumbers,
dto.BillingUsername, dto.BillingPassword, dto.BillingSberUsername, dto.BillingSberPassword,
dto.BillingVisaMasterUsername, dto.BillingVisaMasterPassword,
dto.BillingSwiftUsername, dto.BillingSwiftPassword, dto.Active, id,
)
item, err := scanBranch(row)
if errors.Is(err, pgx.ErrNoRows) {
return branches.BranchDTO{}, err
}
if isUniqueViolation(err) {
return branches.BranchDTO{}, fmt.Errorf("unikal nomer already exists")
return branches.BranchDTO{}, fmt.Errorf("unique code already exists")
}
return item, err
}
@@ -211,27 +237,44 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
return nil
}
func (r *repository) ensureRelations(ctx context.Context, provinceID, locationID int64) error {
func (r *repository) ensureProvince(ctx context.Context, provinceID *int64) error {
if provinceID == nil {
return nil
}
var id int64
err := r.client.QueryRow(ctx,
`SELECT id FROM location WHERE id = $1 AND province_id = $2`, locationID, provinceID,
`SELECT id FROM provinces WHERE id = $1`, *provinceID,
).Scan(&id)
if errors.Is(err, pgx.ErrNoRows) {
return fmt.Errorf("location not found in province")
return fmt.Errorf("province not found")
}
return err
}
func marshalBranchJSON(title, address branches.LocalizedText) ([]byte, []byte, error) {
titleJSON, err := json.Marshal(title)
func marshalBranchJSON(name, address branches.LocalizedText, phoneNumbers []string) ([]byte, []byte, []byte, error) {
nameJSON, err := json.Marshal(name)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
addressJSON, err := json.Marshal(address)
if err != nil {
return nil, nil, err
return nil, nil, nil, err
}
return titleJSON, addressJSON, nil
if phoneNumbers == nil {
phoneNumbers = []string{}
}
phoneNumbersJSON, err := json.Marshal(phoneNumbers)
if err != nil {
return nil, nil, nil, err
}
return nameJSON, addressJSON, phoneNumbersJSON, nil
}
func nullIfEmpty(value string) interface{} {
if value == "" {
return nil
}
return value
}
func isUniqueViolation(err error) bool {
@@ -245,22 +288,39 @@ type scannable interface {
func scanBranch(row scannable) (branches.BranchDTO, error) {
var item branches.BranchDTO
var titleJSON, addressJSON []byte
var nameJSON, addressJSON, provinceNameJSON, phoneNumbersJSON []byte
var uniqueCode *string
err := row.Scan(
&item.Id, &titleJSON, &item.ProvinceId, &item.LocationId, &item.UnikalNomer, &addressJSON,
&item.BillingUsername, &item.BillingPassword, &item.BillingUsernameSber, &item.BillingPasswordSber,
&item.BillingUsernameVisa, &item.BillingPasswordVisa,
&item.BillingUsernameSwift, &item.BillingPasswordSwift,
&item.Id, &uniqueCode, &nameJSON, &addressJSON, &item.Region, &item.ProvinceId, &provinceNameJSON,
&phoneNumbersJSON,
&item.BillingUsername, &item.BillingPassword,
&item.BillingSberUsername, &item.BillingSberPassword,
&item.BillingVisaMasterUsername, &item.BillingVisaMasterPassword,
&item.BillingSwiftUsername, &item.BillingSwiftPassword, &item.Active,
)
if err != nil {
return branches.BranchDTO{}, err
}
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
if uniqueCode != nil {
item.UniqueCode = *uniqueCode
}
if err := json.Unmarshal(nameJSON, &item.Name); err != nil {
return branches.BranchDTO{}, err
}
if err := json.Unmarshal(addressJSON, &item.Address); err != nil {
return branches.BranchDTO{}, err
}
if provinceNameJSON != nil {
if err := json.Unmarshal(provinceNameJSON, &item.ProvinceName); err != nil {
return branches.BranchDTO{}, err
}
}
if err := json.Unmarshal(phoneNumbersJSON, &item.PhoneNumbers); err != nil {
return branches.BranchDTO{}, err
}
if item.PhoneNumbers == nil {
item.PhoneNumbers = []string{}
}
return item, nil
}