Subversion Repositories SmartDukaan

Rev

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