diff --git a/data.mv.db b/data.mv.db index b9a589c..30f0367 100644 Binary files a/data.mv.db and b/data.mv.db differ diff --git a/src/main/java/com/example/maxim/lab3/controller/BuyerController.java b/src/main/java/com/example/maxim/lab3/controller/BuyerController.java index e262f56..6ce2232 100644 --- a/src/main/java/com/example/maxim/lab3/controller/BuyerController.java +++ b/src/main/java/com/example/maxim/lab3/controller/BuyerController.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping(WebConfiguration.REST_API + "/buyer") +@RequestMapping("(WebConfiguration.REST_API + /buyer") public class BuyerController { private final BuyerService buyerService; public BuyerController(BuyerService buyerService){ @@ -39,6 +39,7 @@ public class BuyerController { return new BuyerDTO(buyerService.findBuyer(id)); } + @GetMapping public List findAllBuyer() { return buyerService.findAllBuyers() diff --git a/src/main/java/com/example/maxim/lab3/controller/BuyerDTO.java b/src/main/java/com/example/maxim/lab3/controller/BuyerDTO.java index 0aac610..c89ad04 100644 --- a/src/main/java/com/example/maxim/lab3/controller/BuyerDTO.java +++ b/src/main/java/com/example/maxim/lab3/controller/BuyerDTO.java @@ -6,12 +6,12 @@ import jakarta.validation.constraints.NotBlank; public class BuyerDTO { private long id; - @NotBlank(message = "FirstName can't be null or empty") + @NotBlank(message = "buyerFirstName can't be null or empty") private String buyerFirstName; - @NotBlank(message = "SecondName can't be null or empty") + @NotBlank(message = "buyerSecondName can't be null or empty") private String buyerSecondName; public BuyerDTO(Buyer buyer){ - this.id = buyer.getId(); + this.id =buyer.getId(); this.buyerFirstName = buyer.getBuyerFirstName(); this.buyerSecondName = buyer.getBuyerSecondName(); } diff --git a/src/main/java/com/example/maxim/lab3/controller/BuyerMVCController.java b/src/main/java/com/example/maxim/lab3/controller/BuyerMVCController.java index a6e29c0..176d65c 100644 --- a/src/main/java/com/example/maxim/lab3/controller/BuyerMVCController.java +++ b/src/main/java/com/example/maxim/lab3/controller/BuyerMVCController.java @@ -22,6 +22,7 @@ public class BuyerMVCController { .toList()); return "buyer"; } + @GetMapping(value = {"/edit", "/edit/{id}"}) public String editBuyer(@PathVariable(required = false) Long id, Model model) { diff --git a/src/main/java/com/example/maxim/lab3/controller/CarController.java b/src/main/java/com/example/maxim/lab3/controller/CarController.java index bbae588..24de0c2 100644 --- a/src/main/java/com/example/maxim/lab3/controller/CarController.java +++ b/src/main/java/com/example/maxim/lab3/controller/CarController.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping(WebConfiguration.REST_API + "/car") +@RequestMapping("WebConfiguration.REST_API + /car") public class CarController { private final CarService carService; public CarController(CarService carService){ @@ -37,8 +37,8 @@ public class CarController { @GetMapping public List findAllCars(){ return carService.findAllCars() - .stream() - .map(x -> new CarDTO(x)) - .toList(); + .stream() + .map(x -> new CarDTO(x)) + .toList(); } } diff --git a/src/main/java/com/example/maxim/lab3/controller/CarDTO.java b/src/main/java/com/example/maxim/lab3/controller/CarDTO.java index f9529ca..f0784f8 100644 --- a/src/main/java/com/example/maxim/lab3/controller/CarDTO.java +++ b/src/main/java/com/example/maxim/lab3/controller/CarDTO.java @@ -8,7 +8,7 @@ import java.util.List; public class CarDTO { private long id; - @NotBlank(message = "CarName can't be null or empty") + @NotBlank(message = "carName can't be null or empty") private String carName; private List buyerDTOList; private List storeDTOList; @@ -33,19 +33,24 @@ public class CarDTO { public String getCarName(){ return carName; } - public void setCarName(String carName){ - this.carName = carName; - } public List getBuyerDTOList(){ return buyerDTOList; } - public void setBuyerDTOList(List buyers){ - this.buyerDTOList = buyers; + + public void setCarName(String carName) { + this.carName = carName; } + + public void setBuyerDTOList(List buyerDTOList) { + this.buyerDTOList = buyerDTOList; + } + + public void setStoreDTOList(List storeDTOList) { + this.storeDTOList = storeDTOList; + } + public List getStoreDTOList(){ return storeDTOList; } - public void setStoreDTOList(List stores){ - this.storeDTOList = stores; - } } + diff --git a/src/main/java/com/example/maxim/lab3/controller/CarMVCController.java b/src/main/java/com/example/maxim/lab3/controller/CarMVCController.java index 26dffe5..5999a44 100644 --- a/src/main/java/com/example/maxim/lab3/controller/CarMVCController.java +++ b/src/main/java/com/example/maxim/lab3/controller/CarMVCController.java @@ -32,11 +32,10 @@ public class CarMVCController { } @GetMapping(value = {"/edit", "/edit/{id}"}) public String editCar(@PathVariable(required = false) Long id, - @RequestParam(value ="PW", required = false) String PW, - Model model) { + Model model) { if (id == null || id <= 0) { List buyers = buyerService.findAllBuyers().stream() - .map(BuyerDTO::new).toList(); + .map(BuyerDTO::new).toList(); List stores = storeService.findAllStores().stream() .map(StoreDTO::new).toList(); model.addAttribute("CarDTO", new CarDTO()); @@ -44,75 +43,73 @@ public class CarMVCController { model.addAttribute("stores", stores); return "car-create"; } else { - if(PW.equals("N")) { - model.addAttribute("carId", id); - model.addAttribute("CarDTO", new CarDTO(carService.findCar(id))); - return "car-create"; - } - if(PW.equals("W")) { + String name = carService.findCar(id).getCarName(); List buyers = buyerService.findAllBuyers().stream() .map(BuyerDTO::new).toList(); List carBuyers = carService - .findCar(id).getBuyers().stream().map(BuyerDTO::new).toList(); + .findCar(id) + .getBuyers() + .stream() + .map(BuyerDTO::new) + .toList(); + List stores = storeService.findAllStores().stream() + .map(StoreDTO::new).toList(); + List carStores = carService + .findCar(id) + .getStores() + .stream() + .map(StoreDTO::new) + .toList(); + model.addAttribute("name", name); model.addAttribute("carId", id); model.addAttribute("carBuyers", carBuyers); model.addAttribute("buyers", buyers); - return "car-buyer"; - } - if(PW.equals("P")) { - List stores = storeService.findAllStores().stream() - .map(StoreDTO::new).toList(); - List carStores = carService - .findCar(id).getStores().stream().map(StoreDTO::new).toList(); - model.addAttribute("carId", id); model.addAttribute("carStores", carStores); model.addAttribute("stores", stores); - return "car-store"; - } + model.addAttribute("CarDTO", new CarDTO(carService.findCar(id))); } - return "car"; + return "car-set"; } - @PostMapping(value = {"/0"}) + @PostMapping(value = {"/"}) public String saveCar(@ModelAttribute @Valid CarDTO carDTO, - BindingResult bindingResult, - Model model) { + BindingResult bindingResult, + Model model) { if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors()); return "car-create"; } - carService.addCar(carDTO); + carService.addCar(carDTO); +// } else { +// carService.updateCar(id, carDTO); +// } return "redirect:/car"; } @PostMapping(value = {"/{id}"}) - public String editCarName(@PathVariable Long id, - @RequestParam("carName") String name){ - carService.findCar(id).setCarName(name); - carService.updateCar(id, new CarDTO(carService.findCar(id))); - return "redirect:/car/"; - } - @PostMapping(value = {"/{id}/buyer"}) - public String editBuyerCar(@PathVariable Long id, - @RequestParam("buyer") Long buyerId, - @RequestParam(value = "delete", required = false) boolean del, - Model model) { - if (del) { - carService.deleteBuyer(id, buyerId); - } else { - carService.addBuyer(id, buyerId); - } - return "redirect:/car/edit/" + id + "?PW=W"; - } - @PostMapping(value = {"/{id}/store"}) - public String editStoreCar(@PathVariable Long id, - @RequestParam("store") Long storeId, - @RequestParam(value = "delete", required = false) boolean del, - Model model) { - if (del) { - carService.deleteStore(id, storeId); - } else { - carService.addStore(id, storeId); + public String editCar(@PathVariable Long id, + @RequestParam("PW") String PW, + @RequestParam("wpName") String name, + @RequestParam(value="idPW", required = false) Long idPW, + @RequestParam(value = "delete", required = false) boolean del, + Model model) { + if(PW.equals("N")){ + carService.findCar(id).setCarName(name); + carService.updateCar(id, new CarDTO(carService.findCar(id))); } - return "redirect:/car/edit/" + id + "?PW=P"; + if (PW.equals("W")){ + if (del == true) { + carService.deleteBuyer(id, idPW); + } else { + carService.addBuyer(id, idPW); + } + } + else if(PW.equals("P")){ + if (del == true) { + carService.deleteStore(id, idPW); + } else { + carService.addStore(id, idPW); + } + } + return "redirect:/car/edit/" + id; } @PostMapping("/delete/{id}") public String deleteCar(@PathVariable Long id) { diff --git a/src/main/java/com/example/maxim/lab3/controller/StoreController.java b/src/main/java/com/example/maxim/lab3/controller/StoreController.java index f2d0bb6..9ebd719 100644 --- a/src/main/java/com/example/maxim/lab3/controller/StoreController.java +++ b/src/main/java/com/example/maxim/lab3/controller/StoreController.java @@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*; import java.util.List; @RestController -@RequestMapping(WebConfiguration.REST_API + "/store") +@RequestMapping("WebConfiguration.REST_API + /store") public class StoreController { private final StoreService storeService; public StoreController(StoreService storeService){ @@ -40,16 +40,10 @@ public class StoreController { } @GetMapping("/buyer/{id}") public List findBuyersOnCar(@PathVariable Long id){ - return storeService.findAllBuyersProducedStore(id) - .stream() - .map(BuyerDTO::new) - .toList(); + return storeService.findAllBuyersProducedStore(id).stream().map(BuyerDTO::new).toList(); } @GetMapping public List findAllStore() { - return storeService.findAllStores() - .stream() - .map(StoreDTO::new) - .toList(); + return storeService.findAllStores().stream().map(StoreDTO::new).toList(); } } diff --git a/src/main/java/com/example/maxim/lab3/controller/StoreDTO.java b/src/main/java/com/example/maxim/lab3/controller/StoreDTO.java index a438535..92f10cc 100644 --- a/src/main/java/com/example/maxim/lab3/controller/StoreDTO.java +++ b/src/main/java/com/example/maxim/lab3/controller/StoreDTO.java @@ -9,7 +9,7 @@ import java.util.List; public class StoreDTO { private long id; - @NotBlank(message = "StoreName can't be null or empty") + @NotBlank(message = "storeName can't be null or empty") private String storeName; private List carDTOList; public StoreDTO(Store store){ @@ -18,7 +18,7 @@ public class StoreDTO { this.carDTOList = store.getCar() == null ? null : store.getCar() .stream() .filter(x -> x.getStores().contains(store.getId())) - .map(y -> new CarDTO()) + .map(y -> new CarDTO(y)) .toList(); } public StoreDTO() { @@ -33,7 +33,9 @@ public class StoreDTO { public void setStoreName(String storeName){ this.storeName = storeName; } + public List getCarDTOList(){ return carDTOList; } } + diff --git a/src/main/java/com/example/maxim/lab3/controller/StoreMVCController.java b/src/main/java/com/example/maxim/lab3/controller/StoreMVCController.java index 274b26c..6511dec 100644 --- a/src/main/java/com/example/maxim/lab3/controller/StoreMVCController.java +++ b/src/main/java/com/example/maxim/lab3/controller/StoreMVCController.java @@ -23,7 +23,7 @@ public class StoreMVCController { return "store"; } @GetMapping("/info/{id}") - public String findBuyersOnCar(@PathVariable(required = false) Long id, Model model){ + public String findBuyersOnWorkplace(@PathVariable(required = false) Long id, Model model){ model.addAttribute("cathegory", "Кто производит " + storeService.findStore(id).getStoreName()); model.addAttribute("buyers", storeService.findAllBuyersProducedStore(id) .stream() @@ -34,7 +34,7 @@ public class StoreMVCController { } @GetMapping(value = {"/edit", "/edit/{id}"}) public String editStore(@PathVariable(required = false) Long id, - Model model) { + Model model) { if (id == null || id <= 0) { model.addAttribute("StoreDTO", new StoreDTO()); } else { @@ -43,11 +43,11 @@ public class StoreMVCController { } return "store-edit"; } - @PostMapping(value = {"/", "/{id}"}) + @PostMapping(value = {"", "/{id}"}) public String saveStore(@PathVariable(required = false) Long id, - @ModelAttribute @Valid StoreDTO storeDTO, - BindingResult bindingResult, - Model model) { + @ModelAttribute @Valid StoreDTO storeDTO, + BindingResult bindingResult, + Model model) { if (bindingResult.hasErrors()) { model.addAttribute("errors", bindingResult.getAllErrors()); return "store-edit"; @@ -65,3 +65,4 @@ public class StoreMVCController { return "redirect:/store"; } } + diff --git a/src/main/resources/templates/car-set.html b/src/main/resources/templates/car-set.html new file mode 100644 index 0000000..63d77d2 --- /dev/null +++ b/src/main/resources/templates/car-set.html @@ -0,0 +1,132 @@ + + + + + +
+
+
+
+ + + +
+ +
+ +
+
+
+
+
+ + +
+ + +
+
+ +
+
+ + + + + + + + + + + + + + + + + +
idИмяФамилия
+
+ + + +
+
+ +
+
+
+
+
+ + +
+ + +
+
+ + +
+
+ + + + + + + + + + + + + + + + +
idНазваниеЦена
+
+ + + +
+
+ +
+
+
+
+ +
+ + \ No newline at end of file