Subversion Repositories SmartDukaan

Rev

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