Subversion Repositories SmartDukaan

Rev

View as "text/plain" | Blame | Last modification | View Log | RSS feed

package com.spice.profitmandi.web.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
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.exception.ProfitMandiBusinessException;
import com.spice.profitmandi.common.model.CreateVendorRequest;
import com.spice.profitmandi.common.util.Utils;
import com.spice.profitmandi.dao.entity.nonbillable.Vendor;
import com.spice.profitmandi.dao.entity.user.Address;
import com.spice.profitmandi.dao.repository.nonbillable.VendorRepository;
import com.spice.profitmandi.dao.repository.user.AddressRepository;
import com.spice.profitmandi.service.nonbillable.VendorService;

@Controller
@Transactional
public class VendorController {
        
        private static final Logger LOGGER = LogManager.getLogger(VendorController.class);
        
        @Autowired
        private VendorService vendorService;
        
        @Autowired
        private VendorRepository vendorRepository;
        
        @Autowired
        private AddressRepository addressRepository;
        
        @RequestMapping(value = "/createVendor", method = RequestMethod.GET)
        public String createVendor(HttpServletRequest request, Model model) {
                model.addAttribute("stateNames", Utils.getAllStateNames());
                return "create-vendor";
        }
        
        @RequestMapping(value = "/createVendor", method = RequestMethod.POST)
        public String createVendor(HttpServletRequest request, @RequestBody CreateVendorRequest createVendorRequest, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), createVendorRequest);
                vendorService.createVendor(createVendorRequest);
                List<Vendor> vendors = vendorRepository.selectAll(offset, limit);
                long size = vendorRepository.selectAllCount();
                model.addAttribute("vendors", vendors);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (vendors.size() < limit){
                        model.addAttribute("end", offset + vendors.size());
                }else{
                        model.addAttribute("end", offset + limit);
                }
                return "vendors";
        }
        
        @RequestMapping(value = "/getVendors", method = RequestMethod.GET)
        public String getVendors(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                List<Vendor> vendors = vendorRepository.selectAll(offset, limit);
                long size = vendorRepository.selectAllCount();
                model.addAttribute("vendors", vendors);
                model.addAttribute("start", offset + 1);
                model.addAttribute("size", size);
                if (vendors.size() < limit){
                        model.addAttribute("end", offset + vendors.size());
                }else{
                        model.addAttribute("end", offset + limit);
                }
                return "vendors";
        }
        
        @RequestMapping(value = "/getPaginatedVendors", method = RequestMethod.GET)
        public String getPaginatedVendors(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
                List<Vendor> vendors  = vendorRepository.selectAll(offset, limit);
                model.addAttribute("vendors", vendors);
                return "vendors-paginated";
        }
        
        @RequestMapping(value = "/getVendorDetails", method = RequestMethod.GET)
        public String getVendorDetails(HttpServletRequest request, int vendorId, Model model) throws ProfitMandiBusinessException{
                Vendor vendor = vendorRepository.selectById(vendorId);
                Address address = addressRepository.selectById(vendor.getAddressId());
                model.addAttribute("vendor", vendor);
                model.addAttribute("vendorAddress", address);
                return "vendor-details";
        }
        
}