Subversion Repositories SmartDukaan

Rev

Rev 21302 | Rev 21315 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
21292 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
21309 ashik.ali 4
import java.util.Map;
21292 ashik.ali 5
 
6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.HttpStatus;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
21309 ashik.ali 14
import org.springframework.web.bind.annotation.RequestBody;
21292 ashik.ali 15
import org.springframework.web.bind.annotation.RequestMapping;
16
import org.springframework.web.bind.annotation.RequestMethod;
17
import org.springframework.web.bind.annotation.RequestParam;
18
 
19
import com.spice.profitmandi.common.ResponseCodeHolder;
20
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21
import com.spice.profitmandi.common.model.ProfitMandiConstants;
22
import com.spice.profitmandi.dao.entity.Retailer;
23
import com.spice.profitmandi.dao.repository.RetailerRepository;
24
import com.spice.profitmandi.web.model.ProfitMandiResponse;
25
import com.spice.profitmandi.web.model.Response;
26
import com.spice.profitmandi.web.model.ResponseStatus;
27
 
21302 ashik.ali 28
import io.swagger.annotations.ApiImplicitParam;
29
import io.swagger.annotations.ApiImplicitParams;
30
import io.swagger.annotations.ApiOperation;
31
 
21292 ashik.ali 32
@Controller
33
public class RetailerController {
34
 
35
	private static final Logger LOGGER=LoggerFactory.getLogger(RetailerController.class);
36
 
37
	@Autowired
38
	RetailerRepository retailerRepository;
39
 
21302 ashik.ali 40
	@ApiImplicitParams({
41
		@ApiImplicitParam(name = "Auth-Token", value = "Auth-Token", 
42
				required = true, dataType = "string", paramType = "header")
43
	})
21309 ashik.ali 44
 
21302 ashik.ali 45
	@ApiOperation(value = "Create Retailer")
46
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER, method=RequestMethod.POST)
21292 ashik.ali 47
	public ResponseEntity<?> createRetailer(HttpServletRequest request){
48
		LOGGER.info("requested url : "+request.getRequestURL().toString());
49
		final Retailer retailer = (Retailer)request.getAttribute(ProfitMandiConstants.RETAILER);
21302 ashik.ali 50
		request.removeAttribute(ProfitMandiConstants.RETAILER);
21292 ashik.ali 51
		try {
52
			retailer.setCreateTimestamp(LocalDateTime.now());
53
			retailer.setUpdateTimestamp(LocalDateTime.now());
54
			retailerRepository.persist(retailer);
21309 ashik.ali 55
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("RTLR_OK_1000"));
21292 ashik.ali 56
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
57
		}catch (ProfitMandiBusinessException pmbe) {
58
			LOGGER.error("ProfitMandi error: ", pmbe);
59
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
60
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
61
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
62
		}catch (Exception e) {
63
			LOGGER.error("Internal Server Error : ",e);
64
			final Response response=new Response("","","", e.getMessage());
65
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
66
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
67
		}
68
	}
69
 
70
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ALL,method=RequestMethod.GET)
71
	public ResponseEntity<?> getAll(HttpServletRequest request){
72
		LOGGER.info("requested url : "+request.getRequestURL().toString());
73
		try {
74
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectAll());
75
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
76
		}catch (Exception e) {
77
			LOGGER.error("Internal Server Error : ",e);
78
			final Response response=new Response("","","", e.getMessage());
79
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
80
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
81
		}
82
	}
83
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID, method=RequestMethod.GET)
21309 ashik.ali 84
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") long id){
21292 ashik.ali 85
		LOGGER.info("requested url : "+request.getRequestURL().toString());
86
		try {
87
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectById(id));
88
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
89
		}catch (ProfitMandiBusinessException pmbe) {
90
			LOGGER.error("ProfitMandi error: ", pmbe);
91
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
92
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
93
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
94
		}catch (Exception e) {
95
			LOGGER.error("Internal Server Error : ",e);
96
			final Response response=new Response("","","", e.getMessage());
97
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
98
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
99
		}
100
	}
101
 
102
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_NAME, method=RequestMethod.GET)
103
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
104
		LOGGER.info("requested url : "+request.getRequestURL().toString());
105
		try {
106
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, retailerRepository.selectByName(name));
107
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
108
		}catch (ProfitMandiBusinessException pmbe) {
109
			LOGGER.error("ProfitMandi error: ", pmbe);
110
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
111
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
112
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
113
		}catch (Exception e) {
114
			LOGGER.error("Internal Server Error : ",e);
115
			final Response response=new Response("","","", e.getMessage());
116
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
117
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
118
		}
119
	}
120
 
121
 
122
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_ID,method=RequestMethod.DELETE)
21309 ashik.ali 123
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") long id){
21292 ashik.ali 124
		LOGGER.info("requested url : "+request.getRequestURL().toString());
125
		try {
126
			retailerRepository.deleteById(id);
127
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
128
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
129
		}catch (ProfitMandiBusinessException pmbe) {
130
			LOGGER.error("ProfitMandi error: ", pmbe);
131
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
132
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
133
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
134
		}catch (Exception e) {
135
			LOGGER.error("Internal Server error : ",e);
136
			final Response response=new Response("","","", e.getMessage());
137
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
138
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
139
		}
140
	}
141
 
21302 ashik.ali 142
	@RequestMapping(value = ProfitMandiConstants.URL_RETAILER_SHOP_REMOVE, method=RequestMethod.DELETE)
21309 ashik.ali 143
	public ResponseEntity<?> removeShop(HttpServletRequest request, @RequestParam(name = "shopId") long shopId, @RequestParam(name = "retailerId") long retailerId){
21292 ashik.ali 144
		LOGGER.info("requested url : "+request.getRequestURL().toString());
145
		try {
146
			retailerRepository.removeShop(shopId, retailerId);
147
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
148
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
149
		}catch (ProfitMandiBusinessException pmbe) {
150
			LOGGER.error("ProfitMandi error: ", pmbe);
151
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
152
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
153
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
154
		}catch (Exception e) {
155
			LOGGER.error("Internal Server error : ",e);
156
			final Response response=new Response("","","", e.getMessage());
157
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
158
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
159
		}
160
	}
161
 
162
 
163
}