use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ServicesTestSupport method sampleCart.
protected ReadableShoppingCart sampleCart() {
ReadableProduct product = sampleProduct("sampleCart");
assertNotNull(product);
PersistableShoppingCartItem cartItem = new PersistableShoppingCartItem();
cartItem.setProduct(product.getId());
cartItem.setQuantity(1);
final HttpEntity<PersistableShoppingCartItem> cartEntity = new HttpEntity<>(cartItem, getHeader());
final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.postForEntity(String.format("/api/v1/cart/"), cartEntity, ReadableShoppingCart.class);
assertNotNull(response);
assertThat(response.getStatusCode(), is(CREATED));
return response.getBody();
}
use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartAPIIntegrationTest method updateMultiWCartId.
/**
* Update cart items with qty 2 (1) on existing items & adding new item with qty 1 which gives result 2x2+1 = 5
*
* @throws Exception
*/
@Test
@Order(4)
public void updateMultiWCartId() throws Exception {
PersistableShoppingCartItem cartItem1 = new PersistableShoppingCartItem();
cartItem1.setProduct(data.getProducts().get(0).getId());
cartItem1.setQuantity(2);
PersistableShoppingCartItem cartItem2 = new PersistableShoppingCartItem();
cartItem2.setProduct(data.getProducts().get(1).getId());
cartItem2.setQuantity(2);
PersistableShoppingCartItem cartItem3 = new PersistableShoppingCartItem();
cartItem3.setProduct(data.getProducts().get(2).getId());
cartItem3.setQuantity(1);
PersistableShoppingCartItem[] productsQtyUpdates = { cartItem1, cartItem2, cartItem3 };
final HttpEntity<PersistableShoppingCartItem[]> cartEntity = new HttpEntity<>(productsQtyUpdates, getHeader());
final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.exchange(String.format("/api/v1/cart/" + data.getCartId() + "/multi"), HttpMethod.POST, cartEntity, ReadableShoppingCart.class);
assertNotNull(response);
assertThat(response.getStatusCode(), is(CREATED));
assertEquals(5, response.getBody().getQuantity());
}
use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartAPIIntegrationTest method addToCart.
/**
* Add an Item & Create cart, whould give HTTP 201 & 1 qty
*
* @throws Exception
*/
@Test
@Order(1)
public void addToCart() throws Exception {
ReadableProduct product = sampleProduct("addToCart");
assertNotNull(product);
data.getProducts().add(product);
PersistableShoppingCartItem cartItem = new PersistableShoppingCartItem();
cartItem.setProduct(product.getId());
cartItem.setQuantity(1);
final HttpEntity<PersistableShoppingCartItem> cartEntity = new HttpEntity<>(cartItem, getHeader());
final ResponseEntity<ReadableShoppingCart> response = testRestTemplate.postForEntity(String.format("/api/v1/cart/"), cartEntity, ReadableShoppingCart.class);
data.setCartId(response.getBody().getCode());
assertNotNull(response);
assertThat(response.getStatusCode(), is(CREATED));
assertEquals(response.getBody().getQuantity(), 1);
}
use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method createCartItems.
// used for api
private List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> createCartItems(ShoppingCart cartModel, List<PersistableShoppingCartItem> shoppingCartItems, MerchantStore store) throws Exception {
List<Long> productIds = shoppingCartItems.stream().map(s -> Long.valueOf(s.getProduct())).collect(Collectors.toList());
List<Product> products = productService.getProductsByIds(productIds);
if (products == null || products.size() != shoppingCartItems.size()) {
LOG.warn("----------------------- Items with in id-list " + productIds + " does not exist");
throw new ResourceNotFoundException("Item with id " + productIds + " does not exist");
}
List<Product> wrongStoreProducts = products.stream().filter(p -> p.getMerchantStore().getId() != store.getId()).collect(Collectors.toList());
if (wrongStoreProducts.size() > 0) {
throw new ResourceNotFoundException("One or more of the items with id's " + wrongStoreProducts.stream().map(s -> Long.valueOf(s.getId())).collect(Collectors.toList()) + " does not belong to merchant " + store.getId());
}
List<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = new ArrayList<>();
for (Product p : products) {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem item = shoppingCartService.populateShoppingCartItem(p);
Optional<PersistableShoppingCartItem> oShoppingCartItem = shoppingCartItems.stream().filter(i -> i.getProduct() == p.getId()).findFirst();
if (!oShoppingCartItem.isPresent()) {
// Should never happen if not something is updated in realtime or user has item in local storage and add it long time after to cart!
LOG.warn("Missing shoppingCartItem for product " + p.getSku() + " ( " + p.getId() + " )");
continue;
}
PersistableShoppingCartItem shoppingCartItem = oShoppingCartItem.get();
item.setQuantity(shoppingCartItem.getQuantity());
item.setShoppingCart(cartModel);
/**
* Check if product is available
* Check if product quantity is 0
* Check if date available <= now
*/
if (!p.isAvailable()) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
Set<ProductAvailability> availabilities = p.getAvailabilities();
if (availabilities == null) {
throw new Exception("Item with id " + p.getId() + " is not properly configured");
}
for (ProductAvailability availability : availabilities) {
if (availability.getProductQuantity() == null || availability.getProductQuantity().intValue() == 0) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
}
if (!DateUtil.dateBeforeEqualsDate(p.getDateAvailable(), new Date())) {
throw new Exception("Item with id " + p.getId() + " is not available");
}
// end qty & availablility checks
// set attributes
List<com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute> attributes = shoppingCartItem.getAttributes();
if (!CollectionUtils.isEmpty(attributes)) {
for (com.salesmanager.shop.model.catalog.product.attribute.ProductAttribute attribute : attributes) {
ProductAttribute productAttribute = productAttributeService.getById(attribute.getId());
if (productAttribute != null && productAttribute.getProduct().getId().longValue() == p.getId().longValue()) {
com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem attributeItem = new com.salesmanager.core.model.shoppingcart.ShoppingCartAttributeItem(item, productAttribute);
item.addAttributes(attributeItem);
}
}
}
items.add(item);
}
return items;
}
use of com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method modifyCart.
private ReadableShoppingCart modifyCart(ShoppingCart cartModel, PersistableShoppingCartItem item, MerchantStore store, Language language) throws Exception {
com.salesmanager.core.model.shoppingcart.ShoppingCartItem itemModel = createCartItem(cartModel, item, store);
boolean itemModified = false;
// check if existing product
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> items = cartModel.getLineItems();
if (!CollectionUtils.isEmpty(items)) {
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> newItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
Set<com.salesmanager.core.model.shoppingcart.ShoppingCartItem> removeItems = new HashSet<com.salesmanager.core.model.shoppingcart.ShoppingCartItem>();
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem anItem : items) {
// take care of existing product
if (itemModel.getProduct().getId().longValue() == anItem.getProduct().getId()) {
if (item.getQuantity() == 0) {
// left aside item to be removed
// don't add it to new list of item
removeItems.add(anItem);
} else {
// new quantity
anItem.setQuantity(item.getQuantity());
newItems.add(anItem);
}
itemModified = true;
} else {
newItems.add(anItem);
}
}
if (!removeItems.isEmpty()) {
for (com.salesmanager.core.model.shoppingcart.ShoppingCartItem emptyItem : removeItems) {
shoppingCartService.deleteShoppingCartItem(emptyItem.getId());
}
}
if (!itemModified) {
newItems.add(itemModel);
}
if (newItems.isEmpty()) {
newItems = null;
}
cartModel.setLineItems(newItems);
} else {
// new item
if (item.getQuantity() > 0) {
cartModel.getLineItems().add(itemModel);
}
}
// promo code added to the cart but no promo cart exists
if (!StringUtils.isBlank(item.getPromoCode()) && StringUtils.isBlank(cartModel.getPromoCode())) {
cartModel.setPromoCode(item.getPromoCode());
cartModel.setPromoAdded(new Date());
}
saveShoppingCart(cartModel);
// refresh cart
cartModel = shoppingCartService.getById(cartModel.getId(), store);
if (cartModel == null) {
return null;
}
shoppingCartCalculationService.calculate(cartModel, store, language);
ReadableShoppingCart readableCart = new ReadableShoppingCart();
readableCart = readableShoppingCartMapper.convert(cartModel, store, language);
return readableCart;
}
Aggregations