fix(region):基本确定行政区划部分功能,明确模型扫描功能的使用。

This commit is contained in:
徐涛 2023-06-02 06:18:34 +08:00
parent bca0fd777d
commit e5b5322e0d
3 changed files with 49 additions and 0 deletions

40
controller/region.go Normal file
View File

@ -0,0 +1,40 @@
package controller
import (
"electricity_bill_calc/repository"
"electricity_bill_calc/response"
"net/http"
"github.com/gofiber/fiber/v2"
)
func InitializeRegionHandlers(router *fiber.App) {
router.Get("/region/:rid", getSubRegions)
router.Get("/regions/:rid", getParentRegions)
}
func getSubRegions(c *fiber.Ctx) error {
result := response.NewResult(c)
requestParentId := c.Params("rid")
regions, err := repository.RegionRepository.FindSubRegions(requestParentId)
if err != nil {
return result.Error(http.StatusInternalServerError, err.Error())
}
if len(regions) == 0 {
return result.Json(http.StatusNotFound, "未能获取到相关的行政区划。", fiber.Map{"regions": make([]string, 0)})
}
return result.Json(http.StatusOK, "已经获取到相关的行政区划。", fiber.Map{"regions": regions})
}
func getParentRegions(c *fiber.Ctx) error {
result := response.NewResult(c)
requestRegionCode := c.Params("rid")
regions, err := repository.RegionRepository.FindParentRegions(requestRegionCode)
if err != nil {
return result.Error(http.StatusInternalServerError, err.Error())
}
if len(regions) == 0 {
return result.Json(http.StatusNotFound, "未能获取到相关的行政区划。", fiber.Map{"regions": make([]string, 0)})
}
return result.Json(http.StatusOK, "以及获取到相关的行政区划。", fiber.Map{"regions": regions})
}

8
model/region.go Normal file
View File

@ -0,0 +1,8 @@
package model
type Region struct {
Code string `json:"code"`
Name string `json:"name"`
Level int32 `json:"level"`
Parent string `json:"parent"`
}

View File

@ -45,6 +45,7 @@ func App() *fiber.App {
app.Use(security.SessionRecovery)
controller.InitializeUserHandlers(app)
controller.InitializeRegionHandlers(app)
return app
}