Subversion Repositories SmartDukaan

Rev

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

package com.spice.profitmandi.web.controller;

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

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.JsonArray;
import com.eclipsesource.json.JsonObject;
import com.eclipsesource.json.JsonValue;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
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.web.model.ProfitMandiResponse;
import com.spice.profitmandi.web.model.ResponseStatus;
import com.spice.profitmandi.web.res.DealBrands;
import com.spice.profitmandi.web.res.DealObjectResponse;
import com.spice.profitmandi.web.res.DealsResponse;

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

@Controller
public class DealsController {

        private static final Logger logger=LoggerFactory.getLogger(DealsController.class);

        @Value("${python.api.host}")
        private String host;
        @Value("${python.api.port}")
        private int port;

        @RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Get deals")
        public ResponseEntity<?> getDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,@RequestParam(value="offset") String offset, @RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="direction", required=false) String direction, @RequestParam(value="filterData", required=false) String filterData ){
                logger.info("Request "+request.getParameterMap());
                String response = null;
                int userId = (int)request.getAttribute("userId");
                //TODO: move to properties
                String uri = "/deals/"+userId;
                RestClient rc = new RestClient(SchemeType.HTTP, host , port);
                Map<String, String> params = new HashMap<>();
                params.put("offset", offset);
                params.put("limit", limit);
                params.put("categoryId", categoryId);
                params.put("direction", direction);
                params.put("sort", sort);
                params.put("filterData", filterData);
                List<Object> responseObject = new ArrayList<>();
                try {
                        response = rc.get(uri, params);
                } catch (Exception | ProfitMandiBusinessException e) {
                        logger.error("Unable to get deals",e);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                JsonArray result_json = Json.parse(response).asArray();
                for (JsonValue j : result_json ){
                        logger.info("res "+j.asArray());
                        List<Object> innerObject = new ArrayList<>();
                        for (JsonValue jsonObject : j.asArray()){
                                innerObject.add(toDealObject(jsonObject.asObject()));
                        }
                        if (innerObject.size() > 0){
                                responseObject.add(innerObject);
                        }
                }
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

        private Object toDealObject(JsonObject jsonObject){
                if (jsonObject.get("dealObject") != null &&  jsonObject.get("dealObject").asInt() == 1){
                        return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
                }
                return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
        }
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Get brand list and count for category")
        public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
                logger.info("Request "+request.getParameterMap());
                String response = null;
                //TODO: move to properties
                String uri = ProfitMandiConstants.URL_BRANDS;
                RestClient rc = new RestClient(SchemeType.HTTP, host , port);
                Map<String, String> params = new HashMap<>();
                params.put("category_id", category_id);
                List<DealBrands> dealBrandsResponse = null;
                try {
                        response = rc.get(uri, params);
                } catch (Exception | ProfitMandiBusinessException e) {
                        logger.error("Unable to get deals",e);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealBrandsResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
        @ApiImplicitParams({
                @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
                                required = true, dataType = "string", paramType = "header")
        })
        @ApiOperation(value = "Get unit deal object")
        public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value="id") long id){
                String response = null;
                //TODO: move to properties
                String uri = "getDealById/"+id;
                System.out.println("Unit deal "+uri);
                RestClient rc = new RestClient(SchemeType.HTTP, host , port);
                Map<String, String> params = new HashMap<>();
                DealsResponse dealsResponse = null;
                try {
                        response = rc.get(uri, params);
                } catch (Exception | ProfitMandiBusinessException e) {
                        logger.error("Unable to get deals",e);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealsResponse);
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
                }
                JsonObject result_json = Json.parse(response).asObject();
                if (!result_json.isEmpty()){
                        dealsResponse = new Gson().fromJson(response, DealsResponse.class);
                }
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealsResponse);
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }

}