use of com.ncedu.fooddelivery.api.v1.dto.CoordsDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProductServiceImpl method getProductsCount.
@Override
public CountDTO getProductsCount(CoordsDTO coordinates) {
Long warehouseId = getWarehouseIdByCoordinates(coordinates);
int count = productRepo.findAllCount(warehouseId);
return new CountDTO(count);
}
use of com.ncedu.fooddelivery.api.v1.dto.CoordsDTO in project 2021-msk-food-delivery by netcracker-edu.
the class OrderServiceImpl1 method countOrderCost.
@Override
public Double[] countOrderCost(CoordsDTO geo, HashMap<Long, Integer> pairs, Long clientWarehouseId) {
WarehouseInfoDTO warehouse = warehouseService.getNearestWarehouse(geo.getLat(), geo.getLon());
if (warehouse == null)
throw new NotFoundEx(String.format("Lat: %s; lon: %s", geo.getLat().toString(), geo.getLon().toString()));
Long warehouseId = warehouse.getId();
if (!warehouseId.equals(clientWarehouseId))
throw new WarehouseCoordsBindingEx();
Map<Long, Integer> productsAvailableAmounts = new HashMap<>();
Double overallOrderCost = 0.0;
Double overallOrderDiscount = 0.0;
Double orderHighDemandCoeff;
boolean enoughProductPositions = true;
for (Map.Entry<Long, Integer> pair : pairs.entrySet()) {
Long productId = pair.getKey();
Integer requestedAmount = pair.getValue();
// 'findByProductIdAndWarehouseIdWithLock' sets lock on every product position in result set. So no one
// other thread
// will be able to interact with these positions before whole transaction will complete.
List<ProductPosition> productPositions = productPositionRepo.findByProductIdAndWarehouseIdWithLock(productId, warehouseId);
// filtering expired product positions
productPositions = productPositions.stream().filter(new Predicate<ProductPosition>() {
@Override
public boolean test(ProductPosition productPosition) {
Short expirationDays = productPosition.getProduct().getExpirationDays();
Date manufactureDate = productPosition.getManufactureDate();
Calendar c = Calendar.getInstance();
c.setTime(manufactureDate);
c.add(Calendar.DATE, expirationDays);
if (c.before(Calendar.getInstance()))
return false;
return true;
}
}).collect(Collectors.toList());
Integer overallAmount = 0;
for (ProductPosition pos : productPositions) {
overallAmount += pos.getCurrentAmount();
}
if (overallAmount < requestedAmount) {
productsAvailableAmounts.put(productId, overallAmount);
enoughProductPositions = false;
}
if (enoughProductPositions) {
// product price count
Product product = productPositions.get(0).getProduct();
Double productDiscount = product.getDiscount();
Double productPrice = product.getPrice();
overallOrderCost += requestedAmount * (productPrice - productDiscount);
overallOrderDiscount += productDiscount * requestedAmount;
}
}
if (!enoughProductPositions) {
throw new ProductAvailabilityEx(productsAvailableAmounts);
}
orderHighDemandCoeff = countHighDemandCoeff(warehouseId);
overallOrderCost = Math.round(overallOrderCost * orderHighDemandCoeff * 100.0) / 100.0;
return new Double[] { overallOrderCost, overallOrderDiscount, orderHighDemandCoeff };
}
use of com.ncedu.fooddelivery.api.v1.dto.CoordsDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProductServiceImpl method getWarehouseIdByCoordinates.
private Long getWarehouseIdByCoordinates(CoordsDTO coordinates) {
Point geo = makeGeoCoordinates(coordinates);
WarehouseInfoDTO nearestWarehouse = warehouseService.getNearestWarehouse(geo);
log.debug("WAREHOUSE " + nearestWarehouse.getId() + " for coords: " + geo);
return nearestWarehouse.getId();
}
use of com.ncedu.fooddelivery.api.v1.dto.CoordsDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProductServiceImpl method getProductsCountInShowcase.
@Override
public CountDTO getProductsCountInShowcase(CoordsDTO coordinates) {
Long warehouseId = getWarehouseIdByCoordinates(coordinates);
int countInShowcase = productRepo.findAllByInShowcaseCount(warehouseId);
return new CountDTO(countInShowcase);
}
use of com.ncedu.fooddelivery.api.v1.dto.CoordsDTO in project 2021-msk-food-delivery by netcracker-edu.
the class ProductServiceTest method getProductsInShowcaseSuccess.
@Test
public void getProductsInShowcaseSuccess() {
Pageable pageable = PageRequest.of(0, 2);
Page<Product> productPage = ProductUtils.createPageProductsInShowcase(pageable);
Long warehouseId = 4L;
Point geo = makeGeo();
WarehouseInfoDTO warehouseInfoDTO = new WarehouseInfoDTO(warehouseId, null, null, null, null, false);
when(warehouseServiceMock.getNearestWarehouse(geo)).thenReturn(warehouseInfoDTO);
when(productRepoMock.findAllByInShowcase(warehouseId, pageable)).thenReturn(productPage);
CoordsDTO coordsDTO = makeCoordsDTO();
List<ProductDTO> resultList = productService.getProductsInShowcase(coordsDTO, pageable);
List<ProductDTO> perfectList = ProductUtils.createProductDTOListFromPage(productPage);
verify(warehouseServiceMock, times(1)).getNearestWarehouse(geo);
verify(productRepoMock, times(1)).findAllByInShowcase(warehouseId, pageable);
assertEquals(perfectList, resultList);
}
Aggregations