Subversion Repositories SmartDukaan

Rev

Rev 37640 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 37640 Rev 37706
Line 118... Line 118...
118
        }
118
        }
119
        return null;
119
        return null;
120
    }
120
    }
121
 
121
 
122
    private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
122
    private void createCartItem(int cartId, int itemId, int quantity, float sellingPrice) {
-
 
123
        // Cart row first, then lines -- see CartRepository.selectByIdForUpdate.
-
 
124
        cartRepository.selectByIdForUpdate(cartId);
123
        CartLine cartLine = new CartLine();
125
        CartLine cartLine = new CartLine();
124
        cartLine.setActualPrice(sellingPrice);
126
        cartLine.setActualPrice(sellingPrice);
125
        cartLine.setCartId(cartId);
127
        cartLine.setCartId(cartId);
126
        cartLine.setQuantity(quantity);
128
        cartLine.setQuantity(quantity);
127
        // TODO: Estmiate Logic
129
        // TODO: Estmiate Logic
Line 130... Line 132...
130
 
132
 
131
    }
133
    }
132
 
134
 
133
    @Override
135
    @Override
134
    public boolean clearCart(int cartId) throws ProfitMandiBusinessException {
136
    public boolean clearCart(int cartId) throws ProfitMandiBusinessException {
-
 
137
        // Cart row first, then lines -- see CartRepository.selectByIdForUpdate.
-
 
138
        cartRepository.selectByIdForUpdate(cartId);
135
        List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
139
        List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
136
        cartLines.stream().forEach(cartLine -> cartLineRepository.delete(cartLine));
140
        cartLines.stream().forEach(cartLine -> cartLineRepository.delete(cartLine));
137
        Cart cart = cartRepository.selectById(cartId);
141
        Cart cart = cartRepository.selectById(cartId);
138
 
142
 
139
        cart.setUpdateTimestamp(LocalDateTime.now());
143
        cart.setUpdateTimestamp(LocalDateTime.now());
Line 188... Line 192...
188
 
192
 
189
    }
193
    }
190
 
194
 
191
    @Override
195
    @Override
192
    public CartResponse getCartValidation(int cartId) throws ProfitMandiBusinessException {
196
    public CartResponse getCartValidation(int cartId) throws ProfitMandiBusinessException {
-
 
197
        // This method WRITES (cart.totalPrice, cart_line qty/price, line deletes) despite its
-
 
198
        // name, so it is a mutating path: cart row first, then lines.
-
 
199
        // See CartRepository.selectByIdForUpdate.
-
 
200
        cartRepository.selectByIdForUpdate(cartId);
193
        /*
201
        /*
194
         * # No need to validate duplicate items since there are only two ways # to add
202
         * # No need to validate duplicate items since there are only two ways # to add
195
         * items to a cart and both of them check whether the item being # added is a
203
         * items to a cart and both of them check whether the item being # added is a
196
         * duplicate of an already existing item.
204
         * duplicate of an already existing item.
197
         */
205
         */
Line 365... Line 373...
365
        this.addItemsToCart(cartId, cartItems);
373
        this.addItemsToCart(cartId, cartItems);
366
    }
374
    }
367
 
375
 
368
    @Override
376
    @Override
369
    public void addItemsToCart(int cartId, List<CartItem> cartItems) throws ProfitMandiBusinessException {
377
    public void addItemsToCart(int cartId, List<CartItem> cartItems) throws ProfitMandiBusinessException {
-
 
378
        // Take the cart row BEFORE any line is touched -- see CartRepository.selectByIdForUpdate.
-
 
379
        // Inserting a line shared-locks this same row anyway via line_cart_id_fk, so without this
-
 
380
        // the lock order depends on which statement happens to run first and two concurrent edits
-
 
381
        // to one cart can deadlock.
-
 
382
        cartRepository.selectByIdForUpdate(cartId);
370
        logger.info("cart items {}", cartItems);
383
        logger.info("cart items {}", cartItems);
371
        cartItems = cartItems.stream().filter(x -> x.getQuantity() > 0).collect(Collectors.toList());
384
        cartItems = cartItems.stream().filter(x -> x.getQuantity() > 0).collect(Collectors.toList());
372
        Map<Integer, Integer> itemQuantityMap = cartItems.stream()
385
        Map<Integer, Integer> itemQuantityMap = cartItems.stream()
373
                .collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
386
                .collect(Collectors.toMap(x -> x.getItemId(), x -> x.getQuantity()));
374
        List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
387
        List<CartLine> cartLines = cartLineRepository.selectAllByCart(cartId);
Line 404... Line 417...
404
 
417
 
405
    }
418
    }
406
 
419
 
407
    @Override
420
    @Override
408
    public void addShoppingBag(int cartId, long qty) {
421
    public void addShoppingBag(int cartId, long qty) {
-
 
422
        // Cart row first, then lines -- see CartRepository.selectByIdForUpdate.
-
 
423
        cartRepository.selectByIdForUpdate(cartId);
409
        CartLine cl = new CartLine();
424
        CartLine cl = new CartLine();
410
        cl.setItemId(ProfitMandiConstants.ITEM_CARRY_BAG);
425
        cl.setItemId(ProfitMandiConstants.ITEM_CARRY_BAG);
411
        cl.setQuantity((int) qty);
426
        cl.setQuantity((int) qty);
412
        cl.setCartId(cartId);
427
        cl.setCartId(cartId);
413
        cl.setCreateTimestamp(LocalDateTime.now());
428
        cl.setCreateTimestamp(LocalDateTime.now());