package handler import ( "photowall/internal/service" "photowall/pkg/response" "strconv" "github.com/gin-gonic/gin" ) type RegionHandler struct { svc *service.RegionService } func NewRegionHandler(svc *service.RegionService) *RegionHandler { return &RegionHandler{svc: svc} } // List GET /api/regions?parent_id=0 func (h *RegionHandler) List(c *gin.Context) { parentID, _ := strconv.ParseUint(c.DefaultQuery("parent_id", "0"), 10, 64) list, err := h.svc.ListByParent(uint(parentID)) if err != nil { response.Internal(c, err.Error()) return } response.OK(c, list) } // Countries GET /api/regions/countries func (h *RegionHandler) Countries(c *gin.Context) { list, err := h.svc.ListCountries() if err != nil { response.Internal(c, err.Error()) return } response.OK(c, list) } // Path GET /api/regions/:id/path func (h *RegionHandler) Path(c *gin.Context) { id, _ := strconv.ParseUint(c.Param("id"), 10, 64) path, err := h.svc.GetPath(uint(id)) if err != nil { response.NotFound(c, "地区不存在") return } response.OK(c, path) } // Search GET /api/regions/search?keyword=xxx func (h *RegionHandler) Search(c *gin.Context) { keyword := c.Query("keyword") if keyword == "" { response.BadRequest(c, "请输入搜索关键词") return } list, err := h.svc.Search(keyword, 20) if err != nil { response.Internal(c, err.Error()) return } response.OK(c, list) }