feat(region):完成行政区划获取的功能。

This commit is contained in:
徐涛 2022-08-13 20:11:16 +08:00
parent 5214147e49
commit bc60fcf835
4 changed files with 3865 additions and 3805 deletions

38
controller/region.go Normal file
View 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})
}

File diff suppressed because it is too large Load Diff

View File

@ -16,6 +16,7 @@ func Router() *gin.Engine {
router.Use(security.SessionRecovery)
controller.InitializeUserController(router)
controller.InitializeRegionController(router)
return router
}

21
service/region.go Normal file
View File

@ -0,0 +1,21 @@
package service
import (
"electricity_bill_calc/global"
"electricity_bill_calc/model"
"xorm.io/builder"
)
type _RegionService struct{}
var RegionService _RegionService
func (_RegionService) FetchSubRegions(parent string) ([]model.Region, error) {
regions := make([]model.Region, 0)
err := global.DBConn.Where(builder.Eq{"parent": parent}).Asc("code").Find(&regions)
if err != nil {
return make([]model.Region, 0), err
}
return regions, err
}