Commit 2fc40edc authored by Maxym Borodenko's avatar Maxym Borodenko

Autocomplete crops

parent acc3be4b
......@@ -16,6 +16,7 @@
package org.gringlobal.api.v1.impl;
import java.io.IOException;
import java.util.List;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
......@@ -148,4 +149,14 @@ public class CropController extends FilteredCRUDController<Crop, CropService, Cr
FilteredPage<TaxonomySpecies, TaxonomySpeciesFilter> results = new FilteredPage<>(normalizedFilter, crudService.listSpecies(crudService.get(id), filter, pageable));
return results;
}
/**
* Auto-complete crops.
*
* @param term the search term
*/
@GetMapping(value = "/autocomplete", produces = MediaType.APPLICATION_JSON_VALUE)
public List<Crop> autocompleteCrops(@RequestParam("term") final String term) {
return crudService.autocompleteCrops(term);
}
}
......@@ -74,4 +74,12 @@ public interface CropService extends FilteredCRUDService<Crop, CropFilter> {
*/
Page<TaxonomySpecies> listSpecies(Crop crop, TaxonomySpeciesFilter filter, Pageable pageable);
/**
* Auto-complete crops.
*
* @param term the search term
* @return matching crops list
*/
List<Crop> autocompleteCrops(String term);
}
......@@ -18,7 +18,11 @@ package org.gringlobal.service.impl;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.apache.commons.lang3.StringUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.InvalidApiUsageException;
......@@ -26,6 +30,7 @@ import org.gringlobal.api.NotFoundElement;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
import org.gringlobal.model.QCrop;
import org.gringlobal.model.QCropAttach;
import org.gringlobal.model.QTaxonomyCropMap;
import org.gringlobal.model.QTaxonomySpecies;
......@@ -39,10 +44,11 @@ import org.gringlobal.service.CropAttachmentService.CropAttachmentRequest;
import org.gringlobal.service.CropService;
import org.gringlobal.service.filter.CropFilter;
import org.gringlobal.service.filter.TaxonomySpeciesFilter;
import org.hibernate.Hibernate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
......@@ -205,4 +211,13 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
return taxonomySpeciesRepository.findAll(query, pageable);
}
@Override
public List<Crop> autocompleteCrops(String term) {
if (StringUtils.isBlank(term) || term.length() < 1) {
return Collections.emptyList();
}
BooleanExpression expression = QCrop.crop.name.startsWithIgnoreCase(term);
return repository.findAll(expression, PageRequest.of(0, 15, Sort.by("name"))).getContent();
}
}
......@@ -242,6 +242,36 @@ public class CropControllerTest extends AbstractApiV1Test {
/*@formatter:on*/
}
@Test
public void autocompleteTest() throws Exception {
cropService.create(new Crop(CROP_NAME_1, CROP_NOTE_1));
cropService.create(new Crop(CROP_NAME_2, CROP_NOTE_2));
assertThat(cropRepository.count(), equalTo(2L));
String term = CROP_NAME_1.substring(0, 3);
/*@formatter:off*/
mockMvc
.perform(get(CropController.API_URL.concat("/autocomplete"))
.param("term", term)
.contentType(MediaType.APPLICATION_JSON)
)
.andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", hasSize(1)))
.andExpect(jsonPath("$.[0].name", is(CROP_NAME_1)))
.andExpect(jsonPath("$.[0].createdBy", is(USER_ADMINISTRATOR_ID)))
.andExpect(jsonPath("$.[0].modifiedBy", is(USER_ADMINISTRATOR_ID)))
.andExpect(jsonPath("$.[0].ownedBy").isMap())
.andExpect(jsonPath("$.[0].name", is(CROP_NAME_1)))
.andExpect(jsonPath("$.[0].note", is(CROP_NOTE_1)))
.andExpect(jsonPath("$.[0]._class", is(Crop.class.getSimpleName())))
;
/*@formatter:on*/
}
@Test
public void uploadFileTest() throws Exception {
Crop crop = cropService.create(new Crop(CROP_NAME_1, CROP_NOTE_1));
......
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