From 2257ec79f6e8d1295fd958959255b226354c5c56 Mon Sep 17 00:00:00 2001 From: Khaled Elagamy Date: Tue, 19 Aug 2025 00:08:01 +0300 Subject: [PATCH] Make creation of default area nested in city initialization --- .../src/main/java/com/sgs/SgsApplication.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/sge-backend/src/main/java/com/sgs/SgsApplication.java b/sge-backend/src/main/java/com/sgs/SgsApplication.java index 0ce8d67..d150b6a 100644 --- a/sge-backend/src/main/java/com/sgs/SgsApplication.java +++ b/sge-backend/src/main/java/com/sgs/SgsApplication.java @@ -121,7 +121,8 @@ public class SgsApplication implements CommandLineRunner { @Autowired public SgsApplication(RoleService roleService, PermissionService permissionService, RoleRepo roleRepo, - AreaService areaService, NeighborhoodRepo neighborhoodRepo, NeighborhoodService neighborhoodService, CityService cityService, + AreaService areaService, NeighborhoodRepo neighborhoodRepo, NeighborhoodService neighborhoodService, + CityService cityService, CityRepo cityRepo, DistrictRepo districtRepo, DistrictService districtService, CountryRepo countryRepo, CountryService countryService, OrganizationService organizationService, UserService userService, PasswordEncoder passwordEncoder, SectorService sectorService, SubSectorService subSectorService, @@ -789,7 +790,7 @@ public class SgsApplication implements CommandLineRunner { paginate_datacenters_get.setDescription(PermissionDescription.PAGINATE_DATACENTERS_GET); permissionService.save(paginate_datacenters_get); } - + // Ensure SUPER_ADMIN has all permissions Optional superAdminRole = roleRepo.findByTag("SUPER_ADMIN"); if (superAdminRole.isPresent()) { @@ -858,6 +859,7 @@ public class SgsApplication implements CommandLineRunner { } if (cityService.findAll().isEmpty()) { createCitiesFromJson(); + createDefaultArea(); } if (districtService.findAll().isEmpty()) { createDistrictFromJson(); @@ -865,33 +867,30 @@ public class SgsApplication implements CommandLineRunner { if (neighborhoodService.findAll().isEmpty()) { createNeighborhoodsFromJson(); } - if (!cityService.findAll().isEmpty()) { - createDefaultArea(); - } } void createDefaultArea() { // Check if default area already exists List existingAreas = areaService.findAll(); boolean defaultAreaExists = existingAreas.stream() - .anyMatch(area -> "Turkiye".equals(area.getTag()) && area.isDefaultArea()); - + .anyMatch(area -> "Turkiye".equals(area.getTag()) && area.isDefaultArea()); + if (!defaultAreaExists) { Area defaultArea = new Area(); defaultArea.setTag("Turkiye"); defaultArea.setDefaultArea(true); defaultArea.setDeleted(false); - + // Get all cities to add to the default area List allCities = cityService.findAll(); defaultArea.setCities(allCities); - + // Get the Turkey country to add to the default area List countries = countryService.findAll(); if (!countries.isEmpty()) { defaultArea.setCountries(countries); } - + areaService.save(defaultArea); } }