changed column name
This commit is contained in:
@@ -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, ®ionJSON, &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
|
||||
|
||||
@@ -1,23 +1,26 @@
|
||||
package province
|
||||
|
||||
type LocalizedTitle struct {
|
||||
type LocalizedName struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type ProvinceDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Title LocalizedTitle `json:"title"`
|
||||
Status bool `json:"status"`
|
||||
Id int64 `json:"id"`
|
||||
Name LocalizedName `json:"name"`
|
||||
Region LocalizedName `json:"region"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type CreateProvinceDTO struct {
|
||||
Title LocalizedTitle `json:"title" binding:"required"`
|
||||
Status *bool `json:"status"`
|
||||
Name LocalizedName `json:"name" binding:"required"`
|
||||
Region LocalizedName `json:"region" binding:"required"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateProvinceDTO struct {
|
||||
Title LocalizedTitle `json:"title" binding:"required"`
|
||||
Status bool `json:"status"`
|
||||
Name LocalizedName `json:"name" binding:"required"`
|
||||
Region LocalizedName `json:"region" binding:"required"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
@@ -159,7 +159,7 @@ func (h *handler) update(c *gin.Context) {
|
||||
|
||||
// delete godoc
|
||||
// @Summary Delete province
|
||||
// @Description Deletes a province and its locations
|
||||
// @Description Deletes a province
|
||||
// @Tags province
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
|
||||
Reference in New Issue
Block a user