Subversion Repositories SmartDukaan

Rev

Rev 21440 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 21440 Rev 21448
Line 1... Line 1...
1
package com.spice.profitmandi.web.controller;
1
package com.spice.profitmandi.web.controller;
2
 
2
 
3
import java.time.LocalDateTime;
-
 
4
import java.util.Map;
-
 
5
 
-
 
6
import javax.servlet.http.HttpServletRequest;
3
import javax.servlet.http.HttpServletRequest;
7
 
4
 
8
import org.slf4j.Logger;
5
import org.slf4j.Logger;
9
import org.slf4j.LoggerFactory;
6
import org.slf4j.LoggerFactory;
10
import org.springframework.beans.factory.annotation.Autowired;
7
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.http.HttpStatus;
-
 
12
import org.springframework.http.ResponseEntity;
8
import org.springframework.http.ResponseEntity;
13
import org.springframework.stereotype.Controller;
9
import org.springframework.stereotype.Controller;
14
import org.springframework.web.bind.annotation.RequestMapping;
10
import org.springframework.web.bind.annotation.RequestMapping;
15
import org.springframework.web.bind.annotation.RequestMethod;
11
import org.springframework.web.bind.annotation.RequestMethod;
16
import org.springframework.web.bind.annotation.RequestParam;
12
import org.springframework.web.bind.annotation.RequestParam;
17
 
13
 
18
import com.spice.profitmandi.common.ResponseCodeHolder;
14
import com.spice.profitmandi.common.ResponseCodeHolder;
19
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
15
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
20
import com.spice.profitmandi.common.model.ProfitMandiConstants;
16
import com.spice.profitmandi.common.model.ProfitMandiConstants;
21
import com.spice.profitmandi.dao.entity.Api;
17
import com.spice.profitmandi.dao.entity.Api;
22
import com.spice.profitmandi.dao.enumuration.Method;
-
 
23
import com.spice.profitmandi.dao.repository.ApiRepository;
18
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;
19
import com.spice.profitmandi.web.util.ResponseSender;
27
 
20
 
28
@Controller
21
@Controller
29
public class ApiController {
22
public class ApiController {
30
	
23
	
-
 
24
	@Autowired
-
 
25
	ResponseSender<?> responseSender;
-
 
26
	
31
	private static final Logger LOGGER=LoggerFactory.getLogger(ApiController.class);
27
	private static final Logger LOGGER=LoggerFactory.getLogger(ApiController.class);
32
	
28
	
33
	@Autowired
29
	@Autowired
34
	ApiRepository apiRepository;
30
	ApiRepository apiRepository;
35
	
31
	
Line 38... Line 34...
38
		LOGGER.info("requested url : "+request.getRequestURL().toString());
34
		LOGGER.info("requested url : "+request.getRequestURL().toString());
39
		final Api api = (Api)request.getAttribute(ProfitMandiConstants.API);
35
		final Api api = (Api)request.getAttribute(ProfitMandiConstants.API);
40
		request.removeAttribute(ProfitMandiConstants.API);
36
		request.removeAttribute(ProfitMandiConstants.API);
41
		try {
37
		try {
42
			apiRepository.persist(api);
38
			apiRepository.persist(api);
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);
39
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1000"));
45
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
40
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
46
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
41
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
47
			final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.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);
42
			return responseSender.badRequest(profitMandiBusinessException);
50
		}
43
		}
51
	}
44
	}
52
	
45
	
53
	@RequestMapping(value = ProfitMandiConstants.URL_API_ALL,method=RequestMethod.GET)
46
	@RequestMapping(value = ProfitMandiConstants.URL_API_ALL,method=RequestMethod.GET)
54
	public ResponseEntity<?> getAll(HttpServletRequest request){
47
	public ResponseEntity<?> getAll(HttpServletRequest request){
55
		LOGGER.info("requested url : "+request.getRequestURL().toString());
48
		LOGGER.info("requested url : "+request.getRequestURL().toString());
56
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectAll());
-
 
57
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
49
		return responseSender.ok(apiRepository.selectAll());
-
 
50
		
58
		
51
		
59
	}
52
	}
60
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.GET)
53
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.GET)
61
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
54
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
62
		LOGGER.info("requested url : "+request.getRequestURL().toString());
55
		LOGGER.info("requested url : "+request.getRequestURL().toString());
63
		try {
56
		try {
64
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectById(id));
-
 
65
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
57
			return responseSender.ok(apiRepository.selectById(id));
-
 
58
			
66
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
59
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
67
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
60
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
68
			final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
-
 
69
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
-
 
70
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
61
			return responseSender.badRequest(profitMandiBusinessException);
71
		}
62
		}
72
	}
63
	}
73
	
64
	
74
	@RequestMapping(value = ProfitMandiConstants.URL_API_NAME,method=RequestMethod.GET)
65
	@RequestMapping(value = ProfitMandiConstants.URL_API_NAME,method=RequestMethod.GET)
75
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
66
	public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
76
		LOGGER.info("requested url : "+request.getRequestURL().toString());
67
		LOGGER.info("requested url : "+request.getRequestURL().toString());
77
		try {
68
		try {
78
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectByName(name));
-
 
79
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
69
			return responseSender.ok(apiRepository.selectByName(name));
-
 
70
			
80
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
71
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
81
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
72
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
82
			final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
-
 
83
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
-
 
84
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
73
			return responseSender.badRequest(profitMandiBusinessException);
85
		}
74
		}
86
	}
75
	}
87
	
76
	
88
	@RequestMapping(value = ProfitMandiConstants.URL_API_URI,method=RequestMethod.GET)
77
	@RequestMapping(value = ProfitMandiConstants.URL_API_URI,method=RequestMethod.GET)
89
	public ResponseEntity<?> getByUri(HttpServletRequest request, @RequestParam(name = "uri") String uri){
78
	public ResponseEntity<?> getByUri(HttpServletRequest request, @RequestParam(name = "uri") String uri){
90
		LOGGER.info("requested url : "+request.getRequestURL().toString());
79
		LOGGER.info("requested url : "+request.getRequestURL().toString());
91
		final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectByUri(uri));
-
 
92
		return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
80
		return responseSender.ok(apiRepository.selectByUri(uri));
-
 
81
		
93
		
82
		
94
	}
83
	}
95
	
84
	
96
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.DELETE)
85
	@RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.DELETE)
97
	public ResponseEntity<?> deleteById(HttpServletRequest request, @RequestParam(name = "id") int id){
86
	public ResponseEntity<?> deleteById(HttpServletRequest request, @RequestParam(name = "id") int id){
98
		LOGGER.info("requested url : "+request.getRequestURL().toString());
87
		LOGGER.info("requested url : "+request.getRequestURL().toString());
99
		try {
88
		try {
100
			apiRepository.deleteById(id);
89
			apiRepository.deleteById(id);
101
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
-
 
102
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
-
 
103
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
-
 
104
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
-
 
105
			final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
-
 
106
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
-
 
107
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
90
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
108
		}
-
 
109
	}
-
 
110
	
-
 
111
	@SuppressWarnings("unchecked")
-
 
112
	@RequestMapping(value = ProfitMandiConstants.URL_API,method=RequestMethod.PUT)
-
 
113
	public ResponseEntity<?> updateNameById(HttpServletRequest request){
-
 
114
		LOGGER.info("requested url : "+request.getRequestURL().toString());
-
 
115
		final Map<String, Object> updateApiMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.UPDATE_API_MAP);
-
 
116
		request.removeAttribute(ProfitMandiConstants.UPDATE_API_MAP);
-
 
117
		try {
-
 
118
			apiRepository.updateNameById(updateApiMap.get(ProfitMandiConstants.NAME).toString(), updateApiMap.get(ProfitMandiConstants.URI).toString(),(Method)updateApiMap.get(ProfitMandiConstants.METHOD), (Integer)updateApiMap.get(ProfitMandiConstants.ID));
-
 
119
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1002"));
-
 
120
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
-
 
121
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
91
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
122
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
92
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
123
			final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
-
 
124
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
-
 
125
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
93
			return responseSender.badRequest(profitMandiBusinessException);
126
		}
94
		}
127
	}
95
	}
128
		
96
		
129
}
97
}
130
98