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

@@ -21,8 +21,10 @@ func NewRepository(client postgresql.Client, logger *logging.Logger) province.Re
return &repository{client: client, logger: logger}
}
const provinceSelectColumns = `id, name, region, active`
func (r *repository) List(ctx context.Context) ([]province.ProvinceDTO, error) {
rows, err := r.client.Query(ctx, `SELECT id, title, status FROM provinces ORDER BY id`)
rows, err := r.client.Query(ctx, `SELECT `+provinceSelectColumns+` FROM provinces ORDER BY id`)
if err != nil {
return nil, err
}
@@ -41,7 +43,7 @@ func (r *repository) List(ctx context.Context) ([]province.ProvinceDTO, error) {
}
func (r *repository) GetByID(ctx context.Context, id int64) (province.ProvinceDTO, error) {
row := r.client.QueryRow(ctx, `SELECT id, title, status FROM provinces WHERE id = $1`, id)
row := r.client.QueryRow(ctx, `SELECT `+provinceSelectColumns+` FROM provinces WHERE id = $1`, id)
item, err := scanProvince(row)
if errors.Is(err, pgx.ErrNoRows) {
return province.ProvinceDTO{}, err
@@ -50,32 +52,32 @@ func (r *repository) GetByID(ctx context.Context, id int64) (province.ProvinceDT
}
func (r *repository) Create(ctx context.Context, dto province.CreateProvinceDTO) (province.ProvinceDTO, error) {
title, err := json.Marshal(dto.Title)
name, region, err := marshalProvinceJSON(dto.Name, dto.Region)
if err != nil {
return province.ProvinceDTO{}, err
}
status := true
if dto.Status != nil {
status = *dto.Status
active := true
if dto.Active != nil {
active = *dto.Active
}
row := r.client.QueryRow(ctx,
`INSERT INTO provinces (title, status) VALUES ($1, $2) RETURNING id, title, status`,
title, status,
`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) {
title, err := json.Marshal(dto.Title)
name, region, err := marshalProvinceJSON(dto.Name, dto.Region)
if err != nil {
return province.ProvinceDTO{}, err
}
row := r.client.QueryRow(ctx,
`UPDATE provinces SET title = $1, status = $2 WHERE id = $3 RETURNING id, title, status`,
title, dto.Status, id,
`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) {
@@ -95,17 +97,32 @@ func (r *repository) Delete(ctx context.Context, id int64) error {
return nil
}
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
}
type scannable interface {
Scan(dest ...interface{}) error
}
func scanProvince(row scannable) (province.ProvinceDTO, error) {
var item province.ProvinceDTO
var titleJSON []byte
if err := row.Scan(&item.Id, &titleJSON, &item.Status); err != nil {
var nameJSON, regionJSON []byte
if err := row.Scan(&item.Id, &nameJSON, &regionJSON, &item.Active); err != nil {
return province.ProvinceDTO{}, err
}
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
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