| 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 |
|
| 21348 |
kshitij.so |
11 |
import org.apache.thrift.TException;
|
|
|
12 |
import org.apache.thrift.transport.TTransportException;
|
| 21339 |
kshitij.so |
13 |
import org.slf4j.Logger;
|
|
|
14 |
import org.slf4j.LoggerFactory;
|
|
|
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;
|
|
|
30 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
31 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21348 |
kshitij.so |
32 |
import com.spice.profitmandi.thrift.clients.CatalogClient;
|
| 21339 |
kshitij.so |
33 |
import com.spice.profitmandi.web.client.RestClient;
|
|
|
34 |
import com.spice.profitmandi.web.enumuration.SchemeType;
|
|
|
35 |
import com.spice.profitmandi.web.model.ProfitMandiResponse;
|
|
|
36 |
import com.spice.profitmandi.web.model.ResponseStatus;
|
|
|
37 |
import com.spice.profitmandi.web.res.DealObjectResponse;
|
|
|
38 |
import com.spice.profitmandi.web.res.DealsResponse;
|
|
|
39 |
|
| 21348 |
kshitij.so |
40 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
|
|
41 |
import in.shop2020.model.v1.catalog.Item;
|
| 21339 |
kshitij.so |
42 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
43 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
44 |
import io.swagger.annotations.ApiOperation;
|
|
|
45 |
|
|
|
46 |
@Controller
|
|
|
47 |
public class DealsController {
|
|
|
48 |
|
|
|
49 |
private static final Logger logger=LoggerFactory.getLogger(DealsController.class);
|
|
|
50 |
|
|
|
51 |
@Value("${python.api.host}")
|
|
|
52 |
private String host;
|
|
|
53 |
@Value("${python.api.port}")
|
|
|
54 |
private int port;
|
|
|
55 |
|
|
|
56 |
@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
57 |
@ApiImplicitParams({
|
|
|
58 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
59 |
required = true, dataType = "string", paramType = "header")
|
|
|
60 |
})
|
|
|
61 |
@ApiOperation(value = "Get deals")
|
|
|
62 |
public ResponseEntity<?> getDeals(HttpServletRequest request, @PathVariable(value="userId") long userId, @RequestParam(value="categoryId") String categoryId,@RequestParam(value="offset") String offset, @RequestParam(value="limit") String limit, @RequestParam(value="sort", required=false) String sort, @RequestParam(value="direction", required=false) String direction, @RequestParam(value="filterData", required=false) String filterData ){
|
|
|
63 |
logger.info("Request "+request.getParameterMap());
|
|
|
64 |
String response = null;
|
|
|
65 |
String uri = "/deals/"+userId;
|
|
|
66 |
RestClient rc = new RestClient(SchemeType.HTTP, host , port);
|
|
|
67 |
Map<String, String> params = new HashMap<>();
|
|
|
68 |
params.put("offset", offset);
|
|
|
69 |
params.put("limit", limit);
|
|
|
70 |
params.put("categoryId", categoryId);
|
|
|
71 |
params.put("direction", direction);
|
|
|
72 |
params.put("sort", sort);
|
|
|
73 |
params.put("filterData", filterData);
|
|
|
74 |
|
|
|
75 |
try {
|
|
|
76 |
response = rc.get(uri, params);
|
|
|
77 |
} catch (Exception | ProfitMandiBusinessException e) {
|
|
|
78 |
logger.error("Unable to get deals",e);
|
|
|
79 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, new DealsResponse());
|
|
|
80 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
81 |
}
|
|
|
82 |
JsonArray result_json = Json.parse(response).asArray();
|
|
|
83 |
List<Object> responseObject = new ArrayList<>();
|
|
|
84 |
for (JsonValue j : result_json ){
|
|
|
85 |
logger.info("res "+j.asArray());
|
|
|
86 |
List<Object> innerObject = new ArrayList<>();
|
|
|
87 |
for (JsonValue jsonObject : j.asArray()){
|
|
|
88 |
innerObject.add(toObject(jsonObject.asObject()));
|
|
|
89 |
}
|
|
|
90 |
if (innerObject.size() > 0){
|
|
|
91 |
responseObject.add(innerObject);
|
|
|
92 |
}
|
|
|
93 |
}
|
| 21348 |
kshitij.so |
94 |
try {
|
|
|
95 |
CatalogClient cc = new CatalogClient();
|
|
|
96 |
Item item = cc.getClient().getItem(26328);
|
|
|
97 |
logger.info("Thrift item "+item.toString());
|
|
|
98 |
} catch (CatalogServiceException | TException e) {
|
|
|
99 |
// TODO Auto-generated catch block
|
|
|
100 |
e.printStackTrace();
|
|
|
101 |
}
|
|
|
102 |
|
| 21339 |
kshitij.so |
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 |
|
|
|
107 |
private Object toObject(JsonObject jsonObject){
|
|
|
108 |
if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1){
|
|
|
109 |
return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
|
|
|
110 |
}
|
|
|
111 |
return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
|
|
|
115 |
}
|