From e57ce46ce6d28d153da12a53d981a0f8aa3f1ada Mon Sep 17 00:00:00 2001 From: pahmiudahgede Date: Sun, 15 Dec 2024 05:07:46 +0700 Subject: [PATCH] fix: get method wilayah indonesia --- domain/Indonesian_territory.go | 23 +-- dto/Indonesian_territory.go | 51 ++++++ internal/api/routes.go | 4 + internal/controllers/Indonesian_territory.go | 149 ++++++++++++++++++ internal/repositories/Indonesian_territory.go | 100 ++++++++---- internal/services/Indonesian_territory.go | 74 +++++++++ utils/read_csv.go | 21 +++ 7 files changed, 378 insertions(+), 44 deletions(-) create mode 100644 dto/Indonesian_territory.go create mode 100644 utils/read_csv.go diff --git a/domain/Indonesian_territory.go b/domain/Indonesian_territory.go index 075c6fc..86ff9f5 100644 --- a/domain/Indonesian_territory.go +++ b/domain/Indonesian_territory.go @@ -6,19 +6,22 @@ type Province struct { } type Regency struct { - ID string `json:"id"` - ProvinceID string `json:"province_id"` - Name string `json:"name"` + ID string `json:"id"` + ProvinceID string `json:"province_id"` + Name string `json:"name"` + Province *Province `json:"province,omitempty"` } type District struct { - ID string `json:"id"` - RegencyID string `json:"regency_id"` - Name string `json:"name"` + ID string `json:"id"` + RegencyID string `json:"regency_id"` + Name string `json:"name"` + Regency *Regency `json:"regency,omitempty"` } type Village struct { - ID string `json:"id"` - DistrictID string `json:"district_id"` - Name string `json:"name"` -} \ No newline at end of file + ID string `json:"id"` + DistrictID string `json:"district_id"` + Name string `json:"name"` + District *District `json:"district,omitempty"` +} diff --git a/dto/Indonesian_territory.go b/dto/Indonesian_territory.go new file mode 100644 index 0000000..44efe6b --- /dev/null +++ b/dto/Indonesian_territory.go @@ -0,0 +1,51 @@ +package dto + +type ProvinceDetailResponse struct { + ID string `json:"id"` + ProvinsiName string `json:"provinsi_name"` + ListRegency []RegencyItem `json:"list_regency"` +} + +type RegencyDetailResponse struct { + ID string `json:"id"` + RegencyName string `json:"regency_name"` + ProvinceID string `json:"province_id"` + ProvinceName string `json:"province_name"` + ListDistrict []DistrictItem `json:"list_districts"` +} + +type DistrictDetailResponse struct { + ID string `json:"id"` + DistrictName string `json:"district_name"` + ProvinceID string `json:"province_id"` + ProvinceName string `json:"province_name"` + RegencyID string `json:"regency_id"` + RegencyName string `json:"regency_name"` + ListVillages []VillageItem `json:"list_villages"` +} + +type VillageDetailResponse struct { + ID string `json:"id"` + VillageName string `json:"village_name"` + ProvinceID string `json:"province_id"` + RegencyID string `json:"regency_id"` + DistrictID string `json:"district_id"` + ProvinceName string `json:"province_name"` + RegencyName string `json:"regency_name"` + DistrictName string `json:"district_name"` +} + +type RegencyItem struct { + ID string `json:"id"` + RegencyName string `json:"regency_name"` +} + +type DistrictItem struct { + ID string `json:"id"` + DistrictName string `json:"district_name"` +} + +type VillageItem struct { + ID string `json:"id"` + VillageName string `json:"village_name"` +} diff --git a/internal/api/routes.go b/internal/api/routes.go index 2a6698e..9baf27c 100644 --- a/internal/api/routes.go +++ b/internal/api/routes.go @@ -90,4 +90,8 @@ func AppRouter(app *fiber.App) { api.Get("/wilayah-indonesia/regencies", controllers.GetRegencies) api.Get("/wilayah-indonesia/subdistricts", controllers.GetDistricts) api.Get("/wilayah-indonesia/villages", controllers.GetVillages) + api.Get("/wilayah-indonesia/provinces/:id", controllers.GetProvinceByID) + api.Get("/wilayah-indonesia/regencies/:id", controllers.GetRegencyByID) + api.Get("/wilayah-indonesia/subdistricts/:id", controllers.GetDistrictByID) + api.Get("/wilayah-indonesia/villages/:id", controllers.GetVillageByID) } diff --git a/internal/controllers/Indonesian_territory.go b/internal/controllers/Indonesian_territory.go index a01ded7..9cded96 100644 --- a/internal/controllers/Indonesian_territory.go +++ b/internal/controllers/Indonesian_territory.go @@ -23,6 +23,7 @@ func GetProvinces(c *fiber.Ctx) error { )) } +// GetRegencies handles the GET request for regencies func GetRegencies(c *fiber.Ctx) error { regencies, err := services.GetRegencies() if err != nil { @@ -40,6 +41,7 @@ func GetRegencies(c *fiber.Ctx) error { )) } +// GetDistricts handles the GET request for districts func GetDistricts(c *fiber.Ctx) error { districts, err := services.GetDistricts() if err != nil { @@ -57,6 +59,7 @@ func GetDistricts(c *fiber.Ctx) error { )) } +// GetVillages handles the GET request for villages func GetVillages(c *fiber.Ctx) error { villages, err := services.GetVillages() if err != nil { @@ -73,3 +76,149 @@ func GetVillages(c *fiber.Ctx) error { villages, )) } + + +func GetProvinceByID(c *fiber.Ctx) error { + id := c.Params("id") + province, regencies, err := services.GetProvinceByID(id) + if err != nil { + return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse( + fiber.StatusNotFound, + "Province not found", + nil, + )) + } + + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( + fiber.StatusOK, + "Provinces by id retrieved successfully", + fiber.Map{ + "id": province.ID, + "provinsi_name": province.Name, + "list_regency": regencies, + }, + )) +} + +func GetRegencyByID(c *fiber.Ctx) error { + id := c.Params("id") + regency, districts, err := services.GetRegencyByID(id) + if err != nil { + return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse( + fiber.StatusNotFound, + "Regency not found", + nil, + )) + } + + provinces, _ := services.GetProvinces() + var provinceName string + for _, province := range provinces { + if province.ID == regency.ProvinceID { + provinceName = province.Name + break + } + } + + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( + fiber.StatusOK, + "Regency by id retrieved successfully", + fiber.Map{ + "id": regency.ID, + "province_id": regency.ProvinceID, + "province_name": provinceName, + "regency_name": regency.Name, + "list_districts": districts, + }, + )) +} + +func GetDistrictByID(c *fiber.Ctx) error { + id := c.Params("id") + district, villages, err := services.GetDistrictByID(id) + if err != nil { + return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse( + fiber.StatusNotFound, + "District not found", + nil, + )) + } + + provinces, _ := services.GetProvinces() + regencies, _ := services.GetRegencies() + var provinceName, regencyName string + for _, province := range provinces { + if province.ID == district.RegencyID { + provinceName = province.Name + break + } + } + for _, regency := range regencies { + if regency.ID == district.RegencyID { + regencyName = regency.Name + break + } + } + + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( + fiber.StatusOK, + "district by id retrieved successfully", + fiber.Map{ + "id": district.ID, + "province_id": district.RegencyID, + "regency_id": district.RegencyID, + "province_name": provinceName, + "regency_name": regencyName, + "district_name": district.Name, + "list_villages": villages, + }, + )) +} + +func GetVillageByID(c *fiber.Ctx) error { + id := c.Params("id") + village, err := services.GetVillageByID(id) + if err != nil { + return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse( + fiber.StatusNotFound, + "Village not found", + nil, + )) + } + + provinces, _ := services.GetProvinces() + regencies, _ := services.GetRegencies() + districts, _ := services.GetDistricts() + + var provinceName, regencyName, districtName string + for _, province := range provinces { + if province.ID == village.ID { + provinceName = province.Name + } + } + for _, regency := range regencies { + if regency.ID == village.ID { + regencyName = regency.Name + } + } + for _, district := range districts { + if district.ID == village.ID { + districtName = district.Name + } + } + + return c.Status(fiber.StatusOK).JSON(utils.FormatResponse( + fiber.StatusOK, + "villages by id retrieved successfully", + fiber.Map{ + "id": village.ID, + "province_id": village.ID, + "regency_id": village.ID, + "district_id": village.ID, + "province_name": provinceName, + "regency_name": regencyName, + "district_name": districtName, + "village_name": village.Name, + }, + )) +} diff --git a/internal/repositories/Indonesian_territory.go b/internal/repositories/Indonesian_territory.go index 16471cc..ce7b73f 100644 --- a/internal/repositories/Indonesian_territory.go +++ b/internal/repositories/Indonesian_territory.go @@ -1,21 +1,14 @@ package repositories import ( - "encoding/csv" - "os" + "errors" "github.com/pahmiudahgede/senggoldong/domain" + "github.com/pahmiudahgede/senggoldong/utils" ) func GetProvinces() ([]domain.Province, error) { - file, err := os.Open("public/document/provinces.csv") - if err != nil { - return nil, err - } - defer file.Close() - - reader := csv.NewReader(file) - records, err := reader.ReadAll() + records, err := utils.ReadCSV("public/document/provinces.csv") if err != nil { return nil, err } @@ -32,15 +25,9 @@ func GetProvinces() ([]domain.Province, error) { return provinces, nil } +// GetRegencies reads the regencies data from CSV and returns a slice of Regency func GetRegencies() ([]domain.Regency, error) { - file, err := os.Open("public/document/regencies.csv") - if err != nil { - return nil, err - } - defer file.Close() - - reader := csv.NewReader(file) - records, err := reader.ReadAll() + records, err := utils.ReadCSV("public/document/regencies.csv") if err != nil { return nil, err } @@ -58,15 +45,9 @@ func GetRegencies() ([]domain.Regency, error) { return regencies, nil } +// GetDistricts reads the districts data from CSV and returns a slice of District func GetDistricts() ([]domain.District, error) { - file, err := os.Open("public/document/districts.csv") - if err != nil { - return nil, err - } - defer file.Close() - - reader := csv.NewReader(file) - records, err := reader.ReadAll() + records, err := utils.ReadCSV("public/document/districts.csv") if err != nil { return nil, err } @@ -84,15 +65,9 @@ func GetDistricts() ([]domain.District, error) { return districts, nil } +// GetVillages reads the villages data from CSV and returns a slice of Village func GetVillages() ([]domain.Village, error) { - file, err := os.Open("public/document/villages.csv") - if err != nil { - return nil, err - } - defer file.Close() - - reader := csv.NewReader(file) - records, err := reader.ReadAll() + records, err := utils.ReadCSV("public/document/villages.csv") if err != nil { return nil, err } @@ -109,3 +84,60 @@ func GetVillages() ([]domain.Village, error) { return villages, nil } + + +func FindProvinceByID(id string) (domain.Province, error) { + provinces, err := GetProvinces() + if err != nil { + return domain.Province{}, err + } + + for _, province := range provinces { + if province.ID == id { + return province, nil + } + } + return domain.Province{}, errors.New("province not found") +} + +func FindRegencyByID(id string) (domain.Regency, error) { + regencies, err := GetRegencies() + if err != nil { + return domain.Regency{}, err + } + + for _, regency := range regencies { + if regency.ID == id { + return regency, nil + } + } + return domain.Regency{}, errors.New("regency not found") +} + +func FindDistrictByID(id string) (domain.District, error) { + districts, err := GetDistricts() + if err != nil { + return domain.District{}, err + } + + for _, district := range districts { + if district.ID == id { + return district, nil + } + } + return domain.District{}, errors.New("district not found") +} + +func FindVillageByID(id string) (domain.Village, error) { + villages, err := GetVillages() + if err != nil { + return domain.Village{}, err + } + + for _, village := range villages { + if village.ID == id { + return village, nil + } + } + return domain.Village{}, errors.New("village not found") +} diff --git a/internal/services/Indonesian_territory.go b/internal/services/Indonesian_territory.go index 5dc10f5..239ae7d 100644 --- a/internal/services/Indonesian_territory.go +++ b/internal/services/Indonesian_territory.go @@ -13,6 +13,7 @@ func GetProvinces() ([]domain.Province, error) { return provinces, nil } +// GetRegencies retrieves a list of regencies func GetRegencies() ([]domain.Regency, error) { regencies, err := repositories.GetRegencies() if err != nil { @@ -21,6 +22,7 @@ func GetRegencies() ([]domain.Regency, error) { return regencies, nil } +// GetDistricts retrieves a list of districts func GetDistricts() ([]domain.District, error) { districts, err := repositories.GetDistricts() if err != nil { @@ -29,6 +31,7 @@ func GetDistricts() ([]domain.District, error) { return districts, nil } +// GetVillages retrieves a list of villages func GetVillages() ([]domain.Village, error) { villages, err := repositories.GetVillages() if err != nil { @@ -36,3 +39,74 @@ func GetVillages() ([]domain.Village, error) { } return villages, nil } + +func GetProvinceByID(id string) (domain.Province, []domain.Regency, error) { + province, err := repositories.FindProvinceByID(id) + if err != nil { + return domain.Province{}, nil, err + } + + regencies, err := repositories.GetRegencies() + if err != nil { + return domain.Province{}, nil, err + } + + var listRegency []domain.Regency + for _, regency := range regencies { + if regency.ProvinceID == province.ID { + listRegency = append(listRegency, regency) + } + } + + return province, listRegency, nil +} + +func GetRegencyByID(id string) (domain.Regency, []domain.District, error) { + regency, err := repositories.FindRegencyByID(id) + if err != nil { + return domain.Regency{}, nil, err + } + + districts, err := repositories.GetDistricts() + if err != nil { + return domain.Regency{}, nil, err + } + + var listDistrict []domain.District + for _, district := range districts { + if district.RegencyID == regency.ID { + listDistrict = append(listDistrict, district) + } + } + + return regency, listDistrict, nil +} + +func GetDistrictByID(id string) (domain.District, []domain.Village, error) { + district, err := repositories.FindDistrictByID(id) + if err != nil { + return domain.District{}, nil, err + } + + villages, err := repositories.GetVillages() + if err != nil { + return domain.District{}, nil, err + } + + var listVillage []domain.Village + for _, village := range villages { + if village.DistrictID == district.ID { + listVillage = append(listVillage, village) + } + } + + return district, listVillage, nil +} + +func GetVillageByID(id string) (domain.Village, error) { + village, err := repositories.FindVillageByID(id) + if err != nil { + return domain.Village{}, err + } + return village, nil +} diff --git a/utils/read_csv.go b/utils/read_csv.go new file mode 100644 index 0000000..aaf569c --- /dev/null +++ b/utils/read_csv.go @@ -0,0 +1,21 @@ +package utils + +import ( + "encoding/csv" + "os" +) + +func ReadCSV(filePath string) ([][]string, error) { + file, err := os.Open(filePath) + if err != nil { + return nil, err + } + defer file.Close() + + reader := csv.NewReader(file) + records, err := reader.ReadAll() + if err != nil { + return nil, err + } + return records, nil +}