use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart 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.ReadableShoppingCart 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.ReadableShoppingCart 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.ReadableShoppingCart in project shopizer by shopizer-ecommerce.
the class ShoppingCartFacadeImpl method addToCart.
@Override
public ReadableShoppingCart addToCart(Customer customer, PersistableShoppingCartItem item, MerchantStore store, Language language) throws Exception {
Validate.notNull(customer, "Customer cannot be null");
Validate.notNull(customer.getId(), "Customer.id cannot be null or empty");
ShoppingCart cartModel = shoppingCartService.getShoppingCart(customer);
// if cart does not exist create a new one
if (cartModel == null) {
cartModel = new ShoppingCart();
cartModel.setCustomerId(customer.getId());
cartModel.setMerchantStore(store);
cartModel.setShoppingCartCode(uniqueShoppingCartCode());
}
return readableShoppingCart(cartModel, item, store, language);
}
use of com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart 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