Subversion Repositories SmartDukaan

Rev

Rev 35435 | View as "text/plain" | Blame | Compare with Previous | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.time.LocalDateTime;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.spice.profitmandi.common.ResponseCodeHolder;
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.ProfitMandiConstants;
import com.spice.profitmandi.common.web.util.ResponseSender;
import com.spice.profitmandi.dao.entity.dtr.Shop;
import com.spice.profitmandi.dao.repository.dtr.RetailerRepository;
import com.spice.profitmandi.dao.repository.dtr.ShopRepository;

@Controller
@Transactional(rollbackFor=Throwable.class)
public class ShopController {
        
        @Autowired
        private ResponseSender<?> responseSender;
        
        private static final Logger LOGGER=LogManager.getLogger(ShopController.class);
        
        @Autowired
        ShopRepository shopRepository;
        
        @Autowired
        RetailerRepository retailerRepository;
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP, method=RequestMethod.POST)
        public ResponseEntity<?> createShop(HttpServletRequest request) throws ProfitMandiBusinessException{
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                @SuppressWarnings("unchecked")
                final Map<String, Object>  createShopMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
                request.removeAttribute(ProfitMandiConstants.CREATE_SHOP_MAP);
                
                Shop shop = new Shop();
                shop.setRetailerId((int)createShopMap.get(ProfitMandiConstants.RETAILER_ID));
                //shop.setRetailer(retailer);
                shop.setName(createShopMap.get(ProfitMandiConstants.NAME).toString());
                shop.setCreateTimestamp(LocalDateTime.now());
                shop.setUpdateTimestamp(LocalDateTime.now());
                shopRepository.persist(shop);
                return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1000"));
                
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id)throws ProfitMandiBusinessException{
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                return responseSender.ok(shopRepository.selectById(id));
        }
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.DELETE)
        public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id)throws ProfitMandiBusinessException{
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                shopRepository.deleteById(id);
                return responseSender.ok(ResponseCodeHolder.getMessage("API_OK_1001"));
        }
        
}