Skip to content
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
merged 105 commits into from
Jul 6, 2023
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 Jun 30, 2023
24f6a49
Added methods to get Districts from City
ospodaryk Jun 30, 2023
971013b
Added necessary error messages
ospodaryk Jun 30, 2023
f0bf6cd
Added tests to all function
ospodaryk Jun 30, 2023
30a0cf8
Added endpoint to method
ospodaryk Jun 30, 2023
005356d
Fixed bug and Vulnerability
ospodaryk Jun 30, 2023
0747134
Fixed bug
ospodaryk Jun 30, 2023
a816303
Fixed bug
ospodaryk Jun 30, 2023
9ca8b3c
Fixed bug
ospodaryk Jun 30, 2023
e6c2120
return
ospodaryk Jun 30, 2023
dfd8d8a
fix
ospodaryk Jun 30, 2023
b801e86
Fixed bug and Vulnerability
ospodaryk Jun 30, 2023
4be23f5
Fixed Vulnerability
ospodaryk Jun 30, 2023
197f24a
Fixed bugs
ospodaryk Jun 30, 2023
cc5c053
Added javadoc to method
ospodaryk Jul 1, 2023
d6e9b47
Fixed tests to methods
ospodaryk Jul 1, 2023
2e56f52
Fixed controller
ospodaryk Jul 1, 2023
c460610
Added tests
ospodaryk Jul 1, 2023
be57bdc
Formatted
ospodaryk Jul 1, 2023
25d9c80
Removed code smell
ospodaryk Jul 1, 2023
9ad4b0e
Fixed bug
ospodaryk Jul 1, 2023
7ae9aef
Fixed code Smell
ospodaryk Jul 1, 2023
bfb53a1
Fixed code Smell and bugs
ospodaryk Jul 1, 2023
795d079
added ApiOperation and ApiResponses
ospodaryk Jul 3, 2023
695d6e6
Removed * from import
ospodaryk Jul 3, 2023
47b6f39
Added enum for location and Kyiv as constant
ospodaryk Jul 4, 2023
68e671b
Used constants instead of variables and magic numbers
ospodaryk Jul 4, 2023
a1e6d14
Removed unnecessary line
ospodaryk Jul 4, 2023
e6a6677
Added javadoc
ospodaryk Jul 4, 2023
55a3065
Provided the parametrized type
ospodaryk Jul 4, 2023
7f812dc
Fixed bug
ospodaryk Jul 4, 2023
3cee79b
Working some tests
ospodaryk Jul 4, 2023
7d76ec3
working 22/27 tests
ospodaryk Jul 4, 2023
b42522c
working 24/27 tests
ospodaryk Jul 4, 2023
1312f46
working 27/27 tests
ospodaryk Jul 4, 2023
7909c31
Added tests
ospodaryk Jul 4, 2023
f76ec49
delete unnecessary lines
ospodaryk Jul 4, 2023
d4d45d9
Adde new Error Message
ospodaryk Jul 4, 2023
9e706ee
fixed files
ospodaryk Jul 4, 2023
3ba3107
FIXED TESTS
ospodaryk Jul 4, 2023
ca738b6
Fixed tests
ospodaryk Jul 4, 2023
aec06e7
added test
ospodaryk Jul 4, 2023
f1b6c68
formatted
ospodaryk Jul 4, 2023
fbd4ec3
Merge branch 'dev' into bug-districts
ospodaryk Jul 4, 2023
f6220dc
fixed code smell
ospodaryk Jul 4, 2023
c60c703
Added tests to enum
ospodaryk Jul 4, 2023
ed67e0b
fixed code smell
ospodaryk Jul 4, 2023
9cd3339
fixed code smell
ospodaryk Jul 4, 2023
bf2e569
fixed code
ospodaryk Jul 4, 2023
7495c6d
fixed tests
ospodaryk Jul 4, 2023
113bbfa
fixed tests
ospodaryk Jul 4, 2023
afbda52
fixed tests
ospodaryk Jul 4, 2023
3d51041
formatted
ospodaryk Jul 4, 2023
41461b9
removed code smell
ospodaryk Jul 4, 2023
2746ec8
Covered all lines
ospodaryk Jul 4, 2023
d15d588
Fixed code
ospodaryk Jul 4, 2023
e605393
Fixed code
ospodaryk Jul 4, 2023
77fed80
Added test
ospodaryk Jul 4, 2023
9dfa1f6
Added test
ospodaryk Jul 4, 2023
1025b93
removed imports
ospodaryk Jul 5, 2023
83d5575
Merge branch 'dev' into bug-districts
ospodaryk Jul 5, 2023
ec55515
Fixed controller
ospodaryk Jul 5, 2023
e26ccbd
Fexed magic numbers and page size
ospodaryk Jul 5, 2023
19b6273
Fixed tests
ospodaryk Jul 5, 2023
3203f0d
Fixed errors
ospodaryk Jul 5, 2023
5363731
Fixed some bugs
ospodaryk Jul 5, 2023
ea80f5b
Deleted some code
ospodaryk Jul 5, 2023
2b9d183
Removed unnesesary lines
ospodaryk Jul 5, 2023
ec7d35f
FIXED MAGIC NUMBERS
ospodaryk Jul 5, 2023
342f796
FIXED MAGIC NUMBERS
ospodaryk Jul 5, 2023
0b14248
FIXED MAGIC NUMBERS
ospodaryk Jul 5, 2023
6be5620
Updated java doc
ospodaryk Jul 5, 2023
1cd4108
formatted
ospodaryk Jul 5, 2023
4b3d82b
removed import.*
ospodaryk Jul 5, 2023
19ad7db
deleted get(0) and added new error
ospodaryk Jul 5, 2023
92275ef
deleted get(0) and added new error
ospodaryk Jul 5, 2023
66f8700
Added DEFAULT_PAGE_VALUE
ospodaryk Jul 5, 2023
c7f5358
deleted unnesesary import
ospodaryk Jul 5, 2023
20c1b35
Fixed infinity loop
ospodaryk Jul 5, 2023
887fcfa
deleted unnesesary import
ospodaryk Jul 5, 2023
076cef1
added in SecurityConfig endpoint
ospodaryk Jul 5, 2023
6027645
removed import *
ospodaryk Jul 5, 2023
91c6c59
added forbiden
ospodaryk Jul 5, 2023
f774aac
Fixed some moments
ospodaryk Jul 5, 2023
62a66a9
Fixed errors
ospodaryk Jul 5, 2023
9a93e83
Fixed tests
ospodaryk Jul 5, 2023
c86e467
Formatted deleted import
ospodaryk Jul 5, 2023
3858eec
Deleted nameUa from LocationDivision
ospodaryk Jul 5, 2023
f1ec8a4
Deleted error 400
ospodaryk Jul 5, 2023
a9fb013
Renamed name to locationNameMap
ospodaryk Jul 5, 2023
f3394fa
Renamed name to locationNameMap
ospodaryk Jul 5, 2023
5648cb3
Renamed name to locationNameMap
ospodaryk Jul 5, 2023
b58eacf
Fixed all tests
ospodaryk Jul 5, 2023
ac8c98a
formatted
ospodaryk Jul 5, 2023
f940636
reformatted
ospodaryk Jul 5, 2023
5fb338a
reformatted
ospodaryk Jul 5, 2023
1933bec
renamed in tests
ospodaryk Jul 6, 2023
be965a2
reformatted
ospodaryk Jul 6, 2023
c1ad904
Fixed LocationApiService
ospodaryk Jul 6, 2023
518dc45
Fixed some bugs
ospodaryk Jul 6, 2023
2cc8bd4
Fixed some bugs
ospodaryk Jul 6, 2023
ab7a783
Fixed шьзщке *
ospodaryk Jul 6, 2023
55c38c9
Fixed bugs with page_size
ospodaryk Jul 6, 2023
38029eb
added new tests
ospodaryk Jul 6, 2023
d80c60e
rewrited in streams
ospodaryk Jul 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added enum for location and Kyiv as constant
ospodaryk committed Jul 4, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 47b6f39a873c33d1995ee947c8aa689791b83894
17 changes: 17 additions & 0 deletions dao/src/main/java/greencity/enums/LocationDivision.java
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;
}
}
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;

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);

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"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
}
}