forked from free-lancers/electricity_bill_calc_service
feat(region):完成行政区划获取的功能。
This commit is contained in:
38
controller/region.go
Normal file
38
controller/region.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package controller
|
||||
|
||||
import (
|
||||
"electricity_bill_calc/response"
|
||||
"electricity_bill_calc/service"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type _RegionController struct {
|
||||
Router *gin.Engine
|
||||
}
|
||||
|
||||
var RegionController *_RegionController
|
||||
|
||||
func InitializeRegionController(router *gin.Engine) {
|
||||
RegionController = &_RegionController{
|
||||
Router: router,
|
||||
}
|
||||
|
||||
RegionController.Router.GET("/region/:rid", RegionController.FetchRegions)
|
||||
}
|
||||
|
||||
func (_RegionController) FetchRegions(c *gin.Context) {
|
||||
result := response.NewResult(c)
|
||||
requestParentId := c.Param("rid")
|
||||
regions, err := service.RegionService.FetchSubRegions(requestParentId)
|
||||
if err != nil {
|
||||
result.Error(http.StatusInternalServerError, err.Error())
|
||||
return
|
||||
}
|
||||
if len(regions) == 0 {
|
||||
result.Json(http.StatusNotFound, "未能获取到相关的行政区划。", gin.H{"regions": make([]string, 0)})
|
||||
return
|
||||
}
|
||||
result.Json(http.StatusOK, "已经获取到相关的行政区划。", gin.H{"regions": regions})
|
||||
}
|
Reference in New Issue
Block a user