Commit 708b9dff authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza
Browse files

API: update OrderRequestItems

parent ea650159
......@@ -24,6 +24,7 @@ import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.api.v1.ActionController;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
......@@ -41,6 +42,7 @@ import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.OrderRequestActionService;
import org.gringlobal.service.OrderRequestItemActionService;
import org.gringlobal.service.OrderRequestItemActionService.OrderRequestItemActionRequest;
import org.gringlobal.service.OrderRequestItemService;
import org.gringlobal.service.OrderRequestService;
import org.gringlobal.service.OrderRequestService.OrderRequestedInventory;
import org.gringlobal.service.filter.OrderRequestActionFilter;
......@@ -54,6 +56,7 @@ import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
......@@ -79,10 +82,6 @@ public class OrderRequestController extends FilteredCRUDController<OrderRequest,
return new OrderSpecifier[] { QOrderRequest.orderRequest.modifiedDate.desc() };
}
private OrderSpecifier<?>[] defaultItemSort() {
return new OrderSpecifier[] { QOrderRequestItem.orderRequestItem.sequenceNumber.asc() };
}
@Override
protected Class<OrderRequestFilter> filterType() {
return OrderRequestFilter.class;
......@@ -141,6 +140,89 @@ public class OrderRequestController extends FilteredCRUDController<OrderRequest,
}
}
@RestController("orderRequestItemsApi1")
@RequestMapping(OrderRequestItemsController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "OrderRequestItem")
@Validated
public static class OrderRequestItemsController extends CRUDController<OrderRequestItem, OrderRequestItemService> {
/** The Constant API_URL. */
public static final String API_URL = OrderRequestController.API_URL + "/items";
@Autowired
private OrderRequestService orderRequestService;
private OrderSpecifier<?>[] defaultItemSort() {
return new OrderSpecifier[] { QOrderRequestItem.orderRequestItem.sequenceNumber.asc() };
}
@PostMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Fetch items of an request", summary = "List or filter order items")
public FilteredPage<OrderRequestItem, OrderRequestItemFilter> filterItems(@PathVariable("id") final Long orderId, @Parameter(hidden = true) Pagination page, @RequestBody(required = true) final OrderRequestItemFilter filter) throws SearchException, IOException {
final OrderRequest orderRequest = orderRequestService.get(orderId);
OrderRequestItemFilter normalizedFilter = shortFilterService.normalizeFilter(filter, OrderRequestItemFilter.class);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(100, defaultItemSort()) : page.toPageRequest(100);
return new FilteredPage<>(normalizedFilter, orderRequestService.filterItems(orderRequest, filter, pageable));
}
/**
* Adding new inventories the order request.
*
* @param orderId the order request ID
* @return created items
*/
@PostMapping(value = "/{id}/add-inventory", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Create and add items to order request", summary = "Add items")
public List<OrderRequestItem> addInventories(@PathVariable("id") final Long orderId, @RequestBody final List<OrderRequestedInventory> inventories) {
final OrderRequest orderRequest = orderRequestService.get(orderId);
return orderRequestService.addInventories(orderRequest, inventories);
}
/**
* Update item status.
*
* @param orderId the order id
* @param newStatus the new status
* @param itemIds the item ids
* @return the list
*/
@PostMapping(value = "/{id}/status/{newStatus}")
@Operation(operationId = "updateItemStatus", summary = "Change item status", description = "Update status of selected request items")
public List<OrderRequestItem> updateItemStatus(@PathVariable("id") final Long orderId, @PathVariable("newStatus") @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS) String newStatus,
@RequestBody Set<Long> itemIds) {
final OrderRequest orderRequest = orderRequestService.get(orderId);
return orderRequestService.updateItemStatus(orderRequest, newStatus, itemIds);
}
/**
* Removing items from the order request.
*
* @param orderId the order request ID
* @param itemIds item Ids to be deleted from the order
* @return updated order request
*/
@PostMapping(value = "/{id}/remove", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Remove items from order request", summary = "Remove items")
public OrderRequest removeOrderItems(@PathVariable("id") final Long orderId, @RequestBody final Set<Long> itemIds) {
final OrderRequest orderRequest = orderRequestService.get(orderId);
return orderRequestService.removeOrderItems(orderRequest, itemIds);
}
/**
* Updating items
*
* @param items the list of items to update
* @return updated items
*/
@PutMapping(value = "/update-list")
@Operation(operationId = "updateItems", summary = "Update items", description = "Update a list of request items")
public List<OrderRequestItem> updateItems(@RequestBody final Set<OrderRequestItem> items) {
return crudService.updateAll(items);
}
}
@Override
public OrderRequest create(@RequestBody OrderRequest entity) {
return super.create(entity);
......@@ -166,58 +248,4 @@ public class OrderRequestController extends FilteredCRUDController<OrderRequest,
return super.filter(filterCode, page, filter);
}
@PostMapping(value = "/items/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Fetch items of an request", summary = "List or filter order items")
public FilteredPage<OrderRequestItem, OrderRequestItemFilter> filterItems(@PathVariable("id") final Long orderId, @Parameter(hidden = true) Pagination page, @RequestBody(required = true) final OrderRequestItemFilter filter) throws SearchException, IOException {
final OrderRequest orderRequest = crudService.get(orderId);
OrderRequestItemFilter normalizedFilter = shortFilterService.normalizeFilter(filter, OrderRequestItemFilter.class);
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(100, defaultItemSort()) : page.toPageRequest(100);
var results = new FilteredPage<>(normalizedFilter, crudService.filterItems(orderRequest, filter, pageable));
return results;
}
/**
* Adding new inventories the order request.
*
* @param orderId the order request ID
* @return created items
*/
@PostMapping(value = "/items/{id}/add-inventory", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Create and add items to order request", summary = "Add items")
public List<OrderRequestItem> addInventories(@PathVariable("id") final Long orderId, @RequestBody final List<OrderRequestedInventory> inventories) {
final OrderRequest orderRequest = crudService.get(orderId);
return crudService.addInventories(orderRequest, inventories);
}
/**
* Update item status.
*
* @param orderId the order id
* @param newStatus the new status
* @param itemIds the item ids
* @return the list
*/
@PostMapping(value = "/items/{id}/status/{newStatus}")
@Operation(operationId = "updateItemStatus", summary = "Change item status", description = "Update status of selected request items")
public List<OrderRequestItem> updateItemStatus(@PathVariable("id") final Long orderId, @PathVariable("newStatus") @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS) String newStatus,
@RequestBody Set<Long> itemIds) {
final OrderRequest orderRequest = crudService.get(orderId);
return crudService.updateItemStatus(orderRequest, newStatus, itemIds);
}
/**
* Removing items from the order request.
*
* @param orderId the order request ID
* @param itemIds item Ids to be deleted from the order
* @return updated order request
*/
@PostMapping(value = "/items/{id}/remove", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Remove items from order request", summary = "Remove items")
public OrderRequest removeOrderItems(@PathVariable("id") final Long orderId, @RequestBody final Set<Long> itemIds) {
final OrderRequest orderRequest = crudService.get(orderId);
return crudService.removeOrderItems(orderRequest, itemIds);
}
}
......@@ -15,6 +15,9 @@
*/
package org.gringlobal.service;
import java.util.Collection;
import java.util.List;
import org.gringlobal.model.OrderRequestItem;
import org.gringlobal.service.filter.OrderRequestItemFilter;
......@@ -23,4 +26,6 @@ import org.gringlobal.service.filter.OrderRequestItemFilter;
*/
public interface OrderRequestItemService extends FilteredCRUDService<OrderRequestItem, OrderRequestItemFilter> {
List<OrderRequestItem> updateAll(Collection<OrderRequestItem> items);
}
......@@ -15,12 +15,17 @@
*/
package org.gringlobal.service.impl;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import org.apache.commons.collections4.CollectionUtils;
import org.gringlobal.api.InvalidApiUsageException;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.OrderRequest;
import org.gringlobal.model.OrderRequestItem;
......@@ -168,9 +173,33 @@ public class OrderRequestItemServiceImpl extends FilteredCRUDServiceImpl<OrderRe
@Override
@Transactional
public OrderRequestItem update(OrderRequestItem updated, OrderRequestItem target) {
// TODO Auto-generated method stub
return null;
public OrderRequestItem update(OrderRequestItem input, OrderRequestItem target) {
LOG.debug("Update OrderRequestItem. Input data {}", input);
// save order request
input.setOrderRequest(target.getOrderRequest());
target.apply(input);
var saved = repository.save(target);
return _lazyLoad(saved);
}
@Override
@Transactional
public List<OrderRequestItem> updateAll(Collection<OrderRequestItem> items) {
if (CollectionUtils.isEmpty(items))
return Collections.emptyList();
List<OrderRequestItem> result = new ArrayList<>(items.size());
for (OrderRequestItem item : items) {
if (item.isNew())
throw new InvalidApiUsageException("Unable to update non-existing OrderRequestItem.");
OrderRequestItem target = reload(item);
result.add(update(item, target));
}
return result;
}
}
......@@ -30,6 +30,7 @@ import org.gringlobal.model.Cooperator;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.OrderRequest;
import org.gringlobal.model.OrderRequestItem;
import org.gringlobal.model.QOrderRequestItem;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.persistence.OrderRequestActionRepository;
import org.gringlobal.persistence.OrderRequestItemActionRepository;
......@@ -403,6 +404,110 @@ public class OrderRequestControllerTest extends AbstractApiV1Test {
/*@formatter:on*/
}
@Test
public void updateItemsTest() throws Exception {
Accession accession = addAccessionToDB();
assertThat(accession, is(notNullValue()));
assertThat(accessionRepository.count(), is(1L));
Inventory inventory = addInventoryToDB(accession, INVENTORY_NUMBER_PART1_DEFAULT, null, null);
Inventory inventory2 = addInventoryToDB(accession, INVENTORY_NUMBER_PART1_DEFAULT_2, null, null);
assertThat(inventory, is(notNullValue()));
assertThat(inventory2, is(notNullValue()));
assertThat(inventoryRepository.count(), is(3L));
Cooperator cooperator = cooperatorRepository.findAll().get(0);
assertThat(cooperator, is(notNullValue()));
OrderRequest orderRequest = orderRequestService.create(build(cooperator, NOTE, LOCAL_NUMBER, INTENDED_USE_CODE, ORDER_TYPE_CODE));
assertThat(orderRequest, is(notNullValue()));
assertThat(orderRequestRepository.count(), is(1L));
assertThat(orderRequestItemRepository.count(), is(0L));
List<OrderRequestedInventory> requestedItems = Lists.newArrayList(new OrderRequestedInventory(inventory.getId()), new OrderRequestedInventory(inventory2.getId()));
List<OrderRequestItem> addedItems = orderRequestService.addInventories(orderRequest, requestedItems);
assertThat(addedItems.size(), is(2));
assertThat(orderRequestItemRepository.count(), is(2L));
// update inventories
OrderRequestItem toUpdate1 = addedItems.get(0);
toUpdate1.setInventory(toUpdate1.getInventory().equals(inventory) ? inventory2 : inventory);
OrderRequestItem toUpdate2 = addedItems.get(1);
toUpdate2.setInventory(toUpdate2.getInventory().equals(inventory) ? inventory2 : inventory);
/*@formatter:off*/
mockMvc
.perform(put(OrderRequestController.OrderRequestItemsController.API_URL + "/update-list")
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(verboseMapper.writeValueAsString(List.of(toUpdate1, toUpdate2))))
// .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)))
.andExpect(jsonPath("$.[0]").isMap())
.andExpect(jsonPath("$.[0].inventory").isMap())
.andExpect(jsonPath("$.[1]").isMap())
.andExpect(jsonPath("$.[1].inventory").isMap())
;
/*@formatter:on*/
OrderRequestItem orderRequestItem1 = orderRequestItemRepository.findOne(QOrderRequestItem.orderRequestItem.id.eq(toUpdate1.getId())
.and(QOrderRequestItem.orderRequestItem.inventory.eq(inventory2))).orElse(null);
assertThat(orderRequestItem1, is(notNullValue()));
OrderRequestItem orderRequestItem2 = orderRequestItemRepository.findOne(QOrderRequestItem.orderRequestItem.id.eq(toUpdate2.getId())
.and(QOrderRequestItem.orderRequestItem.inventory.eq(inventory))).orElse(null);
assertThat(orderRequestItem2, is(notNullValue()));
}
@Test
public void updateSingleItemTest() throws Exception {
Accession accession = addAccessionToDB();
assertThat(accession, is(notNullValue()));
assertThat(accessionRepository.count(), is(1L));
Inventory inventory = addInventoryToDB(accession, INVENTORY_NUMBER_PART1_DEFAULT, null, null);
Inventory inventory2 = addInventoryToDB(accession, INVENTORY_NUMBER_PART1_DEFAULT_2, null, null);
assertThat(inventory, is(notNullValue()));
assertThat(inventory2, is(notNullValue()));
assertThat(inventoryRepository.count(), is(3L));
Cooperator cooperator = cooperatorRepository.findAll().get(0);
assertThat(cooperator, is(notNullValue()));
OrderRequest orderRequest = orderRequestService.create(build(cooperator, NOTE, LOCAL_NUMBER, INTENDED_USE_CODE, ORDER_TYPE_CODE));
assertThat(orderRequest, is(notNullValue()));
assertThat(orderRequestRepository.count(), is(1L));
assertThat(orderRequestItemRepository.count(), is(0L));
List<OrderRequestedInventory> requestedItems = Lists.newArrayList(new OrderRequestedInventory(inventory.getId()));
List<OrderRequestItem> addedItems = orderRequestService.addInventories(orderRequest, requestedItems);
assertThat(addedItems.size(), is(1));
assertThat(orderRequestItemRepository.count(), is(1L));
// update inventory
OrderRequestItem toUpdate = addedItems.get(0);
assertThat(toUpdate.getInventory(), equalToObject(inventory));
toUpdate.setInventory(inventory2);
/*@formatter:off*/
mockMvc
.perform(put(OrderRequestController.OrderRequestItemsController.API_URL)
.contentType(MediaType.APPLICATION_JSON_VALUE)
.content(verboseMapper.writeValueAsString(toUpdate)))
// .andDo(org.springframework.test.web.servlet.result.MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$").isMap())
.andExpect(jsonPath("$.id", is(toUpdate.getId().intValue())))
.andExpect(jsonPath("$.inventory").isMap())
.andExpect(jsonPath("$.inventory.id", is(inventory2.getId().intValue())))
;
/*@formatter:on*/
}
@Test
public void removeTest() throws Exception {
Cooperator cooperator = cooperatorRepository.findAll().get(0);
......
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