Subversion Repositories SmartDukaan

Rev

Rev 22287 | Rev 22289 | 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;
21643 ashik.ali 36
import com.spice.profitmandi.common.web.client.RestClient;
22273 amit.gupta 37
import com.spice.profitmandi.dao.entity.catalog.Tag;
22287 amit.gupta 38
import com.spice.profitmandi.service.pricing.PricingService;
22273 amit.gupta 39
import com.spice.profitmandi.service.tag.TagService;
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");
21462 kshitij.so 74
		//TODO: move to properties
21339 kshitij.so 75
		String uri = "/deals/"+userId;
76
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
77
		Map<String, String> params = new HashMap<>();
78
		params.put("offset", offset);
79
		params.put("limit", limit);
80
		params.put("categoryId", categoryId);
81
		params.put("direction", direction);
82
		params.put("sort", sort);
83
		params.put("filterData", filterData);
22272 amit.gupta 84
		params.put("source", "deals");
22273 amit.gupta 85
		params.put("tag_ids", getCommaSeparateTags(userId));
21356 kshitij.so 86
		List<Object> responseObject = new ArrayList<>();
21339 kshitij.so 87
		try {
88
			response = rc.get(uri, params);
89
		} catch (Exception | ProfitMandiBusinessException e) {
90
			logger.error("Unable to get deals",e);
21356 kshitij.so 91
			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 92
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
93
		}
94
		JsonArray result_json = Json.parse(response).asArray();
95
		for (JsonValue j : result_json ){
96
			logger.info("res "+j.asArray());
97
			List<Object> innerObject = new ArrayList<>();
98
			for (JsonValue jsonObject : j.asArray()){
21356 kshitij.so 99
				innerObject.add(toDealObject(jsonObject.asObject()));
21339 kshitij.so 100
			}
101
			if (innerObject.size() > 0){
102
				responseObject.add(innerObject);
103
			}
104
		}
105
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
106
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
107
	}
108
 
22272 amit.gupta 109
 
22288 amit.gupta 110
	private String getCommaSeparateTags(int userId) throws Throwable{
22287 amit.gupta 111
		List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(userId);
112
		List<String> strTagIds = new ArrayList<>();
113
		for (Integer tagId : tagIds) {
114
			strTagIds.add(String.valueOf(tagId));
22273 amit.gupta 115
		}
22287 amit.gupta 116
		return String.join(",", strTagIds);
22273 amit.gupta 117
	}
118
 
119
 
120
	@RequestMapping(value = "/online-deals", method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
22272 amit.gupta 121
	@ApiImplicitParams({
122
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
123
				required = true, dataType = "string", paramType = "header")
124
	})
125
	@ApiOperation(value = "Get online deals")
126
	public ResponseEntity<?> getOnlineDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,
127
			@RequestParam(value="offset") String offset, @RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, 
22288 amit.gupta 128
			@RequestParam(value="direction", required=false) String direction, @RequestParam(value="filterData", required=false) String filterData ) throws Throwable{
22272 amit.gupta 129
		logger.info("Request "+request.getParameterMap());
130
		String response = null;
131
		int userId = (int)request.getAttribute("userId");
132
		//TODO: move to properties
133
		String uri = "/deals/"+userId;
134
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
135
		Map<String, String> params = new HashMap<>();
136
		params.put("offset", offset);
137
		params.put("limit", limit);
138
		params.put("categoryId", categoryId);
139
		params.put("direction", direction);
140
		params.put("sort", sort);
141
		params.put("source", "online");
142
		params.put("filterData", filterData);
22273 amit.gupta 143
		params.put("tag_ids", getCommaSeparateTags(userId));
22272 amit.gupta 144
		List<Object> responseObject = new ArrayList<>();
145
		try {
146
			response = rc.get(uri, params);
147
		} catch (Exception | ProfitMandiBusinessException e) {
148
			logger.error("Unable to get deals",e);
149
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
150
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
151
		}
152
		JsonArray result_json = Json.parse(response).asArray();
153
		for (JsonValue j : result_json ){
154
			logger.info("res "+j.asArray());
155
			List<Object> innerObject = new ArrayList<>();
156
			for (JsonValue jsonObject : j.asArray()){
157
				innerObject.add(toDealObject(jsonObject.asObject()));
158
			}
159
			if (innerObject.size() > 0){
160
				responseObject.add(innerObject);
161
			}
162
		}
163
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
164
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
165
	}
166
 
167
 
168
/*	@RequestMapping(value = "/direct-deals", method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
169
	@ApiImplicitParams({
170
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
171
				required = true, dataType = "string", paramType = "header")
172
	})
173
	public ResponseEntity<?> getDirectDeals(HttpServletRequest request, @RequestParam(value="categoryId") String categoryId,@RequestParam(value="offset") String offset, 
174
			@RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="direction", required=false) String direction, 
175
			@RequestParam(value="filterData", required=false) String filterData ){
176
 
177
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
178
	}*/
179
 
21356 kshitij.so 180
	private Object toDealObject(JsonObject jsonObject){
21339 kshitij.so 181
		if (jsonObject.get("dealObject") != null &&  jsonObject.get("dealObject").asInt() == 1){
182
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
183
		}
184
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
185
	}
21356 kshitij.so 186
 
187
 
188
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
189
	@ApiImplicitParams({
190
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
191
				required = true, dataType = "string", paramType = "header")
192
	})
193
	@ApiOperation(value = "Get brand list and count for category")
194
	public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
195
		logger.info("Request "+request.getParameterMap());
196
		String response = null;
21462 kshitij.so 197
		//TODO: move to properties
21356 kshitij.so 198
		String uri = ProfitMandiConstants.URL_BRANDS;
199
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
200
		Map<String, String> params = new HashMap<>();
201
		params.put("category_id", category_id);
21358 kshitij.so 202
		List<DealBrands> dealBrandsResponse = null;
21356 kshitij.so 203
		try {
204
			response = rc.get(uri, params);
205
		} catch (Exception | ProfitMandiBusinessException e) {
206
			logger.error("Unable to get deals",e);
21358 kshitij.so 207
			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 208
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
209
		}
21358 kshitij.so 210
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
21356 kshitij.so 211
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
212
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
213
	}
214
 
21445 kshitij.so 215
	@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
216
	@ApiImplicitParams({
217
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
218
				required = true, dataType = "string", paramType = "header")
219
	})
220
	@ApiOperation(value = "Get unit deal object")
221
	public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value="id") long id){
222
		String response = null;
21462 kshitij.so 223
		//TODO: move to properties
21445 kshitij.so 224
		String uri = "getDealById/"+id;
225
		System.out.println("Unit deal "+uri);
226
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
227
		Map<String, String> params = new HashMap<>();
228
		DealsResponse dealsResponse = null;
229
		try {
230
			response = rc.get(uri, params);
231
		} catch (Exception | ProfitMandiBusinessException e) {
232
			logger.error("Unable to get deals",e);
233
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealsResponse);
234
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
235
		}
236
		JsonObject result_json = Json.parse(response).asObject();
237
		if (!result_json.isEmpty()){
238
			dealsResponse = new Gson().fromJson(response, DealsResponse.class);
239
		}
240
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealsResponse);
241
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
242
	}
21339 kshitij.so 243
 
244
}