Subversion Repositories SmartDukaan

Rev

Rev 26607 | Rev 28742 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.cart.CartService;
import com.spice.profitmandi.dao.entity.dtr.UserAccount;
import com.spice.profitmandi.dao.enumuration.dtr.AccountType;
import com.spice.profitmandi.dao.model.AddCartRequest;
import com.spice.profitmandi.dao.model.CartItem;
import com.spice.profitmandi.dao.model.CartItemResponseModel;
import com.spice.profitmandi.dao.model.CartResponse;
import com.spice.profitmandi.dao.model.ProductPojo;
import com.spice.profitmandi.dao.model.UserCart;
import com.spice.profitmandi.dao.repository.catalog.ItemRepository;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.util.ContentPojoPopulator;
import com.spice.profitmandi.service.inventory.ItemBucketService;
import com.spice.profitmandi.thrift.clients.UserClient;
import com.spice.profitmandi.web.res.ValidateCartResponse;

import in.shop2020.model.v1.user.ItemQuantity;
import in.shop2020.model.v1.user.UserContextService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class CartController {

        private static final Logger logger = LogManager.getLogger(CartController.class);

        @Autowired
        private ResponseSender<?> responseSender;

        @Autowired
        private UserAccountRepository userAccountRepository;

        @Autowired
        private ItemBucketService itemBucketService;

        @Autowired
        CartService cartService;

        @Autowired
        private ContentPojoPopulator contentPojoPopulator;

        @Autowired
        private ItemRepository itemRepository;

        public static final Map<String, Integer> MIN_BRAND_QTY_LIMIT = new HashMap<>();

        static {
                // MIN_BRAND_QTY_LIMIT.put("Realme", 10);
                MIN_BRAND_QTY_LIMIT.put("Reliance", 5);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "Add items to cart")
        public ResponseEntity<?> validateCart(HttpServletRequest request,
                        @RequestParam(value = "pincode", defaultValue = "110001") String pincode, @RequestParam int bucketId)
                        throws Throwable {

                AddCartRequest cartRequest = new AddCartRequest();
                List<CartItem> ci = new ArrayList<>();
                itemBucketService.getBucketDetails(bucketId).stream().forEach(x -> {
                        ci.add(new CartItem(x.getQuantity(), x.getItemId()));
                });
                cartRequest.setCartItems(ci);
                return this.validateCart(request, cartRequest, "110001");

        }

        @RequestMapping(value = ProfitMandiConstants.URL_CART, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
        @ApiOperation(value = "Add items to cart")
        public ResponseEntity<?> validateCart(HttpServletRequest request, @RequestBody AddCartRequest cartRequest,
                        @RequestParam(value = "pincode", defaultValue = "110001") String pincode) throws Throwable {
                UserAccount userAccount = null;
                ValidateCartResponse vc = null;
                int userId = (int) request.getAttribute("userId");

                int cartId = userAccountRepository.selectByUserIdType(userId, AccountType.cartId).getAccountKey();
                List<CartItem> cartItems = cartRequest.getCartItems();
                cartService.addItemsToCart(cartId, cartItems);
                CartResponse cartValidationResponse = cartService.getCartValidation(cartId);
                for (CartItemResponseModel cartItem : cartValidationResponse.getCartItems()) {
                        ProductPojo pp = contentPojoPopulator.getShortContent(cartItem.getCatalogItemId());
                        if(pp!=null) {
                                cartItem.setImageUrl(pp.getImageUrl());
                                cartItem.setTitle(pp.getTitle());
                        }
                }
                vc = new ValidateCartResponse(cartValidationResponse, "Success", "Items added to cart successfully");
                return responseSender.ok(vc);
        }

        @RequestMapping(value = ProfitMandiConstants.URL_CART_CHANGE_ADDRESS, method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                        @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })

        @ApiOperation(value = "Change address")
        public ResponseEntity<?> changeAddress(HttpServletRequest request,
                        @RequestParam(value = "addressId") long addressId) throws Throwable {
                UserCart uc = userAccountRepository.getUserCart((int) request.getAttribute("userId"));
                cartService.addAddressToCart(uc.getCartId(), addressId);
                return responseSender.ok("Address Changed successfully");
        }
}