Rename users table fields and loan documents table, expand branch billing credentials, switch to /api/admin prefix, refactor auth endpoints, replace PATCH with PUT for status updates, remove file upload APIs, and regenerate Swagger with ordered tags. Co-authored-by: Cursor <cursoragent@cursor.com>
113 lines
2.7 KiB
Go
113 lines
2.7 KiB
Go
package db
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
|
|
"online-back/internal/admin/province"
|
|
"online-back/pkg/client/postgresql"
|
|
"online-back/pkg/logging"
|
|
|
|
"github.com/jackc/pgx/v4"
|
|
)
|
|
|
|
type repository struct {
|
|
client postgresql.Client
|
|
logger *logging.Logger
|
|
}
|
|
|
|
func NewRepository(client postgresql.Client, logger *logging.Logger) province.Repository {
|
|
return &repository{client: client, logger: logger}
|
|
}
|
|
|
|
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`)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var items []province.ProvinceDTO
|
|
for rows.Next() {
|
|
item, err := scanProvince(rows)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, item)
|
|
}
|
|
|
|
return items, rows.Err()
|
|
}
|
|
|
|
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)
|
|
item, err := scanProvince(row)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
return item, err
|
|
}
|
|
|
|
func (r *repository) Create(ctx context.Context, dto province.CreateProvinceDTO) (province.ProvinceDTO, error) {
|
|
title, err := json.Marshal(dto.Title)
|
|
if err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
|
|
status := true
|
|
if dto.Status != nil {
|
|
status = *dto.Status
|
|
}
|
|
|
|
row := r.client.QueryRow(ctx,
|
|
`INSERT INTO provinces (title, status) VALUES ($1, $2) RETURNING id, title, status`,
|
|
title, status,
|
|
)
|
|
return scanProvince(row)
|
|
}
|
|
|
|
func (r *repository) Update(ctx context.Context, id int64, dto province.UpdateProvinceDTO) (province.ProvinceDTO, error) {
|
|
title, err := json.Marshal(dto.Title)
|
|
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,
|
|
)
|
|
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 {
|
|
tag, err := r.client.Exec(ctx, `DELETE FROM provinces WHERE id = $1`, id)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if tag.RowsAffected() == 0 {
|
|
return pgx.ErrNoRows
|
|
}
|
|
return 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 {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
if err := json.Unmarshal(titleJSON, &item.Title); err != nil {
|
|
return province.ProvinceDTO{}, err
|
|
}
|
|
return item, nil
|
|
}
|