Subversion Repositories SmartDukaan

Rev

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