Subversion Repositories SmartDukaan

Rev

Rev 21448 | Rev 21702 | 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
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.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.Shop;
21
import com.spice.profitmandi.dao.repository.RetailerRepository;
22
import com.spice.profitmandi.dao.repository.ShopRepository;
21448 ashik.ali 23
import com.spice.profitmandi.web.util.ResponseSender;
21292 ashik.ali 24
 
25
@Controller
26
public class ShopController {
27
 
21448 ashik.ali 28
	@Autowired
29
	ResponseSender<?> responseSender;
30
 
31
 
21292 ashik.ali 32
	private static final Logger LOGGER=LoggerFactory.getLogger(ShopController.class);
33
 
34
	@Autowired
35
	ShopRepository shopRepository;
36
 
37
	@Autowired
38
	RetailerRepository retailerRepository;
39
 
40
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP, method=RequestMethod.POST)
21302 ashik.ali 41
	public ResponseEntity<?> createShop(HttpServletRequest request){
21292 ashik.ali 42
		LOGGER.info("requested url : "+request.getRequestURL().toString());
43
		@SuppressWarnings("unchecked")
44
		final Map<String, Object>  createShopMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
45
		request.removeAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
46
 
47
		try {
48
			Shop shop = new Shop();
21426 ashik.ali 49
			shop.setRetailerId((int)createShopMap.get(ProfitMandiConstants.RETAILER_ID));
21390 ashik.ali 50
			//shop.setRetailer(retailer);
21292 ashik.ali 51
			shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
52
			shop.setCreateTimestamp(LocalDateTime.now());
53
			shop.setUpdateTimestamp(LocalDateTime.now());
54
			shopRepository.persist(shop);
21448 ashik.ali 55
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1000"));
21440 ashik.ali 56
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
57
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
21448 ashik.ali 58
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 59
		}
60
	}
61
 
62
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP_ALL, method=RequestMethod.GET)
21496 ashik.ali 63
	public ResponseEntity<?> getAll(HttpServletRequest request, @RequestParam(name = ProfitMandiConstants.PAGE_NUMBER) int pageNumber, @RequestParam(name = ProfitMandiConstants.PAGE_SIZE) int pageSize){
21292 ashik.ali 64
		LOGGER.info("requested url : "+request.getRequestURL().toString());
21496 ashik.ali 65
		return responseSender.ok(shopRepository.selectAll(pageNumber, pageSize));
21292 ashik.ali 66
	}
67
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.GET)
21431 ashik.ali 68
	public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 69
		LOGGER.info("requested url : "+request.getRequestURL().toString());
70
		try {
21448 ashik.ali 71
			return responseSender.ok(shopRepository.selectById(id));
21440 ashik.ali 72
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
73
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
21448 ashik.ali 74
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 75
		}
76
	}
77
 
78
 
79
	@RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.DELETE)
21431 ashik.ali 80
	public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
21292 ashik.ali 81
		LOGGER.info("requested url : "+request.getRequestURL().toString());
82
		try {
83
			shopRepository.deleteById(id);
21448 ashik.ali 84
			return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
21440 ashik.ali 85
		}catch (ProfitMandiBusinessException profitMandiBusinessException) {
86
			LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
21448 ashik.ali 87
			return responseSender.badRequest(profitMandiBusinessException);
21292 ashik.ali 88
		}
89
	}
90
 
91
}