generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[UBS Address] User can choose districts on own #1161
Merged
+1,097
−18
Merged
Changes from 1 commit
Commits
Show all changes
105 commits
Select commit
Hold shift + click to select a range
a5a14f6
Added dto to work with data
ospodaryk 24f6a49
Added methods to get Districts from City
ospodaryk 971013b
Added necessary error messages
ospodaryk f0bf6cd
Added tests to all function
ospodaryk 30a0cf8
Added endpoint to method
ospodaryk 005356d
Fixed bug and Vulnerability
ospodaryk 0747134
Fixed bug
ospodaryk a816303
Fixed bug
ospodaryk 9ca8b3c
Fixed bug
ospodaryk e6c2120
return
ospodaryk dfd8d8a
fix
ospodaryk b801e86
Fixed bug and Vulnerability
ospodaryk 4be23f5
Fixed Vulnerability
ospodaryk 197f24a
Fixed bugs
ospodaryk cc5c053
Added javadoc to method
ospodaryk d6e9b47
Fixed tests to methods
ospodaryk 2e56f52
Fixed controller
ospodaryk c460610
Added tests
ospodaryk be57bdc
Formatted
ospodaryk 25d9c80
Removed code smell
ospodaryk 9ad4b0e
Fixed bug
ospodaryk 7ae9aef
Fixed code Smell
ospodaryk bfb53a1
Fixed code Smell and bugs
ospodaryk 795d079
added ApiOperation and ApiResponses
ospodaryk 695d6e6
Removed * from import
ospodaryk 47b6f39
Added enum for location and Kyiv as constant
ospodaryk 68e671b
Used constants instead of variables and magic numbers
ospodaryk a1e6d14
Removed unnecessary line
ospodaryk e6a6677
Added javadoc
ospodaryk 55a3065
Provided the parametrized type
ospodaryk 7f812dc
Fixed bug
ospodaryk 3cee79b
Working some tests
ospodaryk 7d76ec3
working 22/27 tests
ospodaryk b42522c
working 24/27 tests
ospodaryk 1312f46
working 27/27 tests
ospodaryk 7909c31
Added tests
ospodaryk f76ec49
delete unnecessary lines
ospodaryk d4d45d9
Adde new Error Message
ospodaryk 9e706ee
fixed files
ospodaryk 3ba3107
FIXED TESTS
ospodaryk ca738b6
Fixed tests
ospodaryk aec06e7
added test
ospodaryk f1b6c68
formatted
ospodaryk fbd4ec3
Merge branch 'dev' into bug-districts
ospodaryk f6220dc
fixed code smell
ospodaryk c60c703
Added tests to enum
ospodaryk ed67e0b
fixed code smell
ospodaryk 9cd3339
fixed code smell
ospodaryk bf2e569
fixed code
ospodaryk 7495c6d
fixed tests
ospodaryk 113bbfa
fixed tests
ospodaryk afbda52
fixed tests
ospodaryk 3d51041
formatted
ospodaryk 41461b9
removed code smell
ospodaryk 2746ec8
Covered all lines
ospodaryk d15d588
Fixed code
ospodaryk e605393
Fixed code
ospodaryk 77fed80
Added test
ospodaryk 9dfa1f6
Added test
ospodaryk 1025b93
removed imports
ospodaryk 83d5575
Merge branch 'dev' into bug-districts
ospodaryk ec55515
Fixed controller
ospodaryk e26ccbd
Fexed magic numbers and page size
ospodaryk 19b6273
Fixed tests
ospodaryk 3203f0d
Fixed errors
ospodaryk 5363731
Fixed some bugs
ospodaryk ea80f5b
Deleted some code
ospodaryk 2b9d183
Removed unnesesary lines
ospodaryk ec7d35f
FIXED MAGIC NUMBERS
ospodaryk 342f796
FIXED MAGIC NUMBERS
ospodaryk 0b14248
FIXED MAGIC NUMBERS
ospodaryk 6be5620
Updated java doc
ospodaryk 1cd4108
formatted
ospodaryk 4b3d82b
removed import.*
ospodaryk 19ad7db
deleted get(0) and added new error
ospodaryk 92275ef
deleted get(0) and added new error
ospodaryk 66f8700
Added DEFAULT_PAGE_VALUE
ospodaryk c7f5358
deleted unnesesary import
ospodaryk 20c1b35
Fixed infinity loop
ospodaryk 887fcfa
deleted unnesesary import
ospodaryk 076cef1
added in SecurityConfig endpoint
ospodaryk 6027645
removed import *
ospodaryk 91c6c59
added forbiden
ospodaryk f774aac
Fixed some moments
ospodaryk 62a66a9
Fixed errors
ospodaryk 9a93e83
Fixed tests
ospodaryk c86e467
Formatted deleted import
ospodaryk 3858eec
Deleted nameUa from LocationDivision
ospodaryk f1ec8a4
Deleted error 400
ospodaryk a9fb013
Renamed name to locationNameMap
ospodaryk f3394fa
Renamed name to locationNameMap
ospodaryk 5648cb3
Renamed name to locationNameMap
ospodaryk b58eacf
Fixed all tests
ospodaryk ac8c98a
formatted
ospodaryk f940636
reformatted
ospodaryk 5fb338a
reformatted
ospodaryk 1933bec
renamed in tests
ospodaryk be965a2
reformatted
ospodaryk c1ad904
Fixed LocationApiService
ospodaryk 518dc45
Fixed some bugs
ospodaryk 2cc8bd4
Fixed some bugs
ospodaryk ab7a783
Fixed шьзщке *
ospodaryk 55c38c9
Fixed bugs with page_size
ospodaryk 38029eb
added new tests
ospodaryk d80c60e
rewrited in streams
ospodaryk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Added enum for location and Kyiv as constant
commit 47b6f39a873c33d1995ee947c8aa689791b83894
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package greencity.enums; | ||
|
||
|
||
enum LocationDivision { | ||
REGION(1, "Область або АРК"), | ||
DISTRICT_IN_REGION(2, "Район в області або в АРК"), | ||
LOCAL_COMMUNITY(3, "Територіальна громада"), | ||
CITY(4, "Місто або село"), | ||
DISTRICT_IN_CITY(5, "Район в місті"); | ||
int levelId; | ||
String nameUa; | ||
|
||
LocationDivision(int levelId, String nameUa) { | ||
this.levelId = levelId; | ||
this.nameUa = nameUa; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ public class LocationApiService { | |
private static final String CODE = "code"; | ||
private static final String PAGE_SIZE = "page_size"; | ||
private static final String UPPER_ID = "parent"; | ||
|
||
private static LocationDto Kyiv; | ||
private RestTemplate restTemplate; | ||
|
||
/** | ||
|
@@ -36,6 +36,14 @@ public class LocationApiService { | |
*/ | ||
@Autowired | ||
public LocationApiService(RestTemplate restTemplate) { | ||
Map<String, String> name = new HashMap<>(); | ||
name.put("name", "Київ"); | ||
name.put("name_en", "Kyiv"); | ||
Kyiv = LocationDto.builder() | ||
.id("UA80000000000093317") | ||
.parentId(null) | ||
.name(name) | ||
.build(); | ||
this.restTemplate = restTemplate; | ||
} | ||
|
||
|
@@ -53,9 +61,9 @@ public List<LocationDto> getCitiesByName(String regionName, String cityName) { | |
return allCities; | ||
} | ||
return allCities.stream() | ||
.filter(location -> location.getName().containsKey(cityName) | ||
|| location.getName().containsValue(cityName)) | ||
.collect(Collectors.toList()); | ||
.filter(location -> location.getName().containsKey(cityName) | ||
|| location.getName().containsValue(cityName)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
/** | ||
|
@@ -68,15 +76,15 @@ public List<LocationDto> getCitiesByName(String regionName, String cityName) { | |
* @return A LocationDto object containing the location's data. | ||
*/ | ||
private LocationDto findLocationByName(List<LocationDto> locations, String regionName, String locationName, | ||
String errorMessage) { | ||
String errorMessage) { | ||
if (locations.isEmpty()) { | ||
return getCityByNameFromRegionSide(regionName, locationName); | ||
} | ||
return locations.stream() | ||
.filter(location -> location.getName().containsKey(locationName) | ||
|| location.getName().containsValue(locationName)) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundException(errorMessage + locationName)); | ||
.filter(location -> location.getName().containsKey(locationName) | ||
|| location.getName().containsValue(locationName)) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundException(errorMessage + locationName)); | ||
} | ||
|
||
/** | ||
|
@@ -96,10 +104,10 @@ public LocationDto getRegionByName(String regionName) { | |
allRegions = getAllRegions(); | ||
} | ||
return allRegions.stream() | ||
.filter(region -> region.getName().containsKey(regionName) || region.getName() | ||
.containsValue(regionName)) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.REGION_NOT_FOUND + regionName)); | ||
.filter(region -> region.getName().containsKey(regionName) || region.getName() | ||
.containsValue(regionName)) | ||
.findFirst() | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.REGION_NOT_FOUND + regionName)); | ||
} | ||
|
||
/** | ||
|
@@ -133,11 +141,11 @@ public LocationDto getCityByNameFromRegionSide(String regionName, String cityNam | |
LocationDto region = getRegionByName(regionName); | ||
List<LocationDto> districts = getAllDistrictInTheRegionsById(region.getId()); | ||
List<LocationDto> localCommunities = districts.stream() | ||
.flatMap(district -> getAllLocalCommunitiesById(district.getId()).stream()) | ||
.collect(Collectors.toList()); | ||
.flatMap(district -> getAllLocalCommunitiesById(district.getId()).stream()) | ||
.collect(Collectors.toList()); | ||
List<LocationDto> cities = localCommunities.stream() | ||
.flatMap(community -> getAllCitiesById(community.getId()).stream()) | ||
.collect(Collectors.toList()); | ||
.flatMap(community -> getAllCitiesById(community.getId()).stream()) | ||
.collect(Collectors.toList()); | ||
return findLocationByName(cities, regionName, cityName, ErrorMessage.CITY_NOT_FOUND); | ||
} | ||
|
||
|
@@ -149,8 +157,8 @@ public LocationDto getCityByNameFromRegionSide(String regionName, String cityNam | |
* @return A LocationDto object containing the city's data. | ||
*/ | ||
public List<LocationDto> getAllDistrictsInCityByNames(String regionName, String cityName) { | ||
if (cityName.equals("Київ") || cityName.equals("Kyiv")) { | ||
return getAllDistrictsInCityByCityID("UA80000000000093317"); | ||
if (cityName.equals(Kyiv.getName().get("name")) || cityName.equals(Kyiv.getName().get("name_en"))) { | ||
return getAllDistrictsInCityByCityID(Kyiv.getId()); | ||
} | ||
List<LocationDto> allDistricts = null; | ||
List<LocationDto> cities = getCitiesByName(regionName, cityName); | ||
|
@@ -212,7 +220,7 @@ public List<LocationDto> getAllLocalCommunitiesById(String upperId) { | |
* | ||
* @param upperId The ID of the city. | ||
* @return A List of LocationDto objects, each representing a district in the | ||
* city. | ||
* city. | ||
*/ | ||
public List<LocationDto> getAllDistrictsInCityByCityID(String upperId) { | ||
return getLocationDataByUpperId(DEFAULT_PAGE_SIZE, 5, upperId); | ||
|
@@ -225,11 +233,11 @@ public List<LocationDto> getAllDistrictsInCityByCityID(String upperId) { | |
* @param level The hierarchical level of the location (e.g., city, region). | ||
* @param code The code representing the specific location to fetch. | ||
* @return A LocationDto object representing the location matching the provided | ||
* code. | ||
* code. | ||
*/ | ||
private LocationDto getLocationDataByCode(int pageSize, int level, String code) { | ||
UriComponentsBuilder builder = builderUrl(pageSize).queryParam("code", code) | ||
.queryParam(LEVEL, level); | ||
.queryParam(LEVEL, level); | ||
return getResultFromUrl(builder.build().encode().toUri()).get(0); | ||
} | ||
|
||
|
@@ -240,7 +248,7 @@ private LocationDto getLocationDataByCode(int pageSize, int level, String code) | |
* @param level The hierarchical level of the locations to fetch (e.g., city, | ||
* region). | ||
* @return A List of LocationDto objects representing all locations at the | ||
* provided level. | ||
* provided level. | ||
*/ | ||
private List<LocationDto> getLocationDataByLevel(int pageSize, int level) { | ||
UriComponentsBuilder builder = builderUrl(pageSize).queryParam(LEVEL, level); | ||
|
@@ -255,7 +263,7 @@ private List<LocationDto> getLocationDataByLevel(int pageSize, int level) { | |
* region). | ||
* @param name The name of the location to fetch. | ||
* @return A List of LocationDto objects representing the locations matching the | ||
* provided name. | ||
* provided name. | ||
* @throws NotFoundException if no location matching the provided name and level | ||
* can be found. | ||
*/ | ||
|
@@ -264,8 +272,8 @@ private List<LocationDto> getLocationDataByName(int pageSize, int level, String | |
throw new IllegalArgumentException("The name parameter cannot be null"); | ||
} | ||
UriComponentsBuilder builder = builderUrl(pageSize) | ||
.queryParam(NAME, name) | ||
.queryParam(LEVEL, level); | ||
.queryParam(NAME, name) | ||
.queryParam(LEVEL, level); | ||
return getResultFromUrl(builder.build().encode().toUri()); | ||
} | ||
|
||
|
@@ -282,12 +290,12 @@ public List<LocationDto> getResultFromUrl(URI url) { | |
try { | ||
ResponseEntity<Map> response = restTemplate.getForEntity(url, Map.class); | ||
return Optional.ofNullable(response) | ||
.map(ResponseEntity::getBody) | ||
.map(body -> (List<Map<String, Object>>) body.get("results")) | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_LOCATION_BY_URL + url)) | ||
.stream() | ||
.map(this::mapToLocationDto) | ||
.collect(Collectors.toList()); | ||
.map(ResponseEntity::getBody) | ||
.map(body -> (List<Map<String, Object>>) body.get("results")) | ||
.orElseThrow(() -> new NotFoundException(ErrorMessage.NOT_FOUND_LOCATION_BY_URL + url)) | ||
.stream() | ||
.map(this::mapToLocationDto) | ||
.collect(Collectors.toList()); | ||
} catch (RestClientException e) { | ||
throw new NotFoundException(ErrorMessage.NOT_FOUND_LOCATION_BY_URL + url); | ||
} | ||
|
@@ -304,10 +312,10 @@ private LocationDto mapToLocationDto(Map<String, Object> result) { | |
nameMap.put("name", getValueFromMap(result, "name")); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please use the constants you declared here https://github.com/ita-social-projects/GreenCityUBS/pull/1161/files#diff-dc3cf5a2a46824d3495f3c4da780b50ad4ddeebccfc29d59dd1c63d5d89a8152R21 |
||
nameMap.put("name_en", getValueFromMap(result, "name_en")); | ||
return LocationDto.builder() | ||
.id(getValueFromMap(result, "code")) | ||
.parentId(getValueFromMap(result, "parent_id")) | ||
.name(nameMap) | ||
.build(); | ||
.id(getValueFromMap(result, "code")) | ||
.parentId(getValueFromMap(result, "parent_id")) | ||
.name(nameMap) | ||
.build(); | ||
} | ||
|
||
/** | ||
|
@@ -328,14 +336,14 @@ private <T> T getValueFromMap(Map<String, Object> map, String key) { | |
* @param level The level of location to fetch. | ||
* @param upperId The upper ID of the location. | ||
* @return A List of LocationDto objects, each representing a location matching | ||
* the provided parameters. | ||
* the provided parameters. | ||
*/ | ||
private List<LocationDto> getLocationDataByUpperId(int pageSize, int level, String upperId) { | ||
if (upperId == null) { | ||
throw new IllegalArgumentException("The upperId parameter cannot be null"); | ||
} | ||
UriComponentsBuilder builder = builderUrl(pageSize).queryParam(LEVEL, level) | ||
.queryParam(UPPER_ID, upperId); | ||
.queryParam(UPPER_ID, upperId); | ||
return getResultFromUrl(builder.build().encode().toUri()); | ||
} | ||
|
||
|
@@ -349,7 +357,7 @@ private List<LocationDto> getLocationDataByUpperId(int pageSize, int level, Stri | |
*/ | ||
public UriComponentsBuilder builderUrl(int pageSize) { | ||
return UriComponentsBuilder.fromHttpUrl(API_URL) | ||
.queryParam(PAGE, "1") | ||
.queryParam(PAGE_SIZE, pageSize); | ||
.queryParam(PAGE, "1") | ||
.queryParam(PAGE_SIZE, pageSize); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any reason you set
allDistricts = null
?we can use
List<LocationDto> allDistricts = getAllDistrictsInCityByCityID(cityId);