Subversion Repositories SmartDukaan

Rev

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