feat: add fetch by id for API wilayah indonesia
This commit is contained in:
parent
e57ce46ce6
commit
32e789ce9f
|
@ -3,6 +3,7 @@ package domain
|
|||
type Province struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ListRegency []Regency `json:"list_regency,omitempty"`
|
||||
}
|
||||
|
||||
type Regency struct {
|
||||
|
@ -10,6 +11,7 @@ type Regency struct {
|
|||
ProvinceID string `json:"province_id"`
|
||||
Name string `json:"name"`
|
||||
Province *Province `json:"province,omitempty"`
|
||||
ListDistrict []District `json:"list_district,omitempty"`
|
||||
}
|
||||
|
||||
type District struct {
|
||||
|
@ -17,6 +19,7 @@ type District struct {
|
|||
RegencyID string `json:"regency_id"`
|
||||
Name string `json:"name"`
|
||||
Regency *Regency `json:"regency,omitempty"`
|
||||
ListVillage []Village `json:"list_village,omitempty"`
|
||||
}
|
||||
|
||||
type Village struct {
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
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"`
|
||||
}
|
|
@ -23,7 +23,6 @@ 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 {
|
||||
|
@ -41,7 +40,6 @@ 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 {
|
||||
|
@ -59,7 +57,6 @@ 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 {
|
||||
|
@ -77,101 +74,57 @@ func GetVillages(c *fiber.Ctx) error {
|
|||
))
|
||||
}
|
||||
|
||||
|
||||
func GetProvinceByID(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
province, regencies, err := services.GetProvinceByID(id)
|
||||
province, err := services.GetProvinceByID(id)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse(
|
||||
fiber.StatusNotFound,
|
||||
"Province not found",
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to retrieve province",
|
||||
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,
|
||||
},
|
||||
"Province by id retrieved successfully",
|
||||
province,
|
||||
))
|
||||
}
|
||||
|
||||
func GetRegencyByID(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
regency, districts, err := services.GetRegencyByID(id)
|
||||
regency, err := services.GetRegencyByID(id)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse(
|
||||
fiber.StatusNotFound,
|
||||
"Regency not found",
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to retrieve regency",
|
||||
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,
|
||||
},
|
||||
regency,
|
||||
))
|
||||
}
|
||||
|
||||
func GetDistrictByID(c *fiber.Ctx) error {
|
||||
id := c.Params("id")
|
||||
district, villages, err := services.GetDistrictByID(id)
|
||||
district, err := services.GetDistrictByID(id)
|
||||
if err != nil {
|
||||
return c.Status(fiber.StatusNotFound).JSON(utils.FormatResponse(
|
||||
fiber.StatusNotFound,
|
||||
"District not found",
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to retrieve district",
|
||||
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,
|
||||
},
|
||||
"District by id retrieved successfully",
|
||||
district,
|
||||
))
|
||||
}
|
||||
|
||||
|
@ -179,46 +132,16 @@ 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",
|
||||
return c.Status(fiber.StatusInternalServerError).JSON(utils.FormatResponse(
|
||||
fiber.StatusInternalServerError,
|
||||
"Failed to retrieve village",
|
||||
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,
|
||||
},
|
||||
"Village by id retrieved successfully",
|
||||
village,
|
||||
))
|
||||
}
|
||||
|
|
|
@ -25,7 +25,6 @@ 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) {
|
||||
records, err := utils.ReadCSV("public/document/regencies.csv")
|
||||
if err != nil {
|
||||
|
@ -45,7 +44,6 @@ 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) {
|
||||
records, err := utils.ReadCSV("public/document/districts.csv")
|
||||
if err != nil {
|
||||
|
@ -65,7 +63,6 @@ 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) {
|
||||
records, err := utils.ReadCSV("public/document/villages.csv")
|
||||
if err != nil {
|
||||
|
@ -85,8 +82,7 @@ func GetVillages() ([]domain.Village, error) {
|
|||
return villages, nil
|
||||
}
|
||||
|
||||
|
||||
func FindProvinceByID(id string) (domain.Province, error) {
|
||||
func GetProvinceByID(id string) (domain.Province, error) {
|
||||
provinces, err := GetProvinces()
|
||||
if err != nil {
|
||||
return domain.Province{}, err
|
||||
|
@ -94,13 +90,20 @@ func FindProvinceByID(id string) (domain.Province, error) {
|
|||
|
||||
for _, province := range provinces {
|
||||
if province.ID == id {
|
||||
|
||||
regencies, err := GetRegenciesByProvinceID(id)
|
||||
if err != nil {
|
||||
return domain.Province{}, err
|
||||
}
|
||||
|
||||
province.ListRegency = regencies
|
||||
return province, nil
|
||||
}
|
||||
}
|
||||
return domain.Province{}, errors.New("province not found")
|
||||
}
|
||||
|
||||
func FindRegencyByID(id string) (domain.Regency, error) {
|
||||
func GetRegencyByID(id string) (domain.Regency, error) {
|
||||
regencies, err := GetRegencies()
|
||||
if err != nil {
|
||||
return domain.Regency{}, err
|
||||
|
@ -108,13 +111,20 @@ func FindRegencyByID(id string) (domain.Regency, error) {
|
|||
|
||||
for _, regency := range regencies {
|
||||
if regency.ID == id {
|
||||
|
||||
districts, err := GetDistrictsByRegencyID(id)
|
||||
if err != nil {
|
||||
return domain.Regency{}, err
|
||||
}
|
||||
|
||||
regency.ListDistrict = districts
|
||||
return regency, nil
|
||||
}
|
||||
}
|
||||
return domain.Regency{}, errors.New("regency not found")
|
||||
}
|
||||
|
||||
func FindDistrictByID(id string) (domain.District, error) {
|
||||
func GetDistrictByID(id string) (domain.District, error) {
|
||||
districts, err := GetDistricts()
|
||||
if err != nil {
|
||||
return domain.District{}, err
|
||||
|
@ -122,13 +132,20 @@ func FindDistrictByID(id string) (domain.District, error) {
|
|||
|
||||
for _, district := range districts {
|
||||
if district.ID == id {
|
||||
|
||||
villages, err := GetVillagesByDistrictID(id)
|
||||
if err != nil {
|
||||
return domain.District{}, err
|
||||
}
|
||||
|
||||
district.ListVillage = villages
|
||||
return district, nil
|
||||
}
|
||||
}
|
||||
return domain.District{}, errors.New("district not found")
|
||||
}
|
||||
|
||||
func FindVillageByID(id string) (domain.Village, error) {
|
||||
func GetVillageByID(id string) (domain.Village, error) {
|
||||
villages, err := GetVillages()
|
||||
if err != nil {
|
||||
return domain.Village{}, err
|
||||
|
@ -141,3 +158,48 @@ func FindVillageByID(id string) (domain.Village, error) {
|
|||
}
|
||||
return domain.Village{}, errors.New("village not found")
|
||||
}
|
||||
|
||||
func GetRegenciesByProvinceID(provinceID string) ([]domain.Regency, error) {
|
||||
regencies, err := GetRegencies()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []domain.Regency
|
||||
for _, regency := range regencies {
|
||||
if regency.ProvinceID == provinceID {
|
||||
result = append(result, regency)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetDistrictsByRegencyID(regencyID string) ([]domain.District, error) {
|
||||
districts, err := GetDistricts()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []domain.District
|
||||
for _, district := range districts {
|
||||
if district.RegencyID == regencyID {
|
||||
result = append(result, district)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func GetVillagesByDistrictID(districtID string) ([]domain.Village, error) {
|
||||
villages, err := GetVillages()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var result []domain.Village
|
||||
for _, village := range villages {
|
||||
if village.DistrictID == districtID {
|
||||
result = append(result, village)
|
||||
}
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
|
|
@ -13,7 +13,6 @@ 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 {
|
||||
|
@ -22,7 +21,6 @@ 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 {
|
||||
|
@ -31,7 +29,6 @@ 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 {
|
||||
|
@ -40,71 +37,32 @@ func GetVillages() ([]domain.Village, error) {
|
|||
return villages, nil
|
||||
}
|
||||
|
||||
func GetProvinceByID(id string) (domain.Province, []domain.Regency, error) {
|
||||
province, err := repositories.FindProvinceByID(id)
|
||||
func GetProvinceByID(id string) (domain.Province, error) {
|
||||
province, err := repositories.GetProvinceByID(id)
|
||||
if err != nil {
|
||||
return domain.Province{}, nil, err
|
||||
return domain.Province{}, 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
|
||||
return province, nil
|
||||
}
|
||||
|
||||
func GetRegencyByID(id string) (domain.Regency, []domain.District, error) {
|
||||
regency, err := repositories.FindRegencyByID(id)
|
||||
func GetRegencyByID(id string) (domain.Regency, error) {
|
||||
regency, err := repositories.GetRegencyByID(id)
|
||||
if err != nil {
|
||||
return domain.Regency{}, nil, err
|
||||
return domain.Regency{}, 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
|
||||
return regency, nil
|
||||
}
|
||||
|
||||
func GetDistrictByID(id string) (domain.District, []domain.Village, error) {
|
||||
district, err := repositories.FindDistrictByID(id)
|
||||
func GetDistrictByID(id string) (domain.District, error) {
|
||||
district, err := repositories.GetDistrictByID(id)
|
||||
if err != nil {
|
||||
return domain.District{}, nil, err
|
||||
return domain.District{}, 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
|
||||
return district, nil
|
||||
}
|
||||
|
||||
func GetVillageByID(id string) (domain.Village, error) {
|
||||
village, err := repositories.FindVillageByID(id)
|
||||
village, err := repositories.GetVillageByID(id)
|
||||
if err != nil {
|
||||
return domain.Village{}, err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue