Search in sources :

Example 1 with ReadableShoppingCart

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();
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct)

Example 2 with ReadableShoppingCart

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());
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 3 with ReadableShoppingCart

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);
}
Also used : HttpEntity(org.springframework.http.HttpEntity) PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) ReadableProduct(com.salesmanager.shop.model.catalog.product.ReadableProduct) Order(org.junit.jupiter.api.Order) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Test(org.junit.jupiter.api.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 4 with ReadableShoppingCart

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);
}
Also used : ShoppingCart(com.salesmanager.core.model.shoppingcart.ShoppingCart) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)

Example 5 with ReadableShoppingCart

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;
}
Also used : PersistableShoppingCartItem(com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem) ShoppingCartItem(com.salesmanager.shop.model.shoppingcart.ShoppingCartItem) ReadableShoppingCart(com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart) Date(java.util.Date) LocalDate(java.time.LocalDate) HashSet(java.util.HashSet)

Aggregations

ReadableShoppingCart (com.salesmanager.shop.model.shoppingcart.ReadableShoppingCart)19 ShoppingCart (com.salesmanager.core.model.shoppingcart.ShoppingCart)10 PersistableShoppingCartItem (com.salesmanager.shop.model.shoppingcart.PersistableShoppingCartItem)8 ResourceNotFoundException (com.salesmanager.shop.store.api.exception.ResourceNotFoundException)6 HttpEntity (org.springframework.http.HttpEntity)6 Order (org.junit.jupiter.api.Order)5 Test (org.junit.jupiter.api.Test)5 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)5 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)5 ReadableProduct (com.salesmanager.shop.model.catalog.product.ReadableProduct)4 LocalDate (java.time.LocalDate)4 Date (java.util.Date)4 Customer (com.salesmanager.core.model.customer.Customer)3 ServiceRuntimeException (com.salesmanager.shop.store.api.exception.ServiceRuntimeException)3 ShoppingCartItem (com.salesmanager.shop.model.shoppingcart.ShoppingCartItem)2 ApiImplicitParams (io.swagger.annotations.ApiImplicitParams)2 ApiOperation (io.swagger.annotations.ApiOperation)2 Principal (java.security.Principal)2 HashSet (java.util.HashSet)2 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)2