| 21339 |
kshitij.so |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.time.LocalDateTime;
|
|
|
4 |
import java.util.ArrayList;
|
| 22336 |
amit.gupta |
5 |
import java.util.Arrays;
|
| 21339 |
kshitij.so |
6 |
import java.util.HashMap;
|
|
|
7 |
import java.util.List;
|
|
|
8 |
import java.util.Map;
|
|
|
9 |
|
|
|
10 |
import javax.servlet.http.HttpServletRequest;
|
|
|
11 |
|
| 22319 |
amit.gupta |
12 |
import org.apache.commons.lang3.StringUtils;
|
|
|
13 |
import org.json.JSONArray;
|
|
|
14 |
import org.json.JSONObject;
|
| 21339 |
kshitij.so |
15 |
import org.slf4j.Logger;
|
|
|
16 |
import org.slf4j.LoggerFactory;
|
| 22273 |
amit.gupta |
17 |
import org.springframework.beans.factory.annotation.Autowired;
|
| 21339 |
kshitij.so |
18 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
19 |
import org.springframework.http.HttpStatus;
|
|
|
20 |
import org.springframework.http.MediaType;
|
|
|
21 |
import org.springframework.http.ResponseEntity;
|
|
|
22 |
import org.springframework.stereotype.Controller;
|
| 22286 |
amit.gupta |
23 |
import org.springframework.transaction.annotation.Transactional;
|
| 21339 |
kshitij.so |
24 |
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
25 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
26 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
27 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
28 |
|
|
|
29 |
import com.eclipsesource.json.Json;
|
|
|
30 |
import com.eclipsesource.json.JsonArray;
|
|
|
31 |
import com.eclipsesource.json.JsonObject;
|
|
|
32 |
import com.eclipsesource.json.JsonValue;
|
|
|
33 |
import com.google.gson.Gson;
|
| 21356 |
kshitij.so |
34 |
import com.google.gson.reflect.TypeToken;
|
| 21643 |
ashik.ali |
35 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 21339 |
kshitij.so |
36 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
37 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21740 |
ashik.ali |
38 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
|
|
39 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
| 22289 |
amit.gupta |
40 |
import com.spice.profitmandi.common.model.UserInfo;
|
| 21643 |
ashik.ali |
41 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 22319 |
amit.gupta |
42 |
import com.spice.profitmandi.common.web.util.ResponseSender;
|
| 22289 |
amit.gupta |
43 |
import com.spice.profitmandi.dao.enumuration.dtr.RoleType;
|
| 22361 |
amit.gupta |
44 |
import com.spice.profitmandi.dao.model.UserCart;
|
| 22333 |
amit.gupta |
45 |
import com.spice.profitmandi.dao.repository.dtr.Mongo;
|
| 22361 |
amit.gupta |
46 |
import com.spice.profitmandi.dao.repository.dtr.UserAccountRepository;
|
| 22554 |
amit.gupta |
47 |
import com.spice.profitmandi.dao.service.inventory.InventoryService;
|
| 22287 |
amit.gupta |
48 |
import com.spice.profitmandi.service.pricing.PricingService;
|
| 21356 |
kshitij.so |
49 |
import com.spice.profitmandi.web.res.DealBrands;
|
| 21339 |
kshitij.so |
50 |
import com.spice.profitmandi.web.res.DealObjectResponse;
|
|
|
51 |
import com.spice.profitmandi.web.res.DealsResponse;
|
| 22328 |
amit.gupta |
52 |
import com.spice.profitmandi.web.res.FofoAvailabilityInfo;
|
|
|
53 |
import com.spice.profitmandi.web.res.FofoCatalogResponse;
|
| 21339 |
kshitij.so |
54 |
|
|
|
55 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
56 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
57 |
import io.swagger.annotations.ApiOperation;
|
|
|
58 |
|
|
|
59 |
@Controller
|
| 22319 |
amit.gupta |
60 |
@Transactional(rollbackFor = Throwable.class)
|
| 21339 |
kshitij.so |
61 |
public class DealsController {
|
|
|
62 |
|
| 22319 |
amit.gupta |
63 |
private static final Logger logger = LoggerFactory.getLogger(DealsController.class);
|
| 21339 |
kshitij.so |
64 |
|
|
|
65 |
@Value("${python.api.host}")
|
|
|
66 |
private String host;
|
|
|
67 |
@Value("${python.api.port}")
|
|
|
68 |
private int port;
|
| 22319 |
amit.gupta |
69 |
|
|
|
70 |
@Autowired
|
|
|
71 |
private PricingService pricingService;
|
| 22273 |
amit.gupta |
72 |
|
|
|
73 |
@Autowired
|
| 22333 |
amit.gupta |
74 |
private Mongo mongoClient;
|
|
|
75 |
|
|
|
76 |
@Autowired
|
| 22361 |
amit.gupta |
77 |
private UserAccountRepository userAccountRepository;
|
|
|
78 |
|
|
|
79 |
@Autowired
|
| 22319 |
amit.gupta |
80 |
ResponseSender<?> responseSender;
|
| 22336 |
amit.gupta |
81 |
|
| 22554 |
amit.gupta |
82 |
@Autowired
|
|
|
83 |
InventoryService inventoryService;
|
| 22401 |
amit.gupta |
84 |
|
| 22554 |
amit.gupta |
85 |
|
| 22336 |
amit.gupta |
86 |
List<String> filterableParams = Arrays.asList("brand");
|
| 21339 |
kshitij.so |
87 |
|
| 22319 |
amit.gupta |
88 |
@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21339 |
kshitij.so |
89 |
@ApiImplicitParams({
|
| 22319 |
amit.gupta |
90 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 21339 |
kshitij.so |
91 |
@ApiOperation(value = "Get deals")
|
| 22319 |
amit.gupta |
92 |
public ResponseEntity<?> getDeals(HttpServletRequest request, @RequestParam(value = "categoryId") String categoryId,
|
|
|
93 |
@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
|
|
|
94 |
@RequestParam(value = "sort", required = false) String sort,
|
|
|
95 |
@RequestParam(value = "direction", required = false) String direction,
|
|
|
96 |
@RequestParam(value = "filterData", required = false) String filterData) throws Throwable {
|
|
|
97 |
logger.info("Request " + request.getParameterMap());
|
| 21339 |
kshitij.so |
98 |
String response = null;
|
| 22319 |
amit.gupta |
99 |
int userId = (int) request.getAttribute("userId");
|
|
|
100 |
UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
|
|
|
101 |
// TODO: move to properties
|
|
|
102 |
String uri = "/deals/" + userId;
|
|
|
103 |
RestClient rc = new RestClient(SchemeType.HTTP, host, port);
|
| 21339 |
kshitij.so |
104 |
Map<String, String> params = new HashMap<>();
|
|
|
105 |
params.put("offset", offset);
|
|
|
106 |
params.put("limit", limit);
|
|
|
107 |
params.put("categoryId", categoryId);
|
|
|
108 |
params.put("direction", direction);
|
|
|
109 |
params.put("sort", sort);
|
|
|
110 |
params.put("filterData", filterData);
|
| 22272 |
amit.gupta |
111 |
params.put("source", "deals");
|
| 22319 |
amit.gupta |
112 |
if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
|
| 22289 |
amit.gupta |
113 |
params.put("tag_ids", getCommaSeparateTags(userId));
|
|
|
114 |
}
|
| 21356 |
kshitij.so |
115 |
List<Object> responseObject = new ArrayList<>();
|
| 21339 |
kshitij.so |
116 |
try {
|
|
|
117 |
response = rc.get(uri, params);
|
|
|
118 |
} catch (Exception | ProfitMandiBusinessException e) {
|
| 22319 |
amit.gupta |
119 |
logger.error("Unable to get deals", e);
|
|
|
120 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
121 |
request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(),
|
|
|
122 |
HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
|
|
|
123 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
| 21339 |
kshitij.so |
124 |
}
|
|
|
125 |
JsonArray result_json = Json.parse(response).asArray();
|
| 22319 |
amit.gupta |
126 |
for (JsonValue j : result_json) {
|
|
|
127 |
logger.info("res " + j.asArray());
|
| 21339 |
kshitij.so |
128 |
List<Object> innerObject = new ArrayList<>();
|
| 22319 |
amit.gupta |
129 |
for (JsonValue jsonObject : j.asArray()) {
|
| 21356 |
kshitij.so |
130 |
innerObject.add(toDealObject(jsonObject.asObject()));
|
| 21339 |
kshitij.so |
131 |
}
|
| 22319 |
amit.gupta |
132 |
if (innerObject.size() > 0) {
|
| 21339 |
kshitij.so |
133 |
responseObject.add(innerObject);
|
|
|
134 |
}
|
|
|
135 |
}
|
| 22319 |
amit.gupta |
136 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
137 |
request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS,
|
|
|
138 |
responseObject);
|
|
|
139 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
|
| 21339 |
kshitij.so |
140 |
}
|
|
|
141 |
|
| 22319 |
amit.gupta |
142 |
private String getCommaSeparateTags(int userId) throws Throwable {
|
| 22361 |
amit.gupta |
143 |
UserCart uc = userAccountRepository.getUserCart(userId);
|
|
|
144 |
List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
|
| 22287 |
amit.gupta |
145 |
List<String> strTagIds = new ArrayList<>();
|
|
|
146 |
for (Integer tagId : tagIds) {
|
|
|
147 |
strTagIds.add(String.valueOf(tagId));
|
| 22273 |
amit.gupta |
148 |
}
|
| 22287 |
amit.gupta |
149 |
return String.join(",", strTagIds);
|
| 22273 |
amit.gupta |
150 |
}
|
|
|
151 |
|
| 22319 |
amit.gupta |
152 |
@ApiImplicitParams({
|
|
|
153 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
|
|
154 |
@RequestMapping(value = "/fofo", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
155 |
public ResponseEntity<?> getFofo(HttpServletRequest request, @RequestParam(value = "categoryId") String categoryId,
|
|
|
156 |
@RequestParam(value = "offset") String offset, @RequestParam(value = "limit") String limit,
|
| 22336 |
amit.gupta |
157 |
@RequestParam(value = "sort", required = false) String sort, @RequestParam(value = "brand", required = false) String brand) throws Throwable {
|
| 22328 |
amit.gupta |
158 |
List<FofoCatalogResponse> dealResponse = new ArrayList<>();
|
| 22319 |
amit.gupta |
159 |
UserInfo userInfo = (UserInfo) request.getAttribute("userInfo");
|
|
|
160 |
if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
|
| 22361 |
amit.gupta |
161 |
UserCart uc = userAccountRepository.getUserCart(userInfo.getUserId());
|
|
|
162 |
List<Integer> tagIds = pricingService.getTagsIdsByRetailerId(uc.getUserId());
|
| 22319 |
amit.gupta |
163 |
RestClient rc = new RestClient(SchemeType.HTTP, "dtr", 8984);
|
|
|
164 |
Map<String, String> params = new HashMap<>();
|
| 22336 |
amit.gupta |
165 |
List<String> mandatoryQ = new ArrayList<>();
|
|
|
166 |
if(brand != null) {
|
| 22347 |
amit.gupta |
167 |
|
| 22348 |
amit.gupta |
168 |
mandatoryQ.add(String.format("+{!parent which=\"brand_s=%s\"} tagId_i:(%s)", brand, StringUtils.join(tagIds, " ")));
|
| 22347 |
amit.gupta |
169 |
} else {
|
|
|
170 |
mandatoryQ.add(String.format("+{!parent which=\"id:catalog*\"} tagId_i:(%s)", StringUtils.join(tagIds, " ")));
|
| 22336 |
amit.gupta |
171 |
}
|
| 22340 |
amit.gupta |
172 |
params.put("q", StringUtils.join(mandatoryQ," "));
|
| 22319 |
amit.gupta |
173 |
params.put("fl", "*, [child parentFilter=id:catalog*]");
|
|
|
174 |
params.put("sort", "rank_i asc");
|
|
|
175 |
params.put("start", String.valueOf(offset));
|
|
|
176 |
params.put("rows", String.valueOf(limit));
|
|
|
177 |
params.put("wt", "json");
|
| 22330 |
amit.gupta |
178 |
logger.info(rc.getUrl());
|
| 22319 |
amit.gupta |
179 |
String response =rc.get("solr/demo/select", params);
|
|
|
180 |
JSONObject solrResponseJSONObj = new JSONObject(response).getJSONObject("response");
|
|
|
181 |
JSONArray docs = solrResponseJSONObj.getJSONArray("docs");
|
|
|
182 |
for(int i=0; i < docs.length(); i++) {
|
| 22328 |
amit.gupta |
183 |
Map<Integer, FofoAvailabilityInfo> fofoAvailabilityInfoMap = new HashMap<>();
|
| 22319 |
amit.gupta |
184 |
JSONObject doc = docs.getJSONObject(i);
|
| 22328 |
amit.gupta |
185 |
FofoCatalogResponse ffdr = new FofoCatalogResponse();
|
| 22319 |
amit.gupta |
186 |
ffdr.setCatalogId(doc.getInt("catalogId_i"));
|
|
|
187 |
ffdr.setImageUrl(doc.getString("imageUrl_s"));
|
|
|
188 |
ffdr.setTitle(doc.getString("title_s"));
|
| 22333 |
amit.gupta |
189 |
ffdr.setBrand(doc.getString("brand_s"));
|
| 22323 |
amit.gupta |
190 |
for(int j=0; j< doc.getJSONArray("_childDocuments_").length(); j++) {
|
|
|
191 |
JSONObject childItem = doc.getJSONArray("_childDocuments_").getJSONObject(j);
|
| 22319 |
amit.gupta |
192 |
int itemId = childItem.getInt("itemId_i");
|
|
|
193 |
float sellingPrice = (float)childItem.getDouble("sellingPrice_f");
|
| 22328 |
amit.gupta |
194 |
if(fofoAvailabilityInfoMap.containsKey(itemId)) {
|
|
|
195 |
if(fofoAvailabilityInfoMap.get(itemId).getSellingPrice() > sellingPrice) {
|
|
|
196 |
fofoAvailabilityInfoMap.get(itemId).setSellingPrice(sellingPrice);
|
|
|
197 |
fofoAvailabilityInfoMap.get(itemId).setMop((float)childItem.getDouble("mop_f"));
|
| 22319 |
amit.gupta |
198 |
}
|
|
|
199 |
} else {
|
| 22328 |
amit.gupta |
200 |
FofoAvailabilityInfo fdi = new FofoAvailabilityInfo();
|
| 22319 |
amit.gupta |
201 |
fdi.setSellingPrice((float)childItem.getDouble("sellingPrice_f"));
|
|
|
202 |
fdi.setMop((float)childItem.getDouble("mop_f"));
|
| 22324 |
amit.gupta |
203 |
fdi.setColor(childItem.has("color_s")?childItem.getString("color_s"): "");
|
| 22319 |
amit.gupta |
204 |
fdi.setTagId(childItem.getInt("tagId_i"));
|
| 22328 |
amit.gupta |
205 |
fdi.setItem_id(itemId);
|
| 22560 |
amit.gupta |
206 |
fdi.setAvailability(inventoryService.getCachedItemAvailabilityCacheByItemId(itemId).getTotalAvailability());
|
| 22328 |
amit.gupta |
207 |
fdi.setQuantityStep(1);
|
|
|
208 |
fdi.setMinBuyQuantity(1);
|
| 22561 |
amit.gupta |
209 |
fdi.setMaxQuantity(Math.min(fdi.getAvailability(), 100));
|
| 22328 |
amit.gupta |
210 |
fofoAvailabilityInfoMap.put(itemId, fdi);
|
| 22319 |
amit.gupta |
211 |
}
|
|
|
212 |
}
|
| 22328 |
amit.gupta |
213 |
ffdr.setItems(new ArrayList<FofoAvailabilityInfo>(fofoAvailabilityInfoMap.values()));
|
| 22319 |
amit.gupta |
214 |
dealResponse.add(ffdr);
|
|
|
215 |
}
|
|
|
216 |
|
|
|
217 |
} else {
|
|
|
218 |
return responseSender.badRequest(new ProfitMandiBusinessException("Retailer id", userInfo.getUserId(), "NOT_FOFO_RETAILER"));
|
|
|
219 |
}
|
|
|
220 |
return responseSender.ok(dealResponse);
|
|
|
221 |
}
|
| 22273 |
amit.gupta |
222 |
|
| 22319 |
amit.gupta |
223 |
@RequestMapping(value = "/online-deals", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 22272 |
amit.gupta |
224 |
@ApiImplicitParams({
|
| 22319 |
amit.gupta |
225 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 22272 |
amit.gupta |
226 |
@ApiOperation(value = "Get online deals")
|
| 22319 |
amit.gupta |
227 |
public ResponseEntity<?> getOnlineDeals(HttpServletRequest request,
|
|
|
228 |
@RequestParam(value = "categoryId") String categoryId, @RequestParam(value = "offset") String offset,
|
|
|
229 |
@RequestParam(value = "limit") String limit, @RequestParam(value = "sort", required = false) String sort,
|
|
|
230 |
@RequestParam(value = "direction", required = false) String direction,
|
|
|
231 |
@RequestParam(value = "filterData", required = false) String filterData) throws Throwable {
|
|
|
232 |
logger.info("Request " + request.getParameterMap());
|
| 22272 |
amit.gupta |
233 |
String response = null;
|
| 22319 |
amit.gupta |
234 |
int userId = (int) request.getAttribute("userId");
|
| 22289 |
amit.gupta |
235 |
|
| 22319 |
amit.gupta |
236 |
String uri = "/deals/" + userId;
|
|
|
237 |
RestClient rc = new RestClient(SchemeType.HTTP, host, port);
|
| 22272 |
amit.gupta |
238 |
Map<String, String> params = new HashMap<>();
|
|
|
239 |
params.put("offset", offset);
|
|
|
240 |
params.put("limit", limit);
|
|
|
241 |
params.put("categoryId", categoryId);
|
|
|
242 |
params.put("direction", direction);
|
|
|
243 |
params.put("sort", sort);
|
|
|
244 |
params.put("source", "online");
|
|
|
245 |
params.put("filterData", filterData);
|
| 22357 |
amit.gupta |
246 |
/*if (userInfo.getRoleNames().contains(RoleType.FOFO.toString())) {
|
| 22289 |
amit.gupta |
247 |
params.put("tag_ids", getCommaSeparateTags(userId));
|
| 22357 |
amit.gupta |
248 |
}*/
|
| 22272 |
amit.gupta |
249 |
List<Object> responseObject = new ArrayList<>();
|
|
|
250 |
try {
|
|
|
251 |
response = rc.get(uri, params);
|
|
|
252 |
} catch (Exception | ProfitMandiBusinessException e) {
|
| 22319 |
amit.gupta |
253 |
logger.error("Unable to get deals", e);
|
|
|
254 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
255 |
request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(),
|
|
|
256 |
HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, responseObject);
|
|
|
257 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
| 22272 |
amit.gupta |
258 |
}
|
|
|
259 |
JsonArray result_json = Json.parse(response).asArray();
|
| 22319 |
amit.gupta |
260 |
for (JsonValue j : result_json) {
|
|
|
261 |
logger.info("res " + j.asArray());
|
| 22272 |
amit.gupta |
262 |
List<Object> innerObject = new ArrayList<>();
|
| 22319 |
amit.gupta |
263 |
for (JsonValue jsonObject : j.asArray()) {
|
| 22272 |
amit.gupta |
264 |
innerObject.add(toDealObject(jsonObject.asObject()));
|
|
|
265 |
}
|
| 22319 |
amit.gupta |
266 |
if (innerObject.size() > 0) {
|
| 22272 |
amit.gupta |
267 |
responseObject.add(innerObject);
|
|
|
268 |
}
|
|
|
269 |
}
|
| 22319 |
amit.gupta |
270 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
271 |
request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS,
|
|
|
272 |
responseObject);
|
|
|
273 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
|
| 22272 |
amit.gupta |
274 |
}
|
|
|
275 |
|
| 22319 |
amit.gupta |
276 |
/*
|
|
|
277 |
* @RequestMapping(value = "/direct-deals",
|
|
|
278 |
* method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
279 |
*
|
|
|
280 |
* @ApiImplicitParams({
|
|
|
281 |
*
|
|
|
282 |
* @ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required =
|
|
|
283 |
* true, dataType = "string", paramType = "header") }) public
|
|
|
284 |
* ResponseEntity<?> getDirectDeals(HttpServletRequest
|
|
|
285 |
* request, @RequestParam(value="categoryId") String
|
|
|
286 |
* categoryId,@RequestParam(value="offset") String offset,
|
|
|
287 |
*
|
|
|
288 |
* @RequestParam(value="limit") String limit, @RequestParam(value="sort",
|
|
|
289 |
* required=false) String sort, @RequestParam(value="direction",
|
|
|
290 |
* required=false) String direction,
|
|
|
291 |
*
|
|
|
292 |
* @RequestParam(value="filterData", required=false) String filterData ){
|
|
|
293 |
*
|
|
|
294 |
* return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK); }
|
|
|
295 |
*/
|
|
|
296 |
|
|
|
297 |
private Object toDealObject(JsonObject jsonObject) {
|
|
|
298 |
if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1) {
|
| 21339 |
kshitij.so |
299 |
return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
|
|
|
300 |
}
|
|
|
301 |
return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
|
|
|
302 |
}
|
| 22319 |
amit.gupta |
303 |
|
|
|
304 |
@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21356 |
kshitij.so |
305 |
@ApiImplicitParams({
|
| 22319 |
amit.gupta |
306 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 21356 |
kshitij.so |
307 |
@ApiOperation(value = "Get brand list and count for category")
|
| 22319 |
amit.gupta |
308 |
public ResponseEntity<?> getBrands(HttpServletRequest request,
|
|
|
309 |
@RequestParam(value = "category_id") String category_id) {
|
|
|
310 |
logger.info("Request " + request.getParameterMap());
|
| 21356 |
kshitij.so |
311 |
String response = null;
|
| 22319 |
amit.gupta |
312 |
// TODO: move to properties
|
| 21356 |
kshitij.so |
313 |
String uri = ProfitMandiConstants.URL_BRANDS;
|
| 22319 |
amit.gupta |
314 |
RestClient rc = new RestClient(SchemeType.HTTP, host, port);
|
| 21356 |
kshitij.so |
315 |
Map<String, String> params = new HashMap<>();
|
|
|
316 |
params.put("category_id", category_id);
|
| 21358 |
kshitij.so |
317 |
List<DealBrands> dealBrandsResponse = null;
|
| 21356 |
kshitij.so |
318 |
try {
|
|
|
319 |
response = rc.get(uri, params);
|
|
|
320 |
} catch (Exception | ProfitMandiBusinessException e) {
|
| 22319 |
amit.gupta |
321 |
logger.error("Unable to get deals", e);
|
|
|
322 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
323 |
request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(),
|
|
|
324 |
HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealBrandsResponse);
|
|
|
325 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
| 21356 |
kshitij.so |
326 |
}
|
| 22319 |
amit.gupta |
327 |
dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>() {
|
|
|
328 |
}.getType());
|
|
|
329 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
330 |
request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS,
|
|
|
331 |
dealBrandsResponse);
|
|
|
332 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
|
| 21356 |
kshitij.so |
333 |
}
|
| 22319 |
amit.gupta |
334 |
|
|
|
335 |
@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 21445 |
kshitij.so |
336 |
@ApiImplicitParams({
|
| 22319 |
amit.gupta |
337 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 21445 |
kshitij.so |
338 |
@ApiOperation(value = "Get unit deal object")
|
| 22319 |
amit.gupta |
339 |
public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value = "id") long id) {
|
| 21445 |
kshitij.so |
340 |
String response = null;
|
| 22319 |
amit.gupta |
341 |
// TODO: move to properties
|
|
|
342 |
String uri = "getDealById/" + id;
|
|
|
343 |
System.out.println("Unit deal " + uri);
|
|
|
344 |
RestClient rc = new RestClient(SchemeType.HTTP, host, port);
|
| 21445 |
kshitij.so |
345 |
Map<String, String> params = new HashMap<>();
|
|
|
346 |
DealsResponse dealsResponse = null;
|
|
|
347 |
try {
|
|
|
348 |
response = rc.get(uri, params);
|
|
|
349 |
} catch (Exception | ProfitMandiBusinessException e) {
|
| 22319 |
amit.gupta |
350 |
logger.error("Unable to get deals", e);
|
|
|
351 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
352 |
request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(),
|
|
|
353 |
HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, dealsResponse);
|
|
|
354 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.INTERNAL_SERVER_ERROR);
|
| 21445 |
kshitij.so |
355 |
}
|
|
|
356 |
JsonObject result_json = Json.parse(response).asObject();
|
| 22319 |
amit.gupta |
357 |
if (!result_json.isEmpty()) {
|
| 21445 |
kshitij.so |
358 |
dealsResponse = new Gson().fromJson(response, DealsResponse.class);
|
|
|
359 |
}
|
| 22319 |
amit.gupta |
360 |
final ProfitMandiResponse<?> profitMandiResponse = new ProfitMandiResponse<>(LocalDateTime.now(),
|
|
|
361 |
request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS,
|
|
|
362 |
dealsResponse);
|
|
|
363 |
return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
|
| 21445 |
kshitij.so |
364 |
}
|
| 22333 |
amit.gupta |
365 |
|
|
|
366 |
@RequestMapping(value = "/fofo/brands", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
367 |
public ResponseEntity<?> getBrandsToDisplay() {
|
|
|
368 |
return new ResponseEntity<>(mongoClient.getBrandsToDisplay(), HttpStatus.OK);
|
|
|
369 |
}
|
| 22401 |
amit.gupta |
370 |
|
| 22446 |
amit.gupta |
371 |
@RequestMapping(value = "/banners/{bannerType}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
| 22448 |
amit.gupta |
372 |
public ResponseEntity<?> getBanners(@PathVariable String bannerType) {
|
| 22447 |
amit.gupta |
373 |
return new ResponseEntity<>(mongoClient.getBannersByType(bannerType), HttpStatus.OK);
|
| 22446 |
amit.gupta |
374 |
}
|
|
|
375 |
|
| 22406 |
amit.gupta |
376 |
@ApiImplicitParams({
|
|
|
377 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", required = true, dataType = "string", paramType = "header") })
|
| 22401 |
amit.gupta |
378 |
@RequestMapping(value = "/deals/skus/{skus}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
379 |
public ResponseEntity<?> getDealsBySkus(@PathVariable String skus) throws Throwable {
|
|
|
380 |
StringBuffer sb = new StringBuffer("/getDealsForNotification/");
|
|
|
381 |
String uri = sb.append(skus).toString();
|
|
|
382 |
RestClient rc = new RestClient(SchemeType.HTTP, host, port);
|
|
|
383 |
String response = rc.get(uri, new HashMap<>());
|
| 22406 |
amit.gupta |
384 |
JsonArray result_json = Json.parse(response).asArray();
|
| 22407 |
amit.gupta |
385 |
List<Object> responseObject = new ArrayList<>();
|
|
|
386 |
for (JsonValue j : result_json) {
|
|
|
387 |
logger.info("res " + j.asArray());
|
|
|
388 |
List<Object> innerObject = new ArrayList<>();
|
|
|
389 |
for (JsonValue jsonObject : j.asArray()) {
|
|
|
390 |
innerObject.add(toDealObject(jsonObject.asObject()));
|
|
|
391 |
}
|
|
|
392 |
if (innerObject.size() > 0) {
|
|
|
393 |
responseObject.add(innerObject);
|
|
|
394 |
}
|
|
|
395 |
}
|
| 22408 |
amit.gupta |
396 |
return responseSender.ok(responseObject);
|
| 22401 |
amit.gupta |
397 |
}
|
| 21339 |
kshitij.so |
398 |
|
|
|
399 |
}
|