Make creation of default area nested in city initialization

This commit is contained in:
2025-08-19 00:08:01 +03:00
parent 17d77fcda7
commit 2257ec79f6

View File

@@ -121,7 +121,8 @@ public class SgsApplication implements CommandLineRunner {
@Autowired @Autowired
public SgsApplication(RoleService roleService, PermissionService permissionService, RoleRepo roleRepo, 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, CityRepo cityRepo, DistrictRepo districtRepo, DistrictService districtService, CountryRepo countryRepo,
CountryService countryService, OrganizationService organizationService, UserService userService, CountryService countryService, OrganizationService organizationService, UserService userService,
PasswordEncoder passwordEncoder, SectorService sectorService, SubSectorService subSectorService, PasswordEncoder passwordEncoder, SectorService sectorService, SubSectorService subSectorService,
@@ -789,7 +790,7 @@ public class SgsApplication implements CommandLineRunner {
paginate_datacenters_get.setDescription(PermissionDescription.PAGINATE_DATACENTERS_GET); paginate_datacenters_get.setDescription(PermissionDescription.PAGINATE_DATACENTERS_GET);
permissionService.save(paginate_datacenters_get); permissionService.save(paginate_datacenters_get);
} }
// Ensure SUPER_ADMIN has all permissions // Ensure SUPER_ADMIN has all permissions
Optional<Role> superAdminRole = roleRepo.findByTag("SUPER_ADMIN"); Optional<Role> superAdminRole = roleRepo.findByTag("SUPER_ADMIN");
if (superAdminRole.isPresent()) { if (superAdminRole.isPresent()) {
@@ -858,6 +859,7 @@ public class SgsApplication implements CommandLineRunner {
} }
if (cityService.findAll().isEmpty()) { if (cityService.findAll().isEmpty()) {
createCitiesFromJson(); createCitiesFromJson();
createDefaultArea();
} }
if (districtService.findAll().isEmpty()) { if (districtService.findAll().isEmpty()) {
createDistrictFromJson(); createDistrictFromJson();
@@ -865,33 +867,30 @@ public class SgsApplication implements CommandLineRunner {
if (neighborhoodService.findAll().isEmpty()) { if (neighborhoodService.findAll().isEmpty()) {
createNeighborhoodsFromJson(); createNeighborhoodsFromJson();
} }
if (!cityService.findAll().isEmpty()) {
createDefaultArea();
}
} }
void createDefaultArea() { void createDefaultArea() {
// Check if default area already exists // Check if default area already exists
List<Area> existingAreas = areaService.findAll(); List<Area> existingAreas = areaService.findAll();
boolean defaultAreaExists = existingAreas.stream() boolean defaultAreaExists = existingAreas.stream()
.anyMatch(area -> "Turkiye".equals(area.getTag()) && area.isDefaultArea()); .anyMatch(area -> "Turkiye".equals(area.getTag()) && area.isDefaultArea());
if (!defaultAreaExists) { if (!defaultAreaExists) {
Area defaultArea = new Area(); Area defaultArea = new Area();
defaultArea.setTag("Turkiye"); defaultArea.setTag("Turkiye");
defaultArea.setDefaultArea(true); defaultArea.setDefaultArea(true);
defaultArea.setDeleted(false); defaultArea.setDeleted(false);
// Get all cities to add to the default area // Get all cities to add to the default area
List<City> allCities = cityService.findAll(); List<City> allCities = cityService.findAll();
defaultArea.setCities(allCities); defaultArea.setCities(allCities);
// Get the Turkey country to add to the default area // Get the Turkey country to add to the default area
List<Country> countries = countryService.findAll(); List<Country> countries = countryService.findAll();
if (!countries.isEmpty()) { if (!countries.isEmpty()) {
defaultArea.setCountries(countries); defaultArea.setCountries(countries);
} }
areaService.save(defaultArea); areaService.save(defaultArea);
} }
} }