Subversion Repositories SmartDukaan

Rev

Rev 21357 | Rev 21445 | 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;
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;
21339 kshitij.so 29
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
30
import com.spice.profitmandi.common.model.ProfitMandiConstants;
31
import com.spice.profitmandi.web.client.RestClient;
32
import com.spice.profitmandi.web.enumuration.SchemeType;
33
import com.spice.profitmandi.web.model.ProfitMandiResponse;
34
import com.spice.profitmandi.web.model.ResponseStatus;
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")
59
	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 ){
60
		logger.info("Request "+request.getParameterMap());
61
		String response = null;
62
		String uri = "/deals/"+userId;
63
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
64
		Map<String, String> params = new HashMap<>();
65
		params.put("offset", offset);
66
		params.put("limit", limit);
67
		params.put("categoryId", categoryId);
68
		params.put("direction", direction);
69
		params.put("sort", sort);
70
		params.put("filterData", filterData);
21356 kshitij.so 71
		List<Object> responseObject = new ArrayList<>();
21339 kshitij.so 72
		try {
73
			response = rc.get(uri, params);
74
		} catch (Exception | ProfitMandiBusinessException e) {
75
			logger.error("Unable to get deals",e);
21356 kshitij.so 76
			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 77
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
78
		}
79
		JsonArray result_json = Json.parse(response).asArray();
80
		for (JsonValue j : result_json ){
81
			logger.info("res "+j.asArray());
82
			List<Object> innerObject = new ArrayList<>();
83
			for (JsonValue jsonObject : j.asArray()){
21356 kshitij.so 84
				innerObject.add(toDealObject(jsonObject.asObject()));
21339 kshitij.so 85
			}
86
			if (innerObject.size() > 0){
87
				responseObject.add(innerObject);
88
			}
89
		}
90
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, responseObject);
91
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
92
	}
93
 
21356 kshitij.so 94
	private Object toDealObject(JsonObject jsonObject){
21339 kshitij.so 95
		if (jsonObject.get("dealObject") != null &&  jsonObject.get("dealObject").asInt() == 1){
96
			return new Gson().fromJson(jsonObject.toString(), DealObjectResponse.class);
97
		}
98
		return new Gson().fromJson(jsonObject.toString(), DealsResponse.class);
99
	}
21356 kshitij.so 100
 
101
 
102
	@RequestMapping(value = ProfitMandiConstants.URL_BRANDS, method=RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
103
	@ApiImplicitParams({
104
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
105
				required = true, dataType = "string", paramType = "header")
106
	})
107
	@ApiOperation(value = "Get brand list and count for category")
108
	public ResponseEntity<?> getBrands(HttpServletRequest request, @RequestParam(value="category_id") String category_id){
109
		logger.info("Request "+request.getParameterMap());
110
		String response = null;
111
		String uri = ProfitMandiConstants.URL_BRANDS;
112
		RestClient rc = new RestClient(SchemeType.HTTP, host , port);
113
		Map<String, String> params = new HashMap<>();
114
		params.put("category_id", category_id);
21358 kshitij.so 115
		List<DealBrands> dealBrandsResponse = null;
21356 kshitij.so 116
		try {
117
			response = rc.get(uri, params);
118
		} catch (Exception | ProfitMandiBusinessException e) {
119
			logger.error("Unable to get deals",e);
21358 kshitij.so 120
			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 121
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.INTERNAL_SERVER_ERROR);
122
		}
21358 kshitij.so 123
		dealBrandsResponse = new Gson().fromJson(response, new TypeToken<List<DealBrands>>(){}.getType());
21356 kshitij.so 124
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString() , HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, dealBrandsResponse);
125
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
126
	}
127
 
21339 kshitij.so 128
 
129
}