Add region and etrap APIs for branch create form.

Expose distinct regions and etraps-by-region under provinces and branches, and seed real etraps per welayat.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
2026-08-11 12:12:07 +05:00
parent 51283ffa14
commit 1987d4f596
10 changed files with 822 additions and 15 deletions

View File

@@ -8,6 +8,7 @@ import (
"online-back/internal/appresult"
"online-back/internal/handlers"
"online-back/internal/org/province"
"online-back/pkg/logging"
"github.com/gin-gonic/gin"
@@ -19,15 +20,18 @@ const baseURL = "/branches"
type handler struct {
logger *logging.Logger
repository Repository
provinces province.Repository
}
func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler {
return &handler{logger: logger, repository: repository}
func NewHandler(logger *logging.Logger, repository Repository, provinces province.Repository) handlers.Handler {
return &handler{logger: logger, repository: repository, provinces: provinces}
}
func (h *handler) Register(router *gin.RouterGroup) {
group := router.Group(baseURL, appresult.JwtTokenCheck())
group.GET("", h.list)
group.GET("/regions", h.listRegions)
group.GET("/etraps", h.listEtraps)
group.GET("/:id", h.getByID)
group.POST("", h.create)
group.PUT("/:id", h.update)
@@ -67,6 +71,69 @@ func (h *handler) list(c *gin.Context) {
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
// listRegions godoc
// @Summary List regions for branch create
// @Description Returns distinct regions from provinces (use before selecting etrap/provinceId)
// @Tags branches
// @Produce json
// @Security BearerAuth
// @Param active query bool false "Only active provinces" default(true)
// @Success 200 {object} appresult.AppSuccess
// @Failure 400 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /branches/regions [get]
func (h *handler) listRegions(c *gin.Context) {
activeOnly, ok := parseActiveOnly(c, true)
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
result, err := h.provinces.ListRegions(c.Request.Context(), activeOnly)
if err != nil {
h.logger.Errorf("list branch regions failed: %v", err)
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
return
}
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
}
// listEtraps godoc
// @Summary List etraps by region for branch create
// @Description Returns provinces (etraps) for the selected region; pick id as provinceId
// @Tags branches
// @Produce json
// @Security BearerAuth
// @Param region query string true "Region name (en, ru or tm)"
// @Param active query bool false "Only active etraps" default(true)
// @Success 200 {object} appresult.AppSuccess
// @Failure 400 {object} appresult.AppError
// @Failure 500 {object} appresult.AppError
// @Router /branches/etraps [get]
func (h *handler) listEtraps(c *gin.Context) {
region := strings.TrimSpace(c.Query("region"))
if region == "" {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
activeOnly, ok := parseActiveOnly(c, true)
if !ok {
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
return
}
result, err := h.provinces.ListEtrapsByRegion(c.Request.Context(), region, activeOnly)
if err != nil {
h.logger.Errorf("list branch etraps 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
@@ -222,6 +289,18 @@ func parseID(raw string) (int64, bool) {
return id, true
}
func parseActiveOnly(c *gin.Context, defaultValue bool) (bool, bool) {
raw := c.Query("active")
if raw == "" {
return defaultValue, true
}
active, err := strconv.ParseBool(raw)
if err != nil {
return false, false
}
return active, true
}
const (
defaultPage = 1
defaultLimit = 10