Чиним чиним

This commit is contained in:
Кашин Максим 2023-05-14 20:39:33 +04:00
parent b9ce279df5
commit f6d23f860d
11 changed files with 220 additions and 87 deletions

Binary file not shown.

View File

@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping(WebConfiguration.REST_API + "/buyer") @RequestMapping("(WebConfiguration.REST_API + /buyer")
public class BuyerController { public class BuyerController {
private final BuyerService buyerService; private final BuyerService buyerService;
public BuyerController(BuyerService buyerService){ public BuyerController(BuyerService buyerService){
@ -39,6 +39,7 @@ public class BuyerController {
return new BuyerDTO(buyerService.findBuyer(id)); return new BuyerDTO(buyerService.findBuyer(id));
} }
@GetMapping @GetMapping
public List<BuyerDTO> findAllBuyer() { public List<BuyerDTO> findAllBuyer() {
return buyerService.findAllBuyers() return buyerService.findAllBuyers()

View File

@ -6,12 +6,12 @@ import jakarta.validation.constraints.NotBlank;
public class BuyerDTO { public class BuyerDTO {
private long id; private long id;
@NotBlank(message = "FirstName can't be null or empty") @NotBlank(message = "buyerFirstName can't be null or empty")
private String buyerFirstName; private String buyerFirstName;
@NotBlank(message = "SecondName can't be null or empty") @NotBlank(message = "buyerSecondName can't be null or empty")
private String buyerSecondName; private String buyerSecondName;
public BuyerDTO(Buyer buyer){ public BuyerDTO(Buyer buyer){
this.id = buyer.getId(); this.id =buyer.getId();
this.buyerFirstName = buyer.getBuyerFirstName(); this.buyerFirstName = buyer.getBuyerFirstName();
this.buyerSecondName = buyer.getBuyerSecondName(); this.buyerSecondName = buyer.getBuyerSecondName();
} }

View File

@ -22,6 +22,7 @@ public class BuyerMVCController {
.toList()); .toList());
return "buyer"; return "buyer";
} }
@GetMapping(value = {"/edit", "/edit/{id}"}) @GetMapping(value = {"/edit", "/edit/{id}"})
public String editBuyer(@PathVariable(required = false) Long id, public String editBuyer(@PathVariable(required = false) Long id,
Model model) { Model model) {

View File

@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping(WebConfiguration.REST_API + "/car") @RequestMapping("WebConfiguration.REST_API + /car")
public class CarController { public class CarController {
private final CarService carService; private final CarService carService;
public CarController(CarService carService){ public CarController(CarService carService){
@ -37,8 +37,8 @@ public class CarController {
@GetMapping @GetMapping
public List<CarDTO> findAllCars(){ public List<CarDTO> findAllCars(){
return carService.findAllCars() return carService.findAllCars()
.stream() .stream()
.map(x -> new CarDTO(x)) .map(x -> new CarDTO(x))
.toList(); .toList();
} }
} }

View File

@ -8,7 +8,7 @@ import java.util.List;
public class CarDTO { public class CarDTO {
private long id; 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 String carName;
private List<BuyerDTO> buyerDTOList; private List<BuyerDTO> buyerDTOList;
private List<StoreDTO> storeDTOList; private List<StoreDTO> storeDTOList;
@ -33,19 +33,24 @@ public class CarDTO {
public String getCarName(){ public String getCarName(){
return carName; return carName;
} }
public void setCarName(String carName){
this.carName = carName;
}
public List<BuyerDTO> getBuyerDTOList(){ public List<BuyerDTO> getBuyerDTOList(){
return buyerDTOList; return buyerDTOList;
} }
public void setBuyerDTOList(List<BuyerDTO> buyers){
this.buyerDTOList = buyers; public void setCarName(String carName) {
this.carName = carName;
} }
public void setBuyerDTOList(List<BuyerDTO> buyerDTOList) {
this.buyerDTOList = buyerDTOList;
}
public void setStoreDTOList(List<StoreDTO> storeDTOList) {
this.storeDTOList = storeDTOList;
}
public List<StoreDTO> getStoreDTOList(){ public List<StoreDTO> getStoreDTOList(){
return storeDTOList; return storeDTOList;
} }
public void setStoreDTOList(List<StoreDTO> stores){
this.storeDTOList = stores;
}
} }

View File

@ -32,11 +32,10 @@ public class CarMVCController {
} }
@GetMapping(value = {"/edit", "/edit/{id}"}) @GetMapping(value = {"/edit", "/edit/{id}"})
public String editCar(@PathVariable(required = false) Long 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) { if (id == null || id <= 0) {
List<BuyerDTO> buyers = buyerService.findAllBuyers().stream() List<BuyerDTO> buyers = buyerService.findAllBuyers().stream()
.map(BuyerDTO::new).toList(); .map(BuyerDTO::new).toList();
List<StoreDTO> stores = storeService.findAllStores().stream() List<StoreDTO> stores = storeService.findAllStores().stream()
.map(StoreDTO::new).toList(); .map(StoreDTO::new).toList();
model.addAttribute("CarDTO", new CarDTO()); model.addAttribute("CarDTO", new CarDTO());
@ -44,75 +43,73 @@ public class CarMVCController {
model.addAttribute("stores", stores); model.addAttribute("stores", stores);
return "car-create"; return "car-create";
} else { } else {
if(PW.equals("N")) { String name = carService.findCar(id).getCarName();
model.addAttribute("carId", id);
model.addAttribute("CarDTO", new CarDTO(carService.findCar(id)));
return "car-create";
}
if(PW.equals("W")) {
List<BuyerDTO> buyers = buyerService.findAllBuyers().stream() List<BuyerDTO> buyers = buyerService.findAllBuyers().stream()
.map(BuyerDTO::new).toList(); .map(BuyerDTO::new).toList();
List<BuyerDTO> carBuyers = carService List<BuyerDTO> carBuyers = carService
.findCar(id).getBuyers().stream().map(BuyerDTO::new).toList(); .findCar(id)
.getBuyers()
.stream()
.map(BuyerDTO::new)
.toList();
List<StoreDTO> stores = storeService.findAllStores().stream()
.map(StoreDTO::new).toList();
List<StoreDTO> carStores = carService
.findCar(id)
.getStores()
.stream()
.map(StoreDTO::new)
.toList();
model.addAttribute("name", name);
model.addAttribute("carId", id); model.addAttribute("carId", id);
model.addAttribute("carBuyers", carBuyers); model.addAttribute("carBuyers", carBuyers);
model.addAttribute("buyers", buyers); model.addAttribute("buyers", buyers);
return "car-buyer";
}
if(PW.equals("P")) {
List<StoreDTO> stores = storeService.findAllStores().stream()
.map(StoreDTO::new).toList();
List<StoreDTO> carStores = carService
.findCar(id).getStores().stream().map(StoreDTO::new).toList();
model.addAttribute("carId", id);
model.addAttribute("carStores", carStores); model.addAttribute("carStores", carStores);
model.addAttribute("stores", stores); 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, public String saveCar(@ModelAttribute @Valid CarDTO carDTO,
BindingResult bindingResult, BindingResult bindingResult,
Model model) { Model model) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors()); model.addAttribute("errors", bindingResult.getAllErrors());
return "car-create"; return "car-create";
} }
carService.addCar(carDTO); carService.addCar(carDTO);
// } else {
// carService.updateCar(id, carDTO);
// }
return "redirect:/car"; return "redirect:/car";
} }
@PostMapping(value = {"/{id}"}) @PostMapping(value = {"/{id}"})
public String editCarName(@PathVariable Long id, public String editCar(@PathVariable Long id,
@RequestParam("carName") String name){ @RequestParam("PW") String PW,
carService.findCar(id).setCarName(name); @RequestParam("wpName") String name,
carService.updateCar(id, new CarDTO(carService.findCar(id))); @RequestParam(value="idPW", required = false) Long idPW,
return "redirect:/car/"; @RequestParam(value = "delete", required = false) boolean del,
} Model model) {
@PostMapping(value = {"/{id}/buyer"}) if(PW.equals("N")){
public String editBuyerCar(@PathVariable Long id, carService.findCar(id).setCarName(name);
@RequestParam("buyer") Long buyerId, carService.updateCar(id, new CarDTO(carService.findCar(id)));
@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);
} }
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}") @PostMapping("/delete/{id}")
public String deleteCar(@PathVariable Long id) { public String deleteCar(@PathVariable Long id) {

View File

@ -8,7 +8,7 @@ import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping(WebConfiguration.REST_API + "/store") @RequestMapping("WebConfiguration.REST_API + /store")
public class StoreController { public class StoreController {
private final StoreService storeService; private final StoreService storeService;
public StoreController(StoreService storeService){ public StoreController(StoreService storeService){
@ -40,16 +40,10 @@ public class StoreController {
} }
@GetMapping("/buyer/{id}") @GetMapping("/buyer/{id}")
public List<BuyerDTO> findBuyersOnCar(@PathVariable Long id){ public List<BuyerDTO> findBuyersOnCar(@PathVariable Long id){
return storeService.findAllBuyersProducedStore(id) return storeService.findAllBuyersProducedStore(id).stream().map(BuyerDTO::new).toList();
.stream()
.map(BuyerDTO::new)
.toList();
} }
@GetMapping @GetMapping
public List<StoreDTO> findAllStore() { public List<StoreDTO> findAllStore() {
return storeService.findAllStores() return storeService.findAllStores().stream().map(StoreDTO::new).toList();
.stream()
.map(StoreDTO::new)
.toList();
} }
} }

View File

@ -9,7 +9,7 @@ import java.util.List;
public class StoreDTO { public class StoreDTO {
private long id; 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 String storeName;
private List<CarDTO> carDTOList; private List<CarDTO> carDTOList;
public StoreDTO(Store store){ public StoreDTO(Store store){
@ -18,7 +18,7 @@ public class StoreDTO {
this.carDTOList = store.getCar() == null ? null : store.getCar() this.carDTOList = store.getCar() == null ? null : store.getCar()
.stream() .stream()
.filter(x -> x.getStores().contains(store.getId())) .filter(x -> x.getStores().contains(store.getId()))
.map(y -> new CarDTO()) .map(y -> new CarDTO(y))
.toList(); .toList();
} }
public StoreDTO() { public StoreDTO() {
@ -33,7 +33,9 @@ public class StoreDTO {
public void setStoreName(String storeName){ public void setStoreName(String storeName){
this.storeName = storeName; this.storeName = storeName;
} }
public List<CarDTO> getCarDTOList(){ public List<CarDTO> getCarDTOList(){
return carDTOList; return carDTOList;
} }
} }

View File

@ -23,7 +23,7 @@ public class StoreMVCController {
return "store"; return "store";
} }
@GetMapping("/info/{id}") @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("cathegory", "Кто производит " + storeService.findStore(id).getStoreName());
model.addAttribute("buyers", storeService.findAllBuyersProducedStore(id) model.addAttribute("buyers", storeService.findAllBuyersProducedStore(id)
.stream() .stream()
@ -34,7 +34,7 @@ public class StoreMVCController {
} }
@GetMapping(value = {"/edit", "/edit/{id}"}) @GetMapping(value = {"/edit", "/edit/{id}"})
public String editStore(@PathVariable(required = false) Long id, public String editStore(@PathVariable(required = false) Long id,
Model model) { Model model) {
if (id == null || id <= 0) { if (id == null || id <= 0) {
model.addAttribute("StoreDTO", new StoreDTO()); model.addAttribute("StoreDTO", new StoreDTO());
} else { } else {
@ -43,11 +43,11 @@ public class StoreMVCController {
} }
return "store-edit"; return "store-edit";
} }
@PostMapping(value = {"/", "/{id}"}) @PostMapping(value = {"", "/{id}"})
public String saveStore(@PathVariable(required = false) Long id, public String saveStore(@PathVariable(required = false) Long id,
@ModelAttribute @Valid StoreDTO storeDTO, @ModelAttribute @Valid StoreDTO storeDTO,
BindingResult bindingResult, BindingResult bindingResult,
Model model) { Model model) {
if (bindingResult.hasErrors()) { if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors()); model.addAttribute("errors", bindingResult.getAllErrors());
return "store-edit"; return "store-edit";
@ -65,3 +65,4 @@ public class StoreMVCController {
return "redirect:/store"; return "redirect:/store";
} }
} }

View File

@ -0,0 +1,132 @@
<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ultraq.net.nz/thymeleaf/layout " xmlns:th="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<div layout:fragment="content" class="mw-100">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3">
<div class="d-flex justify-content-center">
<input name="PW" type="hidden" th:value="N" />
<label for="wpName" class="form-label mt-2">Название</label>
<input type="text" class="d-flex justify-content-center form-control" id="wpName" style="width: 30vw; margin-left: 2vw;" th:value="${name}" name="wpName"/>
<div style="margin-left: 2vw;">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Обновить</span>
</button>
</div>
<div style="margin-left: 2vw;">
<a class="btn btn-outline-dark text-center button-fixed" th:href="@{/car}">
Назад
</a>
</div>
</div>
</form>
<div class="d-flex container-fluid ">
<div style="width: 45vw; margin-right: 2vw" class="container-fluid">
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" >
<input name="wpName" type="hidden" th:value="${name}" />
<input name="PW" type="hidden" th:value="W" />
<div class="mb-3">
<label for="idW" class="form-label">Покупатель</label>
<select class="form-select" id = "idW" th:name="idPW">
<option th:each="value: ${buyers}"
th:value="${value.id}"
th:text="${value.buyerFirstName} + ' ' + ${value.buyerSecondName}">
</option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Добавить</span>
</button>
</div>
</form>
<table class="table" id="tbl-items">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Имя</th>
<th scope="col">Фамилия</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="carBuyer, iterator: ${carBuyers}">
<td th:text="${iterator.index} + 1"></td>
<td th:text="${carBuyer.buyerFirstName}"></td>
<td th:text="${carBuyer.buyerSecondName}"></td>
<td>
<div>
<a type="button" class="btn btn-outline-dark text-center button-fixed"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${carBuyer.id}').click()|">
<i class="fa fa-trash"></i>
</a>
</div>
<form th:action="@{'/car/' + ${id} + '?PW=W&wpName='+ ${name} +'&idPW=' + ${carBuyer.id} + '&delete=true'}" method="post">
<button th:id="'remove-' + ${carBuyer.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
<div style="width: 45vw; margin-left: 2vw" class="container-fluid">
<form action="#" th:action="@{/car/{id}(id=${id})}" method="post" class="mb-3">
<input name="wpName" type="hidden" th:value="${name}" />
<input type="hidden" th:value="P" name="PW"/>
<div class="mb-3">
<label for="idP" class="form-label">Магазин</label>
<select class="form-select" id = "idP" th:name="idPW">
<option th:each="store, iterator: ${stores}"
th:value="${store.id}"
th:text="${store.storeName}">
</option>
</select>
</div>
<div class="mb-3">
<button type="submit" class="btn btn-outline-dark text-center button-fixed">
<span>Добавить</span>
</button>
</div>
</form>
<table class="table" id="tbl-items">
<thead>
<tr>
<th scope="col">id</th>
<th scope="col">Название</th>
<th scope="col">Цена</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="carStore, iterator: ${carStores}">
<td th:text="${iterator.index} + 1"></td>
<td th:text="${carStore.storeName}"></td>
<td>
<div>
<a type="button" class="btn btn-outline-dark text-center button-fixed"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${carStore.id}').click()|">
<i class="fa fa-trash"></i>
</a>
</div>
<form th:action="@{'/car/' + ${id} + '?PW=P&wpName='+ ${name} + '&idPW=' + ${carStore.id} + '&delete=true'}" method="post">
<button class = "btn btn-outline-dark text-center button-fixed" th:id="'remove-' + ${carStore.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</body>
</html>