From e5b5322e0d6e6752c304d3635373f72208206c86 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=B6=9B?= Date: Fri, 2 Jun 2023 06:18:34 +0800 Subject: [PATCH] =?UTF-8?q?fix(region):=E5=9F=BA=E6=9C=AC=E7=A1=AE?= =?UTF-8?q?=E5=AE=9A=E8=A1=8C=E6=94=BF=E5=8C=BA=E5=88=92=E9=83=A8=E5=88=86?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=EF=BC=8C=E6=98=8E=E7=A1=AE=E6=A8=A1=E5=9E=8B?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=E5=8A=9F=E8=83=BD=E7=9A=84=E4=BD=BF=E7=94=A8?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- controller/region.go | 40 ++++++++++++++++++++++++++++++++++++++++ model/region.go | 8 ++++++++ router/router.go | 1 + 3 files changed, 49 insertions(+) create mode 100644 controller/region.go create mode 100644 model/region.go diff --git a/controller/region.go b/controller/region.go new file mode 100644 index 0000000..9e4af8c --- /dev/null +++ b/controller/region.go @@ -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}) +} diff --git a/model/region.go b/model/region.go new file mode 100644 index 0000000..76315bd --- /dev/null +++ b/model/region.go @@ -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"` +} diff --git a/router/router.go b/router/router.go index 965eeb3..795d48d 100644 --- a/router/router.go +++ b/router/router.go @@ -45,6 +45,7 @@ func App() *fiber.App { app.Use(security.SessionRecovery) controller.InitializeUserHandlers(app) + controller.InitializeRegionHandlers(app) return app }