Subversion Repositories SmartDukaan

Rev

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