Subversion Repositories SmartDukaan

Rev

Rev 35434 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.http.conn.HttpHostConnectException;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.eclipsesource.json.Json;
import com.eclipsesource.json.JsonObject;
import com.spice.profitmandi.common.enumuration.SchemeType;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.client.RestClient;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.model.UserCart;
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
import com.spice.profitmandi.dao.repository.user.AddressRepository;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

@Controller
@Transactional(rollbackFor = Throwable.class)
public class AddressController {
        
        @Value("${python.api.host}")
        private String host;
        @Value("${python.api.port}")
        private int port;
        
        @Autowired
        private ResponseSender<?> responseSender;
        
        private static final Logger LOGGER=LogManager.getLogger(AddressController.class);
        
        @Autowired
        private AddressRepository addressRepository;
        
        @Autowired
        private UserAccountRepository userAccountRepository;
        
        
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @RequestMapping(value = ProfitMandiConstants.URL_ADDRESS_ALL, method=RequestMethod.GET)
        public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize) throws Throwable{
                int userId = (int)request.getAttribute("userId");
                UserCart uc  = userAccountRepository.getUserCart(userId);
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                return responseSender.ok(addressRepository.selectAllByRetailerId(uc.getUserId(), pageNumber, pageSize));
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_ADDRESS_ID, method=RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        return responseSender.ok(addressRepository.selectById(id));
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }
        }

        @RequestMapping(value = "/pin-info/{pinCode}", method=RequestMethod.GET)
        public ResponseEntity<?> getPinInfo(HttpServletRequest request, @PathVariable(value="pinCode") String pinCode){
                String remoteUrl = "pincodeValidation/" + pinCode;
                try {
                        RestClient rc = new RestClient();
                        Map<String, String> params = new HashMap<>();
                        String response = rc.get(SchemeType.HTTP, host, port, remoteUrl, params);
                        JsonObject result_json = Json.parse(response).asObject();
                        return responseSender.ok(result_json);
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        return responseSender.badRequest(profitMandiBusinessException);
                }catch (HttpHostConnectException hhce) {
                        LOGGER.error("Cannot connect to remote url: {}",  remoteUrl);
                        return responseSender.badRequest(new ProfitMandiBusinessException("None", "None", "cannot connect to remote url" + remoteUrl));
                        
                }
        }
        
}