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:
@@ -28,6 +28,8 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
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)
|
||||
@@ -66,6 +68,69 @@ func (h *handler) list(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, appresult.SuccessWithData(result))
|
||||
}
|
||||
|
||||
// listRegions godoc
|
||||
// @Summary List distinct regions
|
||||
// @Description Returns unique regions from the provinces table (for region dropdown)
|
||||
// @Tags province
|
||||
// @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 /provinces/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.repository.ListRegions(c.Request.Context(), activeOnly)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list 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
|
||||
// @Description Returns provinces (etraps) belonging to the selected region
|
||||
// @Tags province
|
||||
// @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 /provinces/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.repository.ListEtrapsByRegion(c.Request.Context(), region, activeOnly)
|
||||
if err != nil {
|
||||
h.logger.Errorf("list etraps by region 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
|
||||
@@ -208,6 +273,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
|
||||
|
||||
Reference in New Issue
Block a user