Subversion Repositories SmartDukaan

Rev

Rev 21426 | Blame | 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.Api;
import com.spice.profitmandi.dao.enumuration.Method;
import com.spice.profitmandi.dao.repository.ApiRepository;
import com.spice.profitmandi.web.model.ProfitMandiResponse;
import com.spice.profitmandi.web.model.Response;
import com.spice.profitmandi.web.model.ResponseStatus;

@Controller
public class ApiController {
        
        private static final Logger LOGGER=LoggerFactory.getLogger(ApiController.class);
        
        @Autowired
        ApiRepository apiRepository;
        
        @RequestMapping(value = ProfitMandiConstants.URL_API, method=RequestMethod.POST)
        public ResponseEntity<?> createApi(HttpServletRequest request){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                final Api api = (Api)request.getAttribute(ProfitMandiConstants.API);
                request.removeAttribute(ProfitMandiConstants.API);
                try {
                        apiRepository.persist(api);
                        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 pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.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_API_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, apiRepository.selectAll());
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                
        }
        @RequestMapping(value = ProfitMandiConstants.URL_API_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, apiRepository.selectById(id));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.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_API_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, apiRepository.selectByName(name));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.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_API_URI,method=RequestMethod.GET)
        public ResponseEntity<?> getByUri(HttpServletRequest request, @RequestParam(name = "uri") String uri){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, apiRepository.selectByUri(uri));
                return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                
        }
        
        @RequestMapping(value = ProfitMandiConstants.URL_API_ID,method=RequestMethod.DELETE)
        public ResponseEntity<?> deleteById(HttpServletRequest request, @RequestParam(name = "id") int id){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                try {
                        apiRepository.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 pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.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);
                }
        }
        
        @SuppressWarnings("unchecked")
        @RequestMapping(value = ProfitMandiConstants.URL_API,method=RequestMethod.PUT)
        public ResponseEntity<?> updateNameById(HttpServletRequest request){
                LOGGER.info("requested url : "+request.getRequestURL().toString());
                final Map<String, Object> updateApiMap = (Map<String, Object>)request.getAttribute(ProfitMandiConstants.UPDATE_API_MAP);
                request.removeAttribute(ProfitMandiConstants.UPDATE_API_MAP);
                try {
                        apiRepository.updateNameById(updateApiMap.get(ProfitMandiConstants.NAME).toString(), updateApiMap.get(ProfitMandiConstants.URI).toString(),(Method)updateApiMap.get(ProfitMandiConstants.METHOD), (Integer)updateApiMap.get(ProfitMandiConstants.ID));
                        final ProfitMandiResponse<?> profitMandiResponse=new ProfitMandiResponse<>(LocalDateTime.now(), request.getRequestURL().toString(), HttpStatus.OK.toString(), HttpStatus.OK, ResponseStatus.SUCCESS, ResponseCodeHolder.getMessage("API_OK_1002"));
                        return new ResponseEntity<>(profitMandiResponse,HttpStatus.OK);
                }catch (ProfitMandiBusinessException pmbe) {
                        LOGGER.error("ProfitMandi error: ", pmbe);
                        final Response response=new Response(pmbe.getRejectedType(), pmbe.getRejectedValue(),pmbe.getCode(), pmbe.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);
                }
        }
                
}