Spaces:
Configuration error
Configuration error
File size: 2,031 Bytes
7ce61af | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | 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)
}
|