Search in sources :

Example 21 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project youtubechannel by lspil.

the class Example5 method main.

public static void main(String[] args) {
    var emf = Persistence.createEntityManagerFactory("my-persistence-unit");
    var em = emf.createEntityManager();
    em.getTransaction().begin();
    CriteriaBuilder builder = em.getCriteriaBuilder();
    CriteriaQuery<Product> query = builder.createQuery(Product.class);
    // String jpql = "SELECT p FROM Product p WHERE p.price > :min AND p.price < :max";
    var min = builder.parameter(Double.class, "min");
    var max = builder.parameter(Double.class, "max");
    Root<Product> product = query.from(Product.class);
    query.select(product).where(builder.and(builder.greaterThan(product.get("price"), min), builder.lessThanOrEqualTo(product.get("price"), max)));
    TypedQuery<Product> tq = em.createQuery(query);
    tq.setParameter("min", 10.0);
    tq.setParameter("max", 20.0);
    List<Product> result = tq.getResultList();
    result.forEach(System.out::println);
    em.getTransaction().commit();
    em.close();
}
Also used : CriteriaBuilder(javax.persistence.criteria.CriteriaBuilder) Product(entities.Product)

Example 22 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project youtubechannel by lspil.

the class ProductService method addProduct.

public void addProduct(String name) {
    var em = emf.createEntityManager();
    var repository = new ProductRepository(em);
    Product p = new Product();
    p.setName(name);
    try {
        em.getTransaction().begin();
        repository.addProduct(p);
        em.getTransaction().commit();
    } catch (RuntimeException e) {
        em.getTransaction().rollback();
    } finally {
        em.close();
    }
}
Also used : ProductRepository(repositories.ProductRepository) Product(entities.Product)

Example 23 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product 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 };
}
Also used : ProductAvailabilityEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx) WarehouseCoordsBindingEx(com.ncedu.fooddelivery.api.v1.errors.orderRegistration.WarehouseCoordsBindingEx) NotFoundEx(com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx) OrderProductPosition(com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition) ProductPosition(com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition) WarehouseInfoDTO(com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)

Example 24 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2021-msk-food-delivery by netcracker-edu.

the class ProductServiceImpl method createProduct.

@Override
public isCreatedDTO createProduct(ProductCreateDTO newProduct) {
    Product product = productMapper.mapToEntity(newProduct);
    product = productRepo.save(product);
    isCreatedDTO createdDTO = new isCreatedDTO();
    createdDTO.setId(product.getId());
    return createdDTO;
}
Also used : Product(com.ncedu.fooddelivery.api.v1.entities.Product) com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO(com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)

Example 25 with Product

use of com.ncedu.fooddelivery.api.v1.entities.Product in project 2021-msk-food-delivery by netcracker-edu.

the class ProductServiceImpl method switchInShowcaseStatus.

@Override
public boolean switchInShowcaseStatus(Long id) {
    Product product = getProductById(id);
    boolean newInShowcase = !product.getInShowcase();
    product.setInShowcase(newInShowcase);
    productRepo.save(product);
    return newInShowcase;
}
Also used : Product(com.ncedu.fooddelivery.api.v1.entities.Product)

Aggregations

Product (com.ncedu.fooddelivery.api.v1.entities.Product)17 Test (org.junit.jupiter.api.Test)12 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)12 WarehouseInfoDTO (com.ncedu.fooddelivery.api.v1.dto.warehouseDTOs.WarehouseInfoDTO)10 OrderProductPosition (com.ncedu.fooddelivery.api.v1.entities.orderProductPosition.OrderProductPosition)9 ProductPosition (com.ncedu.fooddelivery.api.v1.entities.productPosition.ProductPosition)9 Product (entities.Product)9 ProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.ProductDTO)8 SearchProductDTO (com.ncedu.fooddelivery.api.v1.dto.product.SearchProductDTO)7 CriteriaBuilder (javax.persistence.criteria.CriteriaBuilder)6 Point (com.vividsolutions.jts.geom.Point)5 Pageable (org.springframework.data.domain.Pageable)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)4 CoordsDTO (com.ncedu.fooddelivery.api.v1.dto.CoordsDTO)3 NotFoundEx (com.ncedu.fooddelivery.api.v1.errors.notfound.NotFoundEx)3 PageImpl (org.springframework.data.domain.PageImpl)3 com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO (com.ncedu.fooddelivery.api.v1.dto.isCreatedDTO)2 Order (com.ncedu.fooddelivery.api.v1.entities.order.Order)2 ProductAvailabilityEx (com.ncedu.fooddelivery.api.v1.errors.orderRegistration.ProductAvailabilityEx)2