Commit 9e44f4a8 authored by Aleksandr Ivanov's avatar Aleksandr Ivanov

feedback implemented

parent 3374daa7
...@@ -19,7 +19,9 @@ import org.springframework.http.ResponseEntity; ...@@ -19,7 +19,9 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.LinkedHashSet; import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static ee.ria.riha.conf.ApplicationProperties.API_V1_PREFIX; import static ee.ria.riha.conf.ApplicationProperties.API_V1_PREFIX;
import static java.util.stream.Collectors.toList; import static java.util.stream.Collectors.toList;
...@@ -58,36 +60,46 @@ public class InfoSystemController { ...@@ -58,36 +60,46 @@ public class InfoSystemController {
} }
@GetMapping("/autocomplete") @GetMapping("/autocomplete")
@ApiOperation("List all existing information systems for autocomplete") @ApiOperation("List all existing information systems for autocomplete")
@ApiPageableAndFilterableParams public ResponseEntity autocomplete(@RequestParam("searchTerm") String searchTerm) {
public ResponseEntity autocomplete(Pageable pageable, Filterable filterable) {
PageRequest pageable = new PageRequest(0, 10);
// search for exact matches FilterRequest shortNameExact = new FilterRequest("short_name,ilike," + searchTerm, "desc", "id");
PagedResponse<InfoSystem> exactMatches = infoSystemService.list(pageable, createExactMatchFilterFromILikeFilter(filterable)); FilterRequest nameExact = new FilterRequest("name,ilike," + searchTerm, "desc", "id");
if (exactMatches != null && exactMatches.getTotalElements() >= pageable.getPageSize()) { FilterRequest nameFuzzy = new FilterRequest("name,ilike,%" + searchTerm + "%", "desc", "id");
// there are more exact matches than requested.
return ResponseEntity.ok(createPagedModel(exactMatches, infoSystemModelMapper));
} else if (exactMatches != null && exactMatches.getTotalElements() < pageable.getPageSize()) {
// there are some exact matches, need to fetch fuzzy matches
LinkedHashSet<InfoSystem> joinedList = new LinkedHashSet<>(exactMatches.getContent());
PagedResponse<InfoSystem> fuzzyMatches = infoSystemService.list(pageable, filterable);
if (fuzzyMatches != null && fuzzyMatches.getContent() != null) {
joinedList.addAll(fuzzyMatches.getContent());
}
return ResponseEntity.ok(createPagedModel( List<InfoSystem> foundResults = new ArrayList<>();
new PagedResponse<>( for (FilterRequest filterRequest : Arrays.asList(shortNameExact, nameExact, nameFuzzy)) {
pageable,
joinedList.size(), searchInfoSystemsByFilter(pageable, filterRequest)
new ArrayList<>(joinedList)), .forEach(infoSystem -> {
infoSystemModelMapper)); if (!foundResults.contains(infoSystem)) {
} else { foundResults.add(infoSystem);
return ResponseEntity.ok( }
createPagedModel( });
infoSystemService.list(pageable, filterable),
infoSystemModelMapper)); if (foundResults.size() >= pageable.getPageSize()) {
return getResponseEntity(pageable, foundResults.subList(0, pageable.getPageSize()));
}
} }
return getResponseEntity(pageable, foundResults);
}
private ResponseEntity getResponseEntity(PageRequest pageable, List<InfoSystem> response) {
return ResponseEntity.ok(
new PagedResponse(
pageable,
response.size(),
response.stream()
.map(infoSystemModelMapper::map)
.collect(toList())));
}
private List<InfoSystem> searchInfoSystemsByFilter(PageRequest pageable, FilterRequest filterRequest) {
PagedResponse<InfoSystem> exactMatchesShortNames = infoSystemService.list(pageable, filterRequest);
return exactMatchesShortNames.getContent() == null
? Collections.emptyList()
: exactMatchesShortNames.getContent();
} }
private Filterable createExactMatchFilterFromILikeFilter(Filterable filterable) { private Filterable createExactMatchFilterFromILikeFilter(Filterable filterable) {
......
...@@ -189,14 +189,7 @@ export class SystemsService { ...@@ -189,14 +189,7 @@ export class SystemsService {
public getSystemsForAutocomplete(text, ownShortName?, url?): Observable<any> { public getSystemsForAutocomplete(text, ownShortName?, url?): Observable<any> {
let params: HttpParams = new HttpParams(); let params: HttpParams = new HttpParams();
const filtersArr: string[] = []; params = params.set('searchTerm', text);
filtersArr.push(`name,ilike,%${ text }%`);
params = params.set('filter', filtersArr.join());
params = params.set('size', '10');
const urlToUse = url || (this.systemsUrl + "/autocomplete"); const urlToUse = url || (this.systemsUrl + "/autocomplete");
return this.http.get<any>(urlToUse, { return this.http.get<any>(urlToUse, {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment