Subversion Repositories SmartDukaan

Rev

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