Subversion Repositories SmartDukaan

Rev

Rev 21431 | Rev 21448 | Go to most recent revision | 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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
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.dao.entity.Shop;
import com.spice.profitmandi.dao.repository.RetailerRepository;
import com.spice.profitmandi.dao.repository.ShopRepository;
import com.spice.profitmandi.web.model.ProfitMandiResponse;
import com.spice.profitmandi.web.model.Response;
import com.spice.profitmandi.web.model.ResponseStatus;

@Controller
public class ShopController {
        
        private static final Logger LOGGER=LoggerFactory.getLogger(ShopController.class);
        
        @Autowired
        ShopRepository shopRepository;
        
        @Autowired
        RetailerRepository retailerRepository;
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP, method=RequestMethod.POST)
        public ResponseEntity<?> createShop(HttpServletRequest request){
                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);
                
                try {
                        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);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1000"));
                        return new ResponseEntity<>(profitMandiResponse, HttpStatus.OK);
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_ALL, method=RequestMethod.GET)
        public ResponseEntity<?> getAll(HttpServletRequest request){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, shopRepository.selectAll());
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
        }
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.GET)
        public ResponseEntity<?> getById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, shopRepository.selectById(id));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_NAME, method=RequestMethod.GET)
        public ResponseEntity<?> getByName(HttpServletRequest request, @RequestParam(name = "name") String name){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, shopRepository.selectByName(name));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }
        }
        
        
        
        @RequestMapping(value = ProfitMandiConstants.URL_SHOP_ID, method=RequestMethod.DELETE)
        public ResponseEntity<?> removeById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        shopRepository.deleteById(id);
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1001"));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException profitMandiBusinessException) {
                        LOGGER.error("ProfitMandi error: ",  profitMandiBusinessException);
                        final Response response=new Response(profitMandiBusinessException.getRejectedType(), profitMandiBusinessException.getRejectedValue(),profitMandiBusinessException.getCode(), profitMandiBusinessException.getMessage());
                        final ProfitMandiResponse<Response> chatOnResponse=new ProfitMandiResponse<Response>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.BAD_REQUEST.toString(), HttpStatus.BAD_REQUEST, ResponseStatus.FAILURE, response);
                        return new ResponseEntity<>(chatOnResponse,HttpStatus.BAD_REQUEST);
                }
        }
        
}