Commit f63ef8f9 authored by Artem Hrybeniuk's avatar Artem Hrybeniuk Committed by Matija Obreza
Browse files

Added removing crop species endpoint

parent cd362030
......@@ -101,6 +101,20 @@ public class CropController extends FilteredCRUDController<Crop, CropService, Cr
var species = taxonomySpeciesIds.stream().map(TaxonomySpecies::new).collect(Collectors.toList());
return cropService.addSpecies(cropService.get(cropId), species, null);
}
/**
* Removing taxonomy species for the crop.
*
* @param cropId the crop ID
* @param taxonomySpeciesIds the list of taxonomy species ids
* @return removed records
*/
@DeleteMapping(value = "/{id}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "removeSpecies", description = "Remove taxonomy species for the crop")
public List<TaxonomyCropMap> removeSpecies(@PathVariable("id") final Long cropId, @RequestBody final List<Long> taxonomySpeciesIds) {
var species = taxonomySpeciesIds.stream().map(TaxonomySpecies::new).collect(Collectors.toList());
return cropService.removeSpecies(cropService.get(cropId), species);
}
}
/**
......
......@@ -15,12 +15,18 @@
*/
package org.gringlobal.persistence;
import org.gringlobal.model.Crop;
import org.gringlobal.model.TaxonomyCropMap;
import org.gringlobal.model.TaxonomySpecies;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.querydsl.QuerydslPredicateExecutor;
import org.springframework.stereotype.Repository;
import java.util.Collection;
import java.util.List;
@Repository
public interface TaxonomyCropMapRepository extends JpaRepository<TaxonomyCropMap, Long>, QuerydslPredicateExecutor<TaxonomyCropMap> {
List<TaxonomyCropMap> findAllByCropAndTaxonomySpeciesIn(Crop crop, Collection<TaxonomySpecies> taxonomySpecies);
}
......@@ -67,6 +67,15 @@ public interface CropService extends FilteredCRUDService<Crop, CropFilter> {
*/
List<TaxonomyCropMap> addSpecies(@NotNull Crop crop, List<TaxonomySpecies> species, String note);
/**
* Removing taxonomy species for the crop.
*
* @param crop the crop
* @param species the list of taxonomy species
* @return removed records
*/
List<TaxonomyCropMap> removeSpecies(Crop crop, List<TaxonomySpecies> species);
class CropDetails {
@JsonUnwrapped
public Crop crop;
......
......@@ -178,6 +178,21 @@ public class CropServiceImpl extends FilteredCRUDServiceImpl<Crop, CropFilter, C
return savedCropMaps;
}
@Override
@Transactional
public List<TaxonomyCropMap> removeSpecies(Crop crop, List<TaxonomySpecies> species) {
if (CollectionUtils.isEmpty(species)) {
return Collections.emptyList();
}
final var loadedCrop = get(crop);
List<TaxonomyCropMap> mapsForRemove = taxonomyCropMapRepository.findAllByCropAndTaxonomySpeciesIn(crop, species);
taxonomyCropMapRepository.deleteAll(mapsForRemove);
LOG.debug("Removed {} species from crop {}", mapsForRemove.size(), loadedCrop.getName());
return mapsForRemove;
}
@Override
@Transactional
@PreAuthorize("hasAuthority('GROUP_ADMINS')")
......
......@@ -34,6 +34,7 @@ import java.util.Set;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.filerepository.model.RepositoryImage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.api.v1.impl.CropController;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropAttach;
......@@ -50,6 +51,7 @@ import org.gringlobal.test.service.AbstractServicesTest;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.security.test.context.support.WithUserDetails;
......@@ -503,6 +505,60 @@ public class CropControllerTest extends AbstractApiV1Test {
/*@formatter:on*/
}
@Test
public void removeSpeciesTest() throws Exception {
Crop crop = cropService.create(new Crop(CROP_NAME_1, CROP_NOTE_1));
assertThat(cropRepository.count(), is(1L));
var taxonomySpecies1 = addTaxonomySpeciesToDB("vulgare");
assertThat(taxonomySpecies1, notNullValue());
assertThat(taxonomySpecies1.getId(), notNullValue());
var taxonomySpecies2 = addTaxonomySpeciesToDB("andicola");
assertThat(taxonomySpecies2, notNullValue());
assertThat(taxonomySpecies2.getId(), notNullValue());
assertThat(taxonomySpeciesRepository.count(), is(2L));
assertThat(cropMapRepository.count(), is(0L));
cropService.addSpecies(crop, List.of(taxonomySpecies1, taxonomySpecies2), null);
assertThat(cropMapRepository.count(), is(2L));
TaxonomySpeciesFilter filter = new TaxonomySpeciesFilter();
filter.id = Set.of(taxonomySpecies1.getId(), taxonomySpecies2.getId());
var speciesPage = cropService.listSpecies(crop, filter, Pagination.toPageRequest(1, Sort.unsorted()));
assertThat(speciesPage.getTotalElements(), is(2L));
/*@formatter:off*/
mockMvc
.perform(delete(CropController.API_URL.concat("/{id}/species"), crop.getId())
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(Set.of(taxonomySpecies1.getId(), taxonomySpecies2.getId())))
)
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$", hasSize(2)))
// the first
.andExpect(jsonPath("$.[0].crop").isMap())
.andExpect(jsonPath("$.[0].crop.id", is(crop.getId().intValue())))
.andExpect(jsonPath("$.[0].taxonomySpecies").isMap())
.andExpect(jsonPath("$.[0].taxonomySpecies.id", is(oneOf(taxonomySpecies1.getId().intValue(), taxonomySpecies2.getId().intValue()))))
// the second
.andExpect(jsonPath("$.[1].crop").isNumber())
.andExpect(jsonPath("$.[1].crop", is(crop.getId().intValue())))
.andExpect(jsonPath("$.[1].taxonomySpecies").isMap())
.andExpect(jsonPath("$.[1].taxonomySpecies.id", is(oneOf(taxonomySpecies1.getId().intValue(), taxonomySpecies2.getId().intValue()))))
;
/*@formatter:on*/
assertThat(cropMapRepository.count(), is(0L));
speciesPage = cropService.listSpecies(crop, filter, Pagination.toPageRequest(1, Sort.unsorted()));
assertThat(speciesPage.getTotalElements(), is(0L));
}
@Test
public void getDetailsTest() throws Exception{
Crop crop = cropService.create(new Crop(CROP_NAME_1, CROP_NOTE_1));
......
Supports Markdown
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