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();
}
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();
}
}
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 };
}
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;
}
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;
}
Aggregations