changed arhitecture
This commit is contained in:
308
internal/org/branches/db/postgresql.go
Normal file
308
internal/org/branches/db/postgresql.go
Normal file
@@ -0,0 +1,308 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/org/branches"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
"online-back/pkg/repository/genericrepo"
|
||||
|
||||
"github.com/jackc/pgconn"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
type repository struct {
|
||||
client postgresql.Client
|
||||
generic *genericrepo.Repository[branches.BranchDTO]
|
||||
logger *logging.Logger
|
||||
}
|
||||
|
||||
func NewRepository(client postgresql.Client, logger *logging.Logger) branches.Repository {
|
||||
return &repository{
|
||||
client: client,
|
||||
generic: genericrepo.New(client, "branches", branchSelectColumns, scanBranch),
|
||||
logger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
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) {
|
||||
whereClause, args := buildListQuery(filter)
|
||||
|
||||
var count int64
|
||||
countQuery := `SELECT COUNT(*)` + branchFromClause + whereClause
|
||||
if err := r.client.QueryRow(ctx, countQuery, args...).Scan(&count); err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
|
||||
offset := (filter.Page - 1) * filter.Limit
|
||||
limitPlaceholder := len(args) + 1
|
||||
offsetPlaceholder := len(args) + 2
|
||||
listQuery := fmt.Sprintf(
|
||||
`SELECT %s%s%s ORDER BY b.id LIMIT $%d OFFSET $%d`,
|
||||
branchSelectColumns, branchFromClause, whereClause, limitPlaceholder, offsetPlaceholder,
|
||||
)
|
||||
listArgs := append(args, filter.Limit, offset)
|
||||
|
||||
rows, err := r.client.Query(ctx, listQuery, listArgs...)
|
||||
if err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var items []branches.BranchDTO
|
||||
for rows.Next() {
|
||||
item, err := scanBranch(rows)
|
||||
if err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
items = append(items, item)
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return branches.ListBranchDTO{}, err
|
||||
}
|
||||
|
||||
return branches.ListBranchDTO{Count: count, Branches: items}, nil
|
||||
}
|
||||
|
||||
func buildListQuery(filter branches.ListFilter) (whereClause string, args []interface{}) {
|
||||
var conditions []string
|
||||
|
||||
if filter.ProvinceID != nil {
|
||||
args = append(args, *filter.ProvinceID)
|
||||
conditions = append(conditions, fmt.Sprintf("b.province_id = $%d", len(args)))
|
||||
}
|
||||
|
||||
if filter.ProvinceName != "" {
|
||||
args = append(args, likePattern(filter.ProvinceName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
p.name->>'en' ILIKE $%[1]d OR p.name->>'ru' ILIKE $%[1]d OR p.name->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.BranchName != "" {
|
||||
args = append(args, likePattern(filter.BranchName))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
b.name->>'en' ILIKE $%[1]d OR b.name->>'ru' ILIKE $%[1]d OR b.name->>'tm' ILIKE $%[1]d
|
||||
)`, n))
|
||||
}
|
||||
|
||||
if filter.UniqueCode != "" {
|
||||
args = append(args, likePattern(filter.UniqueCode))
|
||||
n := len(args)
|
||||
conditions = append(conditions, fmt.Sprintf("b.unique_code ILIKE $%d", n))
|
||||
}
|
||||
|
||||
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) (branches.BranchDTO, error) {
|
||||
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) {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Create(ctx context.Context, dto branches.CreateBranchDTO) (branches.BranchDTO, error) {
|
||||
if err := r.ensureProvince(ctx, dto.ProvinceId); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
name, address, err := marshalBranchJSON(dto.Name, dto.Address)
|
||||
if err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
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)
|
||||
RETURNING ` + branchReturningColumns + `
|
||||
)
|
||||
SELECT ` + branchReturnedSelectColumns + `
|
||||
FROM inserted i
|
||||
LEFT JOIN provinces p ON p.id = i.province_id`
|
||||
|
||||
row := r.client.QueryRow(ctx, query,
|
||||
nullIfEmpty(dto.UniqueCode), name, address, dto.Region, dto.ProvinceId, dto.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("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.ensureProvince(ctx, dto.ProvinceId); err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
name, address, err := marshalBranchJSON(dto.Name, dto.Address)
|
||||
if err != nil {
|
||||
return branches.BranchDTO{}, err
|
||||
}
|
||||
|
||||
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,
|
||||
nullIfEmpty(dto.UniqueCode), name, address, dto.Region, dto.ProvinceId, dto.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("unique code already exists")
|
||||
}
|
||||
return item, err
|
||||
}
|
||||
|
||||
func (r *repository) Delete(ctx context.Context, id int64) error {
|
||||
return r.generic.Delete(ctx, id)
|
||||
}
|
||||
|
||||
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 provinces WHERE id = $1`, *provinceID,
|
||||
).Scan(&id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return fmt.Errorf("province not found")
|
||||
}
|
||||
return err
|
||||
}
|
||||
|
||||
func marshalBranchJSON(name, address branches.LocalizedText) ([]byte, []byte, error) {
|
||||
nameJSON, err := json.Marshal(name)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
addressJSON, err := json.Marshal(address)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
return nameJSON, addressJSON, nil
|
||||
}
|
||||
|
||||
func nullIfEmpty(value string) interface{} {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error) bool {
|
||||
var pgErr *pgconn.PgError
|
||||
return errors.As(err, &pgErr) && pgErr.Code == "23505"
|
||||
}
|
||||
|
||||
func scanBranch(row genericrepo.Scanner) (branches.BranchDTO, error) {
|
||||
var item branches.BranchDTO
|
||||
var nameJSON, addressJSON, provinceNameJSON []byte
|
||||
var uniqueCode *string
|
||||
|
||||
err := row.Scan(
|
||||
&item.Id, &uniqueCode, &nameJSON, &addressJSON, &item.Region, &item.ProvinceId, &provinceNameJSON,
|
||||
&item.PhoneNumbers,
|
||||
&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 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
|
||||
}
|
||||
}
|
||||
return item, nil
|
||||
}
|
||||
77
internal/org/branches/dto.go
Normal file
77
internal/org/branches/dto.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package branches
|
||||
|
||||
type LocalizedText struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type BranchDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name LocalizedText `json:"name"`
|
||||
Address LocalizedText `json:"address"`
|
||||
Region string `json:"region"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
ProvinceName LocalizedText `json:"provinceName"`
|
||||
PhoneNumbers string `json:"phoneNumbers"`
|
||||
BillingUsername string `json:"billingUsername"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingSberUsername string `json:"billingSberUsername"`
|
||||
BillingSberPassword string `json:"billingSberPassword"`
|
||||
BillingVisaMasterUsername string `json:"billingVisaMasterUsername"`
|
||||
BillingVisaMasterPassword string `json:"billingVisaMasterPassword"`
|
||||
BillingSwiftUsername string `json:"billingSwiftUsername"`
|
||||
BillingSwiftPassword string `json:"billingSwiftPassword"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type CreateBranchDTO struct {
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name LocalizedText `json:"name" binding:"required"`
|
||||
Address LocalizedText `json:"address" binding:"required"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
PhoneNumbers string `json:"phoneNumbers"`
|
||||
BillingUsername string `json:"billingUsername"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingSberUsername string `json:"billingSberUsername"`
|
||||
BillingSberPassword string `json:"billingSberPassword"`
|
||||
BillingVisaMasterUsername string `json:"billingVisaMasterUsername"`
|
||||
BillingVisaMasterPassword string `json:"billingVisaMasterPassword"`
|
||||
BillingSwiftUsername string `json:"billingSwiftUsername"`
|
||||
BillingSwiftPassword string `json:"billingSwiftPassword"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateBranchDTO struct {
|
||||
UniqueCode string `json:"uniqueCode"`
|
||||
Name LocalizedText `json:"name" binding:"required"`
|
||||
Address LocalizedText `json:"address" binding:"required"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
ProvinceId *int64 `json:"provinceId"`
|
||||
PhoneNumbers string `json:"phoneNumbers"`
|
||||
BillingUsername string `json:"billingUsername"`
|
||||
BillingPassword string `json:"billingPassword"`
|
||||
BillingSberUsername string `json:"billingSberUsername"`
|
||||
BillingSberPassword string `json:"billingSberPassword"`
|
||||
BillingVisaMasterUsername string `json:"billingVisaMasterUsername"`
|
||||
BillingVisaMasterPassword string `json:"billingVisaMasterPassword"`
|
||||
BillingSwiftUsername string `json:"billingSwiftUsername"`
|
||||
BillingSwiftPassword string `json:"billingSwiftPassword"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
ProvinceID *int64
|
||||
ProvinceName string
|
||||
BranchName string
|
||||
UniqueCode string
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListBranchDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
Branches []BranchDTO `json:"branches"`
|
||||
}
|
||||
274
internal/org/branches/handler.go
Normal file
274
internal/org/branches/handler.go
Normal file
@@ -0,0 +1,274 @@
|
||||
package branches
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
const baseURL = "/branches"
|
||||
|
||||
type handler struct {
|
||||
logger *logging.Logger
|
||||
repository Repository
|
||||
}
|
||||
|
||||
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
||||
return &handler{logger: logger, repository: repository}
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group.GET("", h.list)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
group.DELETE("/:id", h.delete)
|
||||
}
|
||||
|
||||
// list godoc
|
||||
// @Summary List branches
|
||||
// @Description Returns paginated branches with optional filters
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param province_id query int false "Filter by province ID"
|
||||
// @Param province_name query string false "Filter by province name (en, ru, tm)"
|
||||
// @Param branch_name query string false "Filter by branch name (en, ru, tm)"
|
||||
// @Param unique_code query string false "Filter by unique code"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches [get]
|
||||
func (h *handler) list(c *gin.Context) {
|
||||
filter, ok := parseListFilter(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.List(c.Request.Context(), filter)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list branches failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// getByID godoc
|
||||
// @Summary Get branch by ID
|
||||
// @Description Returns a single branch
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [get]
|
||||
func (h *handler) getByID(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("get branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// create godoc
|
||||
// @Summary Create branch
|
||||
// @Description Creates a new branch
|
||||
// @Tags branches
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param body body CreateBranchDTO true "Branch payload"
|
||||
// @Success 201 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
var dto CreateBranchDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
if isClientError(err) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("create branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// update godoc
|
||||
// @Summary Update branch
|
||||
// @Description Updates an existing branch
|
||||
// @Tags branches
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Param body body UpdateBranchDTO true "Branch payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [put]
|
||||
func (h *handler) update(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateBranchDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
if isClientError(err) {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
h.logger.Errorf("update branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// delete godoc
|
||||
// @Summary Delete branch
|
||||
// @Description Deletes a branch
|
||||
// @Tags branches
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Branch ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /branches/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
h.logger.Errorf("delete branch failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("branch removed"))
|
||||
}
|
||||
|
||||
func isClientError(err error) bool {
|
||||
msg := err.Error()
|
||||
return msg == "province not found" || msg == "unique code already exists"
|
||||
}
|
||||
|
||||
func parseID(raw string) (int64, bool) {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
const (
|
||||
defaultPage = 1
|
||||
defaultLimit = 10
|
||||
maxLimit = 100
|
||||
)
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter := ListFilter{
|
||||
Page: defaultPage,
|
||||
Limit: defaultLimit,
|
||||
}
|
||||
|
||||
if raw := c.Query("province_id"); raw != "" {
|
||||
id, ok := parseID(raw)
|
||||
if !ok {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.ProvinceID = &id
|
||||
}
|
||||
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Page = page
|
||||
}
|
||||
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxLimit {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("province_name")); raw != "" {
|
||||
filter.ProvinceName = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("branch_name")); raw != "" {
|
||||
filter.BranchName = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("unique_code")); raw != "" {
|
||||
filter.UniqueCode = raw
|
||||
}
|
||||
|
||||
return filter, true
|
||||
}
|
||||
11
internal/org/branches/storage.go
Normal file
11
internal/org/branches/storage.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package branches
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
List(ctx context.Context, filter ListFilter) (ListBranchDTO, error)
|
||||
GetByID(ctx context.Context, id int64) (BranchDTO, error)
|
||||
Create(ctx context.Context, dto CreateBranchDTO) (BranchDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdateBranchDTO) (BranchDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
151
internal/org/province/db/postgresql.go
Normal file
151
internal/org/province/db/postgresql.go
Normal file
@@ -0,0 +1,151 @@
|
||||
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 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
|
||||
}
|
||||
39
internal/org/province/dto.go
Normal file
39
internal/org/province/dto.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package province
|
||||
|
||||
type LocalizedName struct {
|
||||
En string `json:"en"`
|
||||
Ru string `json:"ru"`
|
||||
Tm string `json:"tm"`
|
||||
}
|
||||
|
||||
type ProvinceDTO struct {
|
||||
Id int64 `json:"id"`
|
||||
Name LocalizedName `json:"name"`
|
||||
Region LocalizedName `json:"region"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type CreateProvinceDTO struct {
|
||||
Name LocalizedName `json:"name" binding:"required"`
|
||||
Region LocalizedName `json:"region" binding:"required"`
|
||||
Active *bool `json:"active"`
|
||||
}
|
||||
|
||||
type UpdateProvinceDTO struct {
|
||||
Name LocalizedName `json:"name" binding:"required"`
|
||||
Region LocalizedName `json:"region" binding:"required"`
|
||||
Active bool `json:"active"`
|
||||
}
|
||||
|
||||
type ListFilter struct {
|
||||
Name string
|
||||
Region string
|
||||
Active *bool
|
||||
Page int
|
||||
Limit int
|
||||
}
|
||||
|
||||
type ListProvinceDTO struct {
|
||||
Count int64 `json:"count"`
|
||||
Provinces []ProvinceDTO `json:"provinces"`
|
||||
}
|
||||
256
internal/org/province/handler.go
Normal file
256
internal/org/province/handler.go
Normal file
@@ -0,0 +1,256 @@
|
||||
package province
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"online-back/internal/appresult"
|
||||
"online-back/internal/handlers"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/jackc/pgx/v4"
|
||||
)
|
||||
|
||||
const baseURL = "/provinces"
|
||||
|
||||
type handler struct {
|
||||
logger *logging.Logger
|
||||
repository Repository
|
||||
}
|
||||
|
||||
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
|
||||
return &handler{logger: logger, repository: repository}
|
||||
}
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
group := router.Group(baseURL, appresult.JwtTokenCheck())
|
||||
group.GET("", h.list)
|
||||
group.GET("/:id", h.getByID)
|
||||
group.POST("", h.create)
|
||||
group.PUT("/:id", h.update)
|
||||
group.DELETE("/:id", h.delete)
|
||||
}
|
||||
|
||||
// list godoc
|
||||
// @Summary List provinces
|
||||
// @Description Returns paginated provinces with optional filters
|
||||
// @Tags province
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param name query string false "Filter by province name (en, ru, tm)"
|
||||
// @Param region query string false "Filter by region (en, ru, tm)"
|
||||
// @Param active query bool false "Filter by active status"
|
||||
// @Param page query int false "Page number" default(1)
|
||||
// @Param limit query int false "Items per page" default(10)
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /provinces [get]
|
||||
func (h *handler) list(c *gin.Context) {
|
||||
filter, ok := parseListFilter(c)
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
result, err := h.repository.List(c.Request.Context(), filter)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list provinces failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// getByID godoc
|
||||
// @Summary Get province by ID
|
||||
// @Description Returns a single province
|
||||
// @Tags province
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Province ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /provinces/{id} [get]
|
||||
func (h *handler) getByID(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.GetByID(c.Request.Context(), id)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("get province failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// create godoc
|
||||
// @Summary Create province
|
||||
// @Description Creates a new province
|
||||
// @Tags province
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param body body CreateProvinceDTO true "Province payload"
|
||||
// @Success 201 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /provinces [post]
|
||||
func (h *handler) create(c *gin.Context) {
|
||||
var dto CreateProvinceDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Create(c.Request.Context(), dto)
|
||||
if err != nil {
|
||||
h.logger.Errorf("create province failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusCreated, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// update godoc
|
||||
// @Summary Update province
|
||||
// @Description Updates an existing province
|
||||
// @Tags province
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Province ID"
|
||||
// @Param body body UpdateProvinceDTO true "Province payload"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /provinces/{id} [put]
|
||||
func (h *handler) update(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
var dto UpdateProvinceDTO
|
||||
if err := c.ShouldBindJSON(&dto); err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
item, err := h.repository.Update(c.Request.Context(), id, dto)
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
h.logger.Errorf("update province failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(item))
|
||||
}
|
||||
|
||||
// delete godoc
|
||||
// @Summary Delete province
|
||||
// @Description Deletes a province
|
||||
// @Tags province
|
||||
// @Produce json
|
||||
// @Security BearerAuth
|
||||
// @Param id path int true "Province ID"
|
||||
// @Success 200 {object} appresult.AppSuccess
|
||||
// @Failure 400 {object} appresult.AppError
|
||||
// @Failure 404 {object} appresult.AppError
|
||||
// @Failure 500 {object} appresult.AppError
|
||||
// @Router /provinces/{id} [delete]
|
||||
func (h *handler) delete(c *gin.Context) {
|
||||
id, ok := parseID(c.Param("id"))
|
||||
if !ok {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.repository.Delete(c.Request.Context(), id); errors.Is(err, pgx.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, appresult.ErrNotFound)
|
||||
return
|
||||
} else if err != nil {
|
||||
h.logger.Errorf("delete province failed: %v", err)
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData("province removed"))
|
||||
}
|
||||
|
||||
func parseID(raw string) (int64, bool) {
|
||||
id, err := strconv.ParseInt(raw, 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return 0, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
const (
|
||||
defaultPage = 1
|
||||
defaultLimit = 10
|
||||
maxLimit = 100
|
||||
)
|
||||
|
||||
func parseListFilter(c *gin.Context) (ListFilter, bool) {
|
||||
filter := ListFilter{
|
||||
Page: defaultPage,
|
||||
Limit: defaultLimit,
|
||||
}
|
||||
|
||||
if raw := c.Query("page"); raw != "" {
|
||||
page, err := strconv.Atoi(raw)
|
||||
if err != nil || page < 1 {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Page = page
|
||||
}
|
||||
|
||||
if raw := c.Query("limit"); raw != "" {
|
||||
limit, err := strconv.Atoi(raw)
|
||||
if err != nil || limit < 1 || limit > maxLimit {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Limit = limit
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("name")); raw != "" {
|
||||
filter.Name = raw
|
||||
}
|
||||
|
||||
if raw := strings.TrimSpace(c.Query("region")); raw != "" {
|
||||
filter.Region = raw
|
||||
}
|
||||
|
||||
if raw := c.Query("active"); raw != "" {
|
||||
active, err := strconv.ParseBool(raw)
|
||||
if err != nil {
|
||||
return ListFilter{}, false
|
||||
}
|
||||
filter.Active = &active
|
||||
}
|
||||
|
||||
return filter, true
|
||||
}
|
||||
11
internal/org/province/storage.go
Normal file
11
internal/org/province/storage.go
Normal file
@@ -0,0 +1,11 @@
|
||||
package province
|
||||
|
||||
import "context"
|
||||
|
||||
type Repository interface {
|
||||
List(ctx context.Context, filter ListFilter) (ListProvinceDTO, error)
|
||||
GetByID(ctx context.Context, id int64) (ProvinceDTO, error)
|
||||
Create(ctx context.Context, dto CreateProvinceDTO) (ProvinceDTO, error)
|
||||
Update(ctx context.Context, id int64, dto UpdateProvinceDTO) (ProvinceDTO, error)
|
||||
Delete(ctx context.Context, id int64) error
|
||||
}
|
||||
30
internal/org/router.go
Normal file
30
internal/org/router.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// Package org groups the organizational/geographic hierarchy (provinces and
|
||||
// branches) under one bounded context, replacing the flat internal/admin/*
|
||||
// listing.
|
||||
package org
|
||||
|
||||
import (
|
||||
"online-back/internal/handlers"
|
||||
"online-back/internal/org/branches"
|
||||
branchesdb "online-back/internal/org/branches/db"
|
||||
"online-back/internal/org/province"
|
||||
provincedb "online-back/internal/org/province/db"
|
||||
"online-back/pkg/client/postgresql"
|
||||
"online-back/pkg/logging"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Register wires every org module's handler onto router.
|
||||
func Register(router *gin.RouterGroup, client postgresql.Client, logger *logging.Logger) {
|
||||
for _, h := range Handlers(client, logger) {
|
||||
h.Register(router)
|
||||
}
|
||||
}
|
||||
|
||||
func Handlers(client postgresql.Client, logger *logging.Logger) []handlers.Handler {
|
||||
return []handlers.Handler{
|
||||
province.NewHandler(logger, provincedb.NewRepository(client, logger)),
|
||||
branches.NewHandler(logger, branchesdb.NewRepository(client, logger)),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user