[676144b] | 1 | package com.example.autopartz.controller;
|
---|
| 2 |
|
---|
[4d67d70] | 3 | import com.example.autopartz.model.*;
|
---|
| 4 | import com.example.autopartz.repository.*;
|
---|
[2a552fe] | 5 | import com.example.autopartz.service.PartService;
|
---|
| 6 | import com.example.autopartz.service.RepairShopService;
|
---|
[676144b] | 7 | import com.example.autopartz.service.UserService;
|
---|
| 8 | import org.springframework.stereotype.Controller;
|
---|
| 9 | import org.springframework.ui.Model;
|
---|
[4d67d70] | 10 | import org.springframework.web.bind.annotation.*;
|
---|
[676144b] | 11 |
|
---|
| 12 | import javax.servlet.http.HttpServletResponse;
|
---|
| 13 | import java.io.IOException;
|
---|
| 14 | import java.time.LocalDate;
|
---|
| 15 | import java.util.List;
|
---|
| 16 | import java.util.Objects;
|
---|
| 17 |
|
---|
| 18 | @Controller
|
---|
| 19 | @RequestMapping("/")
|
---|
| 20 | public class AdminController {
|
---|
| 21 | private final UserService userService;
|
---|
[84652fb] | 22 | private final CarManufacturerRepository carManufacturerRepository;
|
---|
[676144b] | 23 | private final WarehousemanRepository warehousemanRepository;
|
---|
[f484b14] | 24 | private final DeliverymanRepository deliverymanRepository;
|
---|
[4d67d70] | 25 | private final CategoryRepository categoryRepository;
|
---|
| 26 | private final WarehouseRepository warehouseRepository;
|
---|
| 27 | private final CarRepository carRepository;
|
---|
| 28 | private final PartManufacturerRepository partManufacturerRepository;
|
---|
[2a552fe] | 29 | private final PartService partService;
|
---|
| 30 | private final RepairShopService repairShopService;
|
---|
[676144b] | 31 |
|
---|
[2a552fe] | 32 | public AdminController(UserService userService, CarManufacturerRepository carManufacturerRepository, WarehousemanRepository warehousemanRepository, DeliverymanRepository deliverymanRepository, CategoryRepository categoryRepository, WarehouseRepository warehouseRepository, CarRepository carRepository, PartManufacturerRepository partManufacturerRepository, PartService partService, RepairShopService repairShopService) {
|
---|
[676144b] | 33 | this.userService = userService;
|
---|
[84652fb] | 34 | this.carManufacturerRepository = carManufacturerRepository;
|
---|
[676144b] | 35 | this.warehousemanRepository = warehousemanRepository;
|
---|
[f484b14] | 36 | this.deliverymanRepository = deliverymanRepository;
|
---|
[4d67d70] | 37 | this.categoryRepository = categoryRepository;
|
---|
| 38 | this.warehouseRepository = warehouseRepository;
|
---|
| 39 | this.carRepository = carRepository;
|
---|
| 40 | this.partManufacturerRepository = partManufacturerRepository;
|
---|
[2a552fe] | 41 | this.partService = partService;
|
---|
| 42 | this.repairShopService = repairShopService;
|
---|
[676144b] | 43 | }
|
---|
| 44 |
|
---|
| 45 | @GetMapping("/viewUsers")
|
---|
| 46 | public String getAllUsers(Model model){
|
---|
| 47 | List<User> pendingList = userService.findAllUsers().stream().filter(u->u.getAuthorities().contains(Role.ROLE_PENDING_DELIVERYMAN) || u.getAuthorities().contains(Role.ROLE_PENDING_WAREHOUSEMAN)).toList();
|
---|
[cab5859] | 48 | if(pendingList.size()==0){
|
---|
| 49 | model.addAttribute("hasError",true);
|
---|
| 50 | }
|
---|
| 51 | else {
|
---|
| 52 | model.addAttribute("hasError",false);
|
---|
| 53 | model.addAttribute("users", pendingList);
|
---|
| 54 | }
|
---|
[676144b] | 55 | model.addAttribute("bodyContent", "viewUsers");
|
---|
| 56 | return "master-template";
|
---|
| 57 | }
|
---|
| 58 | @PostMapping("/approve/{id}")
|
---|
| 59 | public void approve(@PathVariable Integer id, HttpServletResponse response){
|
---|
| 60 | if(Objects.equals(userService.findById(id).getAuthorities().stream().findFirst().get(),Role.ROLE_PENDING_WAREHOUSEMAN)){
|
---|
| 61 | Warehouseman wh = (Warehouseman) userService.findById(id);
|
---|
| 62 | wh.setEmployed_from(LocalDate.now());
|
---|
| 63 | warehousemanRepository.save(wh);
|
---|
[d5913de] | 64 | try {
|
---|
| 65 | response.sendRedirect("/viewUsers");
|
---|
| 66 | } catch (IOException e) {
|
---|
| 67 | throw new RuntimeException(e);
|
---|
| 68 | }
|
---|
[f484b14] | 69 |
|
---|
| 70 | }
|
---|
| 71 | else {
|
---|
| 72 | Deliveryman dm = (Deliveryman) userService.findById(id);
|
---|
| 73 | dm.setEmployed_from(LocalDate.now());
|
---|
| 74 | deliverymanRepository.save(dm);
|
---|
[676144b] | 75 | try {
|
---|
| 76 | response.sendRedirect("/viewUsers");
|
---|
| 77 | } catch (IOException e) {
|
---|
| 78 | throw new RuntimeException(e);
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
[4d67d70] | 82 | @GetMapping("/addPart")
|
---|
| 83 | public String addPart(Model model){
|
---|
| 84 | model.addAttribute("categories",categoryRepository.findAll());
|
---|
| 85 | model.addAttribute("warehouses",warehouseRepository.findAll());
|
---|
| 86 | model.addAttribute("cars",carRepository.findAll());
|
---|
| 87 | model.addAttribute("manufacturers",partManufacturerRepository.findAll());
|
---|
| 88 | model.addAttribute("bodyContent","addPart");
|
---|
| 89 | return "master-template";
|
---|
| 90 | }
|
---|
| 91 | @PostMapping("/addPart")
|
---|
| 92 | public void addPart(@RequestParam String name, @RequestParam(required = false) String description,
|
---|
| 93 | @RequestParam Integer manufacturer, @RequestParam List<Car> cars,
|
---|
| 94 | @RequestParam List<Category> categories, @RequestParam Integer warehouse,
|
---|
| 95 | @RequestParam Integer quantity, @RequestParam Integer amount, HttpServletResponse response){
|
---|
[2a552fe] | 96 | partService.addPart(name,description,manufacturer,cars,categories,warehouse,quantity,amount);
|
---|
[4d67d70] | 97 | try {
|
---|
| 98 | response.sendRedirect("/");
|
---|
| 99 | } catch (IOException e) {
|
---|
| 100 | throw new RuntimeException(e);
|
---|
| 101 | }
|
---|
| 102 | }
|
---|
[84652fb] | 103 | @GetMapping("/addCarManufacturer")
|
---|
| 104 | public String getCarManView(Model model){
|
---|
| 105 | model.addAttribute("bodyContent","addCarManufacturer");
|
---|
| 106 | return "master-template";
|
---|
| 107 | }
|
---|
| 108 | @PostMapping("/addCarManufacturer")
|
---|
| 109 | public void saveCarManufacturer(@RequestParam String name,@RequestParam String location,
|
---|
[2a552fe] | 110 | HttpServletResponse response) {
|
---|
[84652fb] | 111 | carManufacturerRepository.save(new CarManufacturer(name,location));
|
---|
| 112 | try {
|
---|
| 113 | response.sendRedirect("/");
|
---|
| 114 | } catch (IOException e) {
|
---|
| 115 | throw new RuntimeException(e);
|
---|
| 116 | }
|
---|
| 117 | }
|
---|
| 118 | @GetMapping("/addPartManufacturer")
|
---|
| 119 | public String getPartManView(Model model){
|
---|
| 120 | model.addAttribute("bodyContent","addPartManufacturer");
|
---|
| 121 | return "master-template";
|
---|
| 122 | }
|
---|
| 123 | @PostMapping("/addPartManufacturer")
|
---|
| 124 | public void savePartManufacturer(@RequestParam String name,@RequestParam String location,
|
---|
[2a552fe] | 125 | HttpServletResponse response) {
|
---|
[84652fb] | 126 | partManufacturerRepository.save(new PartManufacturer(name,location));
|
---|
| 127 | try {
|
---|
| 128 | response.sendRedirect("/");
|
---|
| 129 | } catch (IOException e) {
|
---|
| 130 | throw new RuntimeException(e);
|
---|
| 131 | }
|
---|
| 132 | }
|
---|
| 133 | @GetMapping("/addCategory")
|
---|
| 134 | public String getCategoryView(Model model){
|
---|
| 135 | model.addAttribute("bodyContent","addCategory");
|
---|
| 136 | return "master-template";
|
---|
| 137 | }
|
---|
| 138 | @PostMapping("/addCategory")
|
---|
| 139 | public void saveCategory(@RequestParam String name,
|
---|
[2a552fe] | 140 | HttpServletResponse response) {
|
---|
[84652fb] | 141 | categoryRepository.save(new Category(name));
|
---|
| 142 | try {
|
---|
| 143 | response.sendRedirect("/");
|
---|
| 144 | } catch (IOException e) {
|
---|
| 145 | throw new RuntimeException(e);
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | @GetMapping("/addCar")
|
---|
| 149 | public String getCarView(Model model){
|
---|
| 150 | model.addAttribute("bodyContent","addCar");
|
---|
| 151 | model.addAttribute("manufacturers",carManufacturerRepository.findAll());
|
---|
| 152 | return "master-template";
|
---|
| 153 | }
|
---|
| 154 | @PostMapping("/addCar")
|
---|
| 155 | public void saveCar(@RequestParam Integer since,@RequestParam Integer till,
|
---|
| 156 | @RequestParam String name,@RequestParam Integer mId,
|
---|
| 157 | HttpServletResponse response) {
|
---|
| 158 | carRepository.save(new Car(since,till,name,carManufacturerRepository.findById(mId).get()));
|
---|
| 159 | try {
|
---|
| 160 | response.sendRedirect("/");
|
---|
| 161 | } catch (IOException e) {
|
---|
| 162 | throw new RuntimeException(e);
|
---|
| 163 | }
|
---|
| 164 | }
|
---|
| 165 | @GetMapping("/addRepairShop")
|
---|
| 166 | public String getRepairShopView(Model model){
|
---|
| 167 | model.addAttribute("bodyContent","addRepairShop");
|
---|
| 168 | model.addAttribute("manufacturers",carManufacturerRepository.findAll());
|
---|
| 169 | return "master-template";
|
---|
| 170 | }
|
---|
| 171 | @PostMapping("/addRepairShop")
|
---|
| 172 | public void saveRepairShop(@RequestParam String name,@RequestParam String location,
|
---|
| 173 | @RequestParam String number,@RequestParam Integer carMId,
|
---|
| 174 | HttpServletResponse response) {
|
---|
[2a552fe] | 175 | repairShopService.save(name,location,number,carMId);
|
---|
[84652fb] | 176 | try {
|
---|
| 177 | response.sendRedirect("/");
|
---|
| 178 | } catch (IOException e) {
|
---|
| 179 | throw new RuntimeException(e);
|
---|
| 180 | }
|
---|
| 181 | }
|
---|
| 182 | @GetMapping("/addWarehouse")
|
---|
| 183 | public String getWarehouseView(Model model){
|
---|
| 184 | model.addAttribute("bodyContent","addWarehouse");
|
---|
| 185 | return "master-template";
|
---|
| 186 | }
|
---|
| 187 | @PostMapping("/addWarehouse")
|
---|
| 188 | public void saveWarehouse(@RequestParam String name,
|
---|
| 189 | HttpServletResponse response) {
|
---|
| 190 | warehouseRepository.save(new Warehouse(name));
|
---|
| 191 | try {
|
---|
| 192 | response.sendRedirect("/");
|
---|
| 193 | } catch (IOException e) {
|
---|
| 194 | throw new RuntimeException(e);
|
---|
| 195 | }
|
---|
| 196 | }
|
---|
[676144b] | 197 | }
|
---|