Subversion Repositories SmartDukaan

Rev

Rev 21352 | Rev 21357 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21339 kshitij.so 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
4
import java.util.ArrayList;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8
 
9
import javax.servlet.http.HttpServletRequest;
10
 
21348 kshitij.so 11
import org.apache.thrift.TException;
21356 kshitij.so 12
import org.apache.thrift.transport.TTransportException;
21339 kshitij.so 13
import org.slf4j.Logger;
14
import org.slf4j.LoggerFactory;
15
import org.springframework.beans.factory.annotation.Value;
16
import org.springframework.http.HttpStatus;
17
import org.springframework.http.MediaType;
18
import org.springframework.http.ResponseEntity;
19
import org.springframework.stereotype.Controller;
20
import org.springframework.web.bind.annotation.PathVariable;
21
import org.springframework.web.bind.annotation.RequestMapping;
22
import org.springframework.web.bind.annotation.RequestMethod;
23
import org.springframework.web.bind.annotation.RequestParam;
24
 
25
import com.eclipsesource.json.Json;
26
import com.eclipsesource.json.JsonArray;
27
import com.eclipsesource.json.JsonObject;
28
import com.eclipsesource.json.JsonValue;
29
import com.google.gson.Gson;
21356 kshitij.so 30
import com.google.gson.reflect.TypeToken;
21339 kshitij.so 31
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
32
import com.spice.profitmandi.common.model.ProfitMandiConstants;
33
import com.spice.profitmandi.web.client.RestClient;
34
import com.spice.profitmandi.web.enumuration.SchemeType;
35
import com.spice.profitmandi.web.model.ProfitMandiResponse;
36
import com.spice.profitmandi.web.model.ResponseStatus;
21356 kshitij.so 37
import com.spice.profitmandi.web.res.DealBrands;
21339 kshitij.so 38
import com.spice.profitmandi.web.res.DealObjectResponse;
39
import com.spice.profitmandi.web.res.DealsResponse;
40
 
21356 kshitij.so 41
import in.shop2020.model.v1.catalog.CatalogService.Client;
42
import in.shop2020.thrift.clients.CatalogClient;
21339 kshitij.so 43
import io.swagger.annotations.ApiImplicitParam;
44
import io.swagger.annotations.ApiImplicitParams;
45
import io.swagger.annotations.ApiOperation;
46
 
47
@Controller
48
public class DealsController {
49
 
50
	private static final Logger logger=LoggerFactory.getLogger(DealsController.class);
51
 
52
	@Value("${python.api.host}")
53
	private String host;
54
	@Value("${python.api.port}")
55
	private int port;
56
 
57
	@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
58
	@ApiImplicitParams({
59
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
60
				required = true, dataType = "string", paramType = "header")
61
	})
62
	@ApiOperation(value = "Get deals")
63
	public ResponseEntity<?> getDeals(HttpServletRequest request, @PathVariable(value="userId") long userId, @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 ){
64
		logger.info("Request "+request.getParameterMap());
65
		String response = null;
66
		String uri = "/deals/"+userId;
67
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
68
		Map<String, String> params = new HashMap<>();
69
		params.put("offset", offset);
70
		params.put("limit", limit);
71
		params.put("categoryId", categoryId);
72
		params.put("direction", direction);
73
		params.put("sort", sort);
74
		params.put("filterData", filterData);
21356 kshitij.so 75
		List<Object> responseObject = new ArrayList<>();
21339 kshitij.so 76
		try {
77
			response = rc.get(uri, params);
78
		} catch (Exception | ProfitMandiBusinessException e) {
79
			logger.error("Unable to get deals",e);
21356 kshitij.so 80
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
21339 kshitij.so 81
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
82
		}
83
		JsonArray result_json = Json.parse(response).asArray();
84
		for (JsonValue j : result_json ){
85
			logger.info("res "+j.asArray());
86
			List<Object> innerObject = new ArrayList<>();
87
			for (JsonValue jsonObject : j.asArray()){
21356 kshitij.so 88
				innerObject.add(toDealObject(jsonObject.asObject()));
21339 kshitij.so 89
			}
90
			if (innerObject.size() > 0){
91
				responseObject.add(innerObject);
92
			}
93
		}
94
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
95
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
96
	}
97
 
21356 kshitij.so 98
	private Object toDealObject(JsonObject jsonObject){
21339 kshitij.so 99
		if (jsonObject.get("dealObject") != null &&  jsonObject.get("dealObject").asInt() == 1){
100
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
101
		}
102
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
103
	}
21356 kshitij.so 104
 
105
 
106
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
107
	@ApiImplicitParams({
108
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
109
				required = true, dataType = "string", paramType = "header")
110
	})
111
	@ApiOperation(value = "Get brand list and count for category")
112
	public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
113
		logger.info("Request "+request.getParameterMap());
114
		String response = null;
115
		String uri = ProfitMandiConstants.URL_BRANDS;
116
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
117
		Map<String, String> params = new HashMap<>();
118
		params.put("category_id", category_id);
119
		try {
120
			response = rc.get(uri, params);
121
		} catch (Exception | ProfitMandiBusinessException e) {
122
			logger.error("Unable to get deals",e);
123
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, new DealBrands());
124
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
125
		}
126
		List<DealBrands> dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
127
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
128
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
129
	}
130
 
21339 kshitij.so 131
 
132
}