Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
21220 ashik.ali 1
package com.spice.profitmandi.web.controller;
2
 
3
import java.time.LocalDateTime;
21236 ashik.ali 4
import java.util.Map;
21220 ashik.ali 5
 
6
import javax.servlet.http.HttpServletRequest;
7
 
8
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
21278 ashik.ali 10
import org.springframework.beans.factory.annotation.Autowired;
21220 ashik.ali 11
import org.springframework.http.HttpStatus;
12
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
14
import org.springframework.web.bind.annotation.RequestMapping;
15
import org.springframework.web.bind.annotation.RequestMethod;
16
import org.springframework.web.bind.annotation.RequestParam;
17
 
18
import com.spice.profitmandi.common.ResponseCodeHolder;
21248 ashik.ali 19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
21236 ashik.ali 20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21220 ashik.ali 21
import com.spice.profitmandi.dao.entity.Api;
21236 ashik.ali 22
import com.spice.profitmandi.dao.enumuration.Method;
21220 ashik.ali 23
import com.spice.profitmandi.dao.repository.ApiRepository;
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
 
28
@Controller
29
public class ApiController {
30
 
31
	private static final Logger LOGGER=LoggerFactory.getLogger(ApiController.class);
32
 
21278 ashik.ali 33
	@Autowired
34
	ApiRepository apiRepository;
21220 ashik.ali 35
 
21236 ashik.ali 36
	@RequestMapping(value = ProfitMandiConstants.URL_API, method=RequestMethod.POST)
21220 ashik.ali 37
	public ResponseEntity<?> createApi(HttpServletRequest request){
38
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21236 ashik.ali 39
		final Api api = (Api)request.getAttribute(ProfitMandiConstants.API);
40
		request.removeAttribute(ProfitMandiConstants.API);
21220 ashik.ali 41
		try {
21236 ashik.ali 42
			api.setCreateTimestamp(LocalDateTime.now());
43
			api.setUpdateTimestamp(LocalDateTime.now());
21220 ashik.ali 44
			apiRepository.persist(api);
45
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1000"));
46
			return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
21248 ashik.ali 47
		}catch (ProfitMandiBusinessException pmbe) {
48
			LOGGER.error("ProfitMandi error: ", pmbe);
49
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
21236 ashik.ali 50
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
51
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
21220 ashik.ali 52
		}catch (Exception e) {
21236 ashik.ali 53
			LOGGER.error("Internal Server Error : ",e);
21220 ashik.ali 54
			final Response response=new Response("","","", e.getMessage());
55
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
56
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
57
		}
58
	}
21278 ashik.ali 59
 
21236 ashik.ali 60
	@RequestMapping(value = ProfitMandiConstants.URL_API_ALL,method=RequestMethod.GET)
21220 ashik.ali 61
	public ResponseEntity<?> getAll(HttpServletRequest request){
62
		LOGGER.info("requested url : "+request.getRequestURL().toString());
63
		try {
64
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectAll());
65
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
66
		}catch (Exception e) {
21236 ashik.ali 67
			LOGGER.error("Internal Server Error : ",e);
21220 ashik.ali 68
			final Response response=new Response("","","", e.getMessage());
69
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
70
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
71
		}
72
	}
21236 ashik.ali 73
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.GET)
74
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") long id){
21220 ashik.ali 75
		LOGGER.info("requested url : "+request.getRequestURL().toString());
76
		try {
77
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectById(id));
78
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 79
		}catch (ProfitMandiBusinessException pmbe) {
80
			LOGGER.error("ProfitMandi error: ", pmbe);
81
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
21220 ashik.ali 82
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
83
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
84
		}catch (Exception e) {
21236 ashik.ali 85
			LOGGER.error("Internal Server Error : ",e);
21220 ashik.ali 86
			final Response response=new Response("","","", e.getMessage());
87
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
88
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
89
		}
90
	}
91
 
21236 ashik.ali 92
	@RequestMapping(value = ProfitMandiConstants.URL_API_NAME,method=RequestMethod.GET)
21220 ashik.ali 93
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
94
		LOGGER.info("requested url : "+request.getRequestURL().toString());
95
		try {
96
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectByName(name));
97
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 98
		}catch (ProfitMandiBusinessException pmbe) {
99
			LOGGER.error("ProfitMandi error: ", pmbe);
100
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
101
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
102
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
21220 ashik.ali 103
		}catch (Exception e) {
21236 ashik.ali 104
			LOGGER.error("Internal Server Error : ",e);
21220 ashik.ali 105
			final Response response=new Response("","","", e.getMessage());
106
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
107
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
108
		}
109
	}
110
 
21236 ashik.ali 111
	@RequestMapping(value = ProfitMandiConstants.URL_API_URI,method=RequestMethod.GET)
21220 ashik.ali 112
	public ResponseEntity<?> getByUri(HttpServletRequest request, @RequestParam(name = "uri") String uri){
113
		LOGGER.info("requested url : "+request.getRequestURL().toString());
114
		try {
115
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectByUri(uri));
116
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
117
		}catch (Exception e) {
118
			LOGGER.error("Internal Server error : ",e);
119
			final Response response=new Response("","","", e.getMessage());
120
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
121
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
122
		}
123
	}
124
 
21236 ashik.ali 125
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.DELETE)
126
	public ResponseEntity<?> deleteById(HttpServletRequest request, @RequestParam(name = "id") long id){
21220 ashik.ali 127
		LOGGER.info("requested url : "+request.getRequestURL().toString());
128
		try {
129
			apiRepository.deleteById(id);
130
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
131
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 132
		}catch (ProfitMandiBusinessException pmbe) {
133
			LOGGER.error("ProfitMandi error: ", pmbe);
134
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
21220 ashik.ali 135
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
136
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
137
		}catch (Exception e) {
138
			LOGGER.error("Internal Server error : ",e);
139
			final Response response=new Response("","","", e.getMessage());
140
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
141
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
142
		}
143
	}
144
 
21236 ashik.ali 145
	@SuppressWarnings("unchecked")
146
	@RequestMapping(value = ProfitMandiConstants.URL_API,method=RequestMethod.PUT)
147
	public ResponseEntity<?> updateNameById(HttpServletRequest request){
21220 ashik.ali 148
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21236 ashik.ali 149
		final Map<String, Object> updateApiMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.UPDATE_API_MAP);
150
		request.removeAttribute(ProfitMandiConstants.UPDATE_API_MAP);
21220 ashik.ali 151
		try {
21236 ashik.ali 152
			apiRepository.updateNameById(updateApiMap.get(ProfitMandiConstants.NAME).toString(), updateApiMap.get(ProfitMandiConstants.URI).toString(),(Method)updateApiMap.get(ProfitMandiConstants.METHOD), (Long)updateApiMap.get(ProfitMandiConstants.ID));
21220 ashik.ali 153
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1002"));
154
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
21248 ashik.ali 155
		}catch (ProfitMandiBusinessException pmbe) {
156
			LOGGER.error("ProfitMandi error: ", pmbe);
157
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
21220 ashik.ali 158
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
159
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
160
		}catch (Exception e) {
161
			LOGGER.error("Internal Server Error : ",e);
162
			final Response response=new Response("","","", e.getMessage());
163
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
164
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
165
		}
166
	}
21236 ashik.ali 167
 
21220 ashik.ali 168
}