Subversion Repositories SmartDukaan

Rev

Rev 21740 | Rev 22273 | 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
 
11
import org.slf4j.Logger;
12
import org.slf4j.LoggerFactory;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.http.HttpStatus;
15
import org.springframework.http.MediaType;
16
import org.springframework.http.ResponseEntity;
17
import org.springframework.stereotype.Controller;
18
import org.springframework.web.bind.annotation.PathVariable;
19
import org.springframework.web.bind.annotation.RequestMapping;
20
import org.springframework.web.bind.annotation.RequestMethod;
21
import org.springframework.web.bind.annotation.RequestParam;
22
 
23
import com.eclipsesource.json.Json;
24
import com.eclipsesource.json.JsonArray;
25
import com.eclipsesource.json.JsonObject;
26
import com.eclipsesource.json.JsonValue;
27
import com.google.gson.Gson;
21356 kshitij.so 28
import com.google.gson.reflect.TypeToken;
21643 ashik.ali 29
import com.spice.profitmandi.common.enumuration.SchemeType;
21339 kshitij.so 30
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
31
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21740 ashik.ali 32
import com.spice.profitmandi.common.model.ProfitMandiResponse;
33
import com.spice.profitmandi.common.model.ResponseStatus;
21643 ashik.ali 34
import com.spice.profitmandi.common.web.client.RestClient;
21356 kshitij.so 35
import com.spice.profitmandi.web.res.DealBrands;
21339 kshitij.so 36
import com.spice.profitmandi.web.res.DealObjectResponse;
37
import com.spice.profitmandi.web.res.DealsResponse;
38
 
39
import io.swagger.annotations.ApiImplicitParam;
40
import io.swagger.annotations.ApiImplicitParams;
41
import io.swagger.annotations.ApiOperation;
42
 
43
@Controller
44
public class DealsController {
45
 
46
	private static final Logger logger=LoggerFactory.getLogger(DealsController.class);
47
 
48
	@Value("${python.api.host}")
49
	private String host;
50
	@Value("${python.api.port}")
51
	private int port;
52
 
53
	@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
54
	@ApiImplicitParams({
55
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
56
				required = true, dataType = "string", paramType = "header")
57
	})
58
	@ApiOperation(value = "Get deals")
22272 amit.gupta 59
	public ResponseEntity<?> getDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,
60
			@RequestParam(value="offset") String offset, @RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, 
61
			@RequestParam(value="direction", required=false) String direction, @RequestParam(value="filterData", required=false) String filterData ){
21339 kshitij.so 62
		logger.info("Request "+request.getParameterMap());
63
		String response = null;
21459 kshitij.so 64
		int userId = (int)request.getAttribute("userId");
21462 kshitij.so 65
		//TODO: move to properties
21339 kshitij.so 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);
22272 amit.gupta 75
		params.put("source", "deals");
21356 kshitij.so 76
		List<Object> responseObject = new ArrayList<>();
21339 kshitij.so 77
		try {
78
			response = rc.get(uri, params);
79
		} catch (Exception | ProfitMandiBusinessException e) {
80
			logger.error("Unable to get deals",e);
21356 kshitij.so 81
			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 82
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
83
		}
84
		JsonArray result_json = Json.parse(response).asArray();
85
		for (JsonValue j : result_json ){
86
			logger.info("res "+j.asArray());
87
			List<Object> innerObject = new ArrayList<>();
88
			for (JsonValue jsonObject : j.asArray()){
21356 kshitij.so 89
				innerObject.add(toDealObject(jsonObject.asObject()));
21339 kshitij.so 90
			}
91
			if (innerObject.size() > 0){
92
				responseObject.add(innerObject);
93
			}
94
		}
95
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
96
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
97
	}
98
 
22272 amit.gupta 99
 
100
	@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
101
	@ApiImplicitParams({
102
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
103
				required = true, dataType = "string", paramType = "header")
104
	})
105
	@ApiOperation(value = "Get online deals")
106
	public ResponseEntity<?> getOnlineDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,
107
			@RequestParam(value="offset") String offset, @RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, 
108
			@RequestParam(value="direction", required=false) String direction, @RequestParam(value="filterData", required=false) String filterData ){
109
		logger.info("Request "+request.getParameterMap());
110
		String response = null;
111
		int userId = (int)request.getAttribute("userId");
112
		//TODO: move to properties
113
		String uri = "/deals/"+userId;
114
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
115
		Map<String, String> params = new HashMap<>();
116
		params.put("offset", offset);
117
		params.put("limit", limit);
118
		params.put("categoryId", categoryId);
119
		params.put("direction", direction);
120
		params.put("sort", sort);
121
		params.put("source", "online");
122
		params.put("filterData", filterData);
123
		List<Object> responseObject = new ArrayList<>();
124
		try {
125
			response = rc.get(uri, params);
126
		} catch (Exception | ProfitMandiBusinessException e) {
127
			logger.error("Unable to get deals",e);
128
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
129
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
130
		}
131
		JsonArray result_json = Json.parse(response).asArray();
132
		for (JsonValue j : result_json ){
133
			logger.info("res "+j.asArray());
134
			List<Object> innerObject = new ArrayList<>();
135
			for (JsonValue jsonObject : j.asArray()){
136
				innerObject.add(toDealObject(jsonObject.asObject()));
137
			}
138
			if (innerObject.size() > 0){
139
				responseObject.add(innerObject);
140
			}
141
		}
142
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
143
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
144
	}
145
 
146
 
147
/*	@RequestMapping(value = "/direct-deals", method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
148
	@ApiImplicitParams({
149
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
150
				required = true, dataType = "string", paramType = "header")
151
	})
152
	public ResponseEntity<?> getDirectDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,@RequestParam(value="offset") String offset, 
153
			@RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="direction", required=false) String direction, 
154
			@RequestParam(value="filterData", required=false) String filterData ){
155
 
156
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
157
	}*/
158
 
21356 kshitij.so 159
	private Object toDealObject(JsonObject jsonObject){
21339 kshitij.so 160
		if (jsonObject.get("dealObject") != null &&  jsonObject.get("dealObject").asInt() == 1){
161
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
162
		}
163
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
164
	}
21356 kshitij.so 165
 
166
 
167
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
168
	@ApiImplicitParams({
169
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
170
				required = true, dataType = "string", paramType = "header")
171
	})
172
	@ApiOperation(value = "Get brand list and count for category")
173
	public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
174
		logger.info("Request "+request.getParameterMap());
175
		String response = null;
21462 kshitij.so 176
		//TODO: move to properties
21356 kshitij.so 177
		String uri = ProfitMandiConstants.URL_BRANDS;
178
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
179
		Map<String, String> params = new HashMap<>();
180
		params.put("category_id", category_id);
21358 kshitij.so 181
		List<DealBrands> dealBrandsResponse = null;
21356 kshitij.so 182
		try {
183
			response = rc.get(uri, params);
184
		} catch (Exception | ProfitMandiBusinessException e) {
185
			logger.error("Unable to get deals",e);
21358 kshitij.so 186
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealBrandsResponse);
21356 kshitij.so 187
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
188
		}
21358 kshitij.so 189
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
21356 kshitij.so 190
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
191
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
192
	}
193
 
21445 kshitij.so 194
	@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
195
	@ApiImplicitParams({
196
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
197
				required = true, dataType = "string", paramType = "header")
198
	})
199
	@ApiOperation(value = "Get unit deal object")
200
	public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value="id") long id){
201
		String response = null;
21462 kshitij.so 202
		//TODO: move to properties
21445 kshitij.so 203
		String uri = "getDealById/"+id;
204
		System.out.println("Unit deal "+uri);
205
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
206
		Map<String, String> params = new HashMap<>();
207
		DealsResponse dealsResponse = null;
208
		try {
209
			response = rc.get(uri, params);
210
		} catch (Exception | ProfitMandiBusinessException e) {
211
			logger.error("Unable to get deals",e);
212
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealsResponse);
213
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
214
		}
215
		JsonObject result_json = Json.parse(response).asObject();
216
		if (!result_json.isEmpty()){
217
			dealsResponse = new Gson().fromJson(response, DealsResponse.class);
218
		}
219
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealsResponse);
220
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
221
	}
21339 kshitij.so 222
 
223
}