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
import java.util.Map;
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;
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
import org.springframework.web.multipart.MultipartFile;
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.entity.Shop;
24
import com.spice.profitmandi.dao.repository.RetailerRepository;
25
import com.spice.profitmandi.dao.repository.ShopRepository;
26
import com.spice.profitmandi.web.model.ProfitMandiResponse;
27
import com.spice.profitmandi.web.model.Response;
28
import com.spice.profitmandi.web.model.ResponseStatus;
29
 
30
@Controller
31
public class ShopController {
32
 
33
	private static final Logger LOGGER=LoggerFactory.getLogger(ShopController.class);
34
 
35
	@Autowired
36
	ShopRepository shopRepository;
37
 
38
	@Autowired
39
	RetailerRepository retailerRepository;
40
 
41
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP, method=RequestMethod.POST)
42
	public ResponseEntity<?> createShop(@RequestParam("document") final MultipartFile multipartFile, HttpServletRequest request){
43
		LOGGER.info("requested url : "+request.getRequestURL().toString());
44
		@SuppressWarnings("unchecked")
45
		final Map<String, Object>  createShopMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
46
		request.removeAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
47
 
48
		try {
49
			final Retailer retailer = retailerRepository.selectById(createShopMap.get(ProfitMandiConstants.RETAILER_ID).toString());
50
			Shop shop = new Shop();
51
			shop.setRetailer(retailer);
52
			shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
53
			shop.setCreateTimestamp(LocalDateTime.now());
54
			shop.setUpdateTimestamp(LocalDateTime.now());
55
			shopRepository.persist(shop);
56
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1000"));
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_SHOP_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, shopRepository.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_SHOP_ID, method=RequestMethod.GET)
85
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") String id){
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, shopRepository.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_SHOP_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, shopRepository.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
 
124
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.DELETE)
125
	public ResponseEntity<?> deleteById(HttpServletRequest request, @RequestParam(name = "id") String id){
126
		LOGGER.info("requested url : "+request.getRequestURL().toString());
127
		try {
128
			shopRepository.deleteById(id);
129
			final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
130
			return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
131
		}catch (ProfitMandiBusinessException pmbe) {
132
			LOGGER.error("ProfitMandi error: ", pmbe);
133
			final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.getMessage());
134
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
135
			return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
136
		}catch (Exception e) {
137
			LOGGER.error("Internal Server error : ",e);
138
			final Response response=new Response("","","", e.getMessage());
139
			final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), HttpStatus.INTERNAL_SERVER_ERROR, ResponseStatus.FAILURE, response);
140
			return new ResponseEntity<>(chatOnResponse,HttpStatus.INTERNAL_SERVER_ERROR);
141
		}
142
	}
143
 
144
}