Subversion Repositories SmartDukaan

Rev

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