| 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;
|
|
|
13 |
import org.springframework.beans.factory.annotation.Value;
|
|
|
14 |
import org.springframework.http.HttpStatus;
|
|
|
15 |
import org.springframework.http.MediaType;
|
|
|
16 |
import org.springframework.http.ResponseEntity;
|
|
|
17 |
import org.springframework.stereotype.Controller;
|
|
|
18 |
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
19 |
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
20 |
import org.springframework.web.bind.annotation.RequestMethod;
|
|
|
21 |
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
22 |
|
|
|
23 |
import com.eclipsesource.json.Json;
|
|
|
24 |
import com.eclipsesource.json.JsonArray;
|
|
|
25 |
import com.eclipsesource.json.JsonObject;
|
|
|
26 |
import com.eclipsesource.json.JsonValue;
|
|
|
27 |
import com.google.gson.Gson;
|
| 21356 |
kshitij.so |
28 |
import com.google.gson.reflect.TypeToken;
|
| 21643 |
ashik.ali |
29 |
import com.spice.profitmandi.common.enumuration.SchemeType;
|
| 21339 |
kshitij.so |
30 |
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
|
|
|
31 |
import com.spice.profitmandi.common.model.ProfitMandiConstants;
|
| 21740 |
ashik.ali |
32 |
import com.spice.profitmandi.common.model.ProfitMandiResponse;
|
|
|
33 |
import com.spice.profitmandi.common.model.ResponseStatus;
|
| 21643 |
ashik.ali |
34 |
import com.spice.profitmandi.common.web.client.RestClient;
|
| 21356 |
kshitij.so |
35 |
import com.spice.profitmandi.web.res.DealBrands;
|
| 21339 |
kshitij.so |
36 |
import com.spice.profitmandi.web.res.DealObjectResponse;
|
|
|
37 |
import com.spice.profitmandi.web.res.DealsResponse;
|
|
|
38 |
|
|
|
39 |
import io.swagger.annotations.ApiImplicitParam;
|
|
|
40 |
import io.swagger.annotations.ApiImplicitParams;
|
|
|
41 |
import io.swagger.annotations.ApiOperation;
|
|
|
42 |
|
|
|
43 |
@Controller
|
|
|
44 |
public class DealsController {
|
|
|
45 |
|
|
|
46 |
private static final Logger logger=LoggerFactory.getLogger(DealsController.class);
|
|
|
47 |
|
|
|
48 |
@Value("${python.api.host}")
|
|
|
49 |
private String host;
|
|
|
50 |
@Value("${python.api.port}")
|
|
|
51 |
private int port;
|
|
|
52 |
|
|
|
53 |
@RequestMapping(value = ProfitMandiConstants.URL_DEALS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
54 |
@ApiImplicitParams({
|
|
|
55 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
56 |
required = true, dataType = "string", paramType = "header")
|
|
|
57 |
})
|
|
|
58 |
@ApiOperation(value = "Get deals")
|
| 21459 |
kshitij.so |
59 |
public ResponseEntity<?> getDeals(HttpServletRequest request, @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 ){
|
| 21339 |
kshitij.so |
60 |
logger.info("Request "+request.getParameterMap());
|
|
|
61 |
String response = null;
|
| 21459 |
kshitij.so |
62 |
int userId = (int)request.getAttribute("userId");
|
| 21462 |
kshitij.so |
63 |
//TODO: move to properties
|
| 21339 |
kshitij.so |
64 |
String uri = "/deals/"+userId;
|
|
|
65 |
RestClient rc = new RestClient(SchemeType.HTTP, host , port);
|
|
|
66 |
Map<String, String> params = new HashMap<>();
|
|
|
67 |
params.put("offset", offset);
|
|
|
68 |
params.put("limit", limit);
|
|
|
69 |
params.put("categoryId", categoryId);
|
|
|
70 |
params.put("direction", direction);
|
|
|
71 |
params.put("sort", sort);
|
|
|
72 |
params.put("filterData", filterData);
|
| 21356 |
kshitij.so |
73 |
List<Object> responseObject = new ArrayList<>();
|
| 21339 |
kshitij.so |
74 |
try {
|
|
|
75 |
response = rc.get(uri, params);
|
|
|
76 |
} catch (Exception | ProfitMandiBusinessException e) {
|
|
|
77 |
logger.error("Unable to get deals",e);
|
| 21356 |
kshitij.so |
78 |
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 |
79 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
80 |
}
|
|
|
81 |
JsonArray result_json = Json.parse(response).asArray();
|
|
|
82 |
for (JsonValue j : result_json ){
|
|
|
83 |
logger.info("res "+j.asArray());
|
|
|
84 |
List<Object> innerObject = new ArrayList<>();
|
|
|
85 |
for (JsonValue jsonObject : j.asArray()){
|
| 21356 |
kshitij.so |
86 |
innerObject.add(toDealObject(jsonObject.asObject()));
|
| 21339 |
kshitij.so |
87 |
}
|
|
|
88 |
if (innerObject.size() > 0){
|
|
|
89 |
responseObject.add(innerObject);
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
|
|
|
93 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
|
|
|
94 |
}
|
|
|
95 |
|
| 21356 |
kshitij.so |
96 |
private Object toDealObject(JsonObject jsonObject){
|
| 21339 |
kshitij.so |
97 |
if (jsonObject.get("dealObject") != null && jsonObject.get("dealObject").asInt() == 1){
|
|
|
98 |
return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
|
|
|
99 |
}
|
|
|
100 |
return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
|
|
|
101 |
}
|
| 21356 |
kshitij.so |
102 |
|
|
|
103 |
|
|
|
104 |
@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
105 |
@ApiImplicitParams({
|
|
|
106 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
107 |
required = true, dataType = "string", paramType = "header")
|
|
|
108 |
})
|
|
|
109 |
@ApiOperation(value = "Get brand list and count for category")
|
|
|
110 |
public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
|
|
|
111 |
logger.info("Request "+request.getParameterMap());
|
|
|
112 |
String response = null;
|
| 21462 |
kshitij.so |
113 |
//TODO: move to properties
|
| 21356 |
kshitij.so |
114 |
String uri = ProfitMandiConstants.URL_BRANDS;
|
|
|
115 |
RestClient rc = new RestClient(SchemeType.HTTP, host , port);
|
|
|
116 |
Map<String, String> params = new HashMap<>();
|
|
|
117 |
params.put("category_id", category_id);
|
| 21358 |
kshitij.so |
118 |
List<DealBrands> dealBrandsResponse = null;
|
| 21356 |
kshitij.so |
119 |
try {
|
|
|
120 |
response = rc.get(uri, params);
|
|
|
121 |
} catch (Exception | ProfitMandiBusinessException e) {
|
|
|
122 |
logger.error("Unable to get deals",e);
|
| 21358 |
kshitij.so |
123 |
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 |
124 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
125 |
}
|
| 21358 |
kshitij.so |
126 |
dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
|
| 21356 |
kshitij.so |
127 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
|
|
|
128 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
|
|
|
129 |
}
|
|
|
130 |
|
| 21445 |
kshitij.so |
131 |
@RequestMapping(value = ProfitMandiConstants.URL_UNIT_DEAL, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
|
|
|
132 |
@ApiImplicitParams({
|
|
|
133 |
@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token",
|
|
|
134 |
required = true, dataType = "string", paramType = "header")
|
|
|
135 |
})
|
|
|
136 |
@ApiOperation(value = "Get unit deal object")
|
|
|
137 |
public ResponseEntity<?> getUnitDeal(HttpServletRequest request, @PathVariable(value="id") long id){
|
|
|
138 |
String response = null;
|
| 21462 |
kshitij.so |
139 |
//TODO: move to properties
|
| 21445 |
kshitij.so |
140 |
String uri = "getDealById/"+id;
|
|
|
141 |
System.out.println("Unit deal "+uri);
|
|
|
142 |
RestClient rc = new RestClient(SchemeType.HTTP, host , port);
|
|
|
143 |
Map<String, String> params = new HashMap<>();
|
|
|
144 |
DealsResponse dealsResponse = null;
|
|
|
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, dealsResponse);
|
|
|
150 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
|
|
|
151 |
}
|
|
|
152 |
JsonObject result_json = Json.parse(response).asObject();
|
|
|
153 |
if (!result_json.isEmpty()){
|
|
|
154 |
dealsResponse = new Gson().fromJson(response, DealsResponse.class);
|
|
|
155 |
}
|
|
|
156 |
final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealsResponse);
|
|
|
157 |
return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
|
|
|
158 |
}
|
| 21339 |
kshitij.so |
159 |
|
|
|
160 |
}
|