Subversion Repositories SmartDukaan

Rev

Rev 22040 | Rev 22931 | Go to most recent revision | 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

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
        ResponseSender<?> responseSender;
        
        private static final Logger LOGGER=LoggerFactory.getLogger(AddressController.class);
        
        @Autowired
        AddressRepository addressRepository;
        
        @Autowired
        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.selectAll(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){
                try {
                        RestClient rc = new RestClient(SchemeType.HTTP, host, port);
                        Map<String, String> params = new HashMap<>();
                        String response = rc.get("pincodeValidation/" + pinCode, 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);
                }
        }
        
}