Spaces:
Configuration error
Configuration error
| package cv_controller | |
| import ( | |
| "net/http" | |
| "strconv" | |
| "api.qobiltu.id/models" | |
| "api.qobiltu.id/response" | |
| "api.qobiltu.id/services" | |
| "github.com/gin-gonic/gin" | |
| ) | |
| type RegionController interface { | |
| SeedProvince(ctx *gin.Context) | |
| SeedCity(ctx *gin.Context) | |
| ListProvinces(ctx *gin.Context) | |
| ListCities(ctx *gin.Context) | |
| } | |
| type regionController struct { | |
| regionService services.RegionService | |
| } | |
| func NewRegionController(regionService services.RegionService) RegionController { | |
| return ®ionController{ | |
| regionService: regionService, | |
| } | |
| } | |
| func (c *regionController) SeedProvince(ctx *gin.Context) { | |
| err := c.regionService.SeedProvince(ctx) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Province seeded", nil, nil) | |
| } | |
| func (c *regionController) SeedCity(ctx *gin.Context) { | |
| err := c.regionService.SeedCity(ctx) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "City seeded", nil, nil) | |
| } | |
| func (c *regionController) ListProvinces(ctx *gin.Context) { | |
| provinces, err := c.regionService.ListProvinces(ctx) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Provinces retrieved", provinces, nil) | |
| } | |
| func (c *regionController) ListCities(ctx *gin.Context) { | |
| provinceID := ctx.Query("province_id") | |
| if provinceID == "" { | |
| cities, err := c.regionService.ListCities(ctx) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Cities retrieved", cities, nil) | |
| return | |
| } | |
| provinceIDInt, err := strconv.ParseInt(provinceID, 10, 64) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| req := models.ListCitiesByProvinceIdRequest{ | |
| ProvinceID: provinceIDInt, | |
| } | |
| cities, err := c.regionService.ListCitiesByProvinceId(ctx, &req) | |
| if err != nil { | |
| response.HandleError(ctx, err) | |
| return | |
| } | |
| response.HandleSuccess(ctx, http.StatusOK, "Cities by province retrieved", cities, nil) | |
| } | |