| 23784 |
ashik.ali |
1 |
package com.spice.profitmandi.web.controller;
|
|
|
2 |
|
|
|
3 |
import java.util.List;
|
|
|
4 |
|
|
|
5 |
import javax.servlet.http.HttpServletRequest;
|
|
|
6 |
|
|
|
7 |
import org.apache.logging.log4j.LogManager;
|
|
|
8 |
import org.apache.logging.log4j.Logger;
|
|
|
9 |
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
10 |
import org.springframework.stereotype.Controller;
|
|
|
11 |
import org.springframework.transaction.annotation.Transactional;
|
|
|
12 |
import org.springframework.ui.Model;
|
|
|
13 |
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
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.exception.ProfitMandiBusinessException;
|
|
|
19 |
import com.spice.profitmandi.common.model.CreateVendorRequest;
|
|
|
20 |
import com.spice.profitmandi.common.util.Utils;
|
|
|
21 |
import com.spice.profitmandi.dao.entity.nonbillable.Vendor;
|
|
|
22 |
import com.spice.profitmandi.dao.entity.user.Address;
|
|
|
23 |
import com.spice.profitmandi.dao.repository.nonbillable.VendorRepository;
|
|
|
24 |
import com.spice.profitmandi.dao.repository.user.AddressRepository;
|
|
|
25 |
import com.spice.profitmandi.service.nonbillable.VendorService;
|
|
|
26 |
|
|
|
27 |
@Controller
|
|
|
28 |
@Transactional
|
|
|
29 |
public class VendorController {
|
|
|
30 |
|
|
|
31 |
private static final Logger LOGGER = LogManager.getLogger(VendorController.class);
|
|
|
32 |
|
|
|
33 |
@Autowired
|
|
|
34 |
private VendorService vendorService;
|
|
|
35 |
|
|
|
36 |
@Autowired
|
|
|
37 |
private VendorRepository vendorRepository;
|
|
|
38 |
|
|
|
39 |
@Autowired
|
|
|
40 |
private AddressRepository addressRepository;
|
|
|
41 |
|
|
|
42 |
@RequestMapping(value = "/createVendor", method = RequestMethod.GET)
|
|
|
43 |
public String createVendor(HttpServletRequest request, Model model) {
|
|
|
44 |
model.addAttribute("stateNames", Utils.getAllStateNames());
|
|
|
45 |
return "create-vendor";
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
@RequestMapping(value = "/createVendor", method = RequestMethod.POST)
|
|
|
49 |
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{
|
|
|
50 |
LOGGER.info("Request Received at url {} with body {}", request.getRequestURI(), createVendorRequest);
|
|
|
51 |
vendorService.createVendor(createVendorRequest);
|
|
|
52 |
List<Vendor> vendors = vendorRepository.selectAll(offset, limit);
|
|
|
53 |
long size = vendorRepository.selectAllCount();
|
|
|
54 |
model.addAttribute("vendors", vendors);
|
|
|
55 |
model.addAttribute("start", offset + 1);
|
|
|
56 |
model.addAttribute("size", size);
|
|
|
57 |
if (vendors.size() < limit){
|
|
|
58 |
model.addAttribute("end", offset + vendors.size());
|
|
|
59 |
}else{
|
|
|
60 |
model.addAttribute("end", offset + limit);
|
|
|
61 |
}
|
|
|
62 |
return "vendors";
|
|
|
63 |
}
|
|
|
64 |
|
|
|
65 |
@RequestMapping(value = "/getVendors", method = RequestMethod.GET)
|
|
|
66 |
public String getVendors(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
|
|
67 |
List<Vendor> vendors = vendorRepository.selectAll(offset, limit);
|
|
|
68 |
long size = vendorRepository.selectAllCount();
|
|
|
69 |
model.addAttribute("vendors", vendors);
|
|
|
70 |
model.addAttribute("start", offset + 1);
|
|
|
71 |
model.addAttribute("size", size);
|
|
|
72 |
if (vendors.size() < limit){
|
|
|
73 |
model.addAttribute("end", offset + vendors.size());
|
|
|
74 |
}else{
|
|
|
75 |
model.addAttribute("end", offset + limit);
|
|
|
76 |
}
|
|
|
77 |
return "vendors";
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@RequestMapping(value = "/getPaginatedVendors", method = RequestMethod.GET)
|
|
|
81 |
public String getPaginatedVendors(HttpServletRequest request, @RequestParam(name = "offset", defaultValue = "0") int offset, @RequestParam(name = "limit", defaultValue = "10") int limit, Model model) throws ProfitMandiBusinessException{
|
|
|
82 |
List<Vendor> vendors = vendorRepository.selectAll(offset, limit);
|
|
|
83 |
model.addAttribute("vendors", vendors);
|
|
|
84 |
return "vendors-paginated";
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
@RequestMapping(value = "/getVendorDetails", method = RequestMethod.GET)
|
|
|
88 |
public String getVendorDetails(HttpServletRequest request, int vendorId, Model model) throws ProfitMandiBusinessException{
|
|
|
89 |
Vendor vendor = vendorRepository.selectById(vendorId);
|
|
|
90 |
Address address = addressRepository.selectById(vendor.getAddressId());
|
|
|
91 |
model.addAttribute("vendor", vendor);
|
|
|
92 |
model.addAttribute("vendorAddress", address);
|
|
|
93 |
return "vendor-details";
|
|
|
94 |
}
|
|
|
95 |
|
|
|
96 |
}
|