Чиним чиним

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;
@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<BuyerDTO> findAllBuyer() {
return buyerService.findAllBuyers()

View File

@ -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();
}

View File

@ -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) {

View File

@ -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<CarDTO> findAllCars(){
return carService.findAllCars()
.stream()
.map(x -> new CarDTO(x))
.toList();
.stream()
.map(x -> new CarDTO(x))
.toList();
}
}

View File

@ -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<BuyerDTO> buyerDTOList;
private List<StoreDTO> storeDTOList;
@ -33,19 +33,24 @@ public class CarDTO {
public String getCarName(){
return carName;
}
public void setCarName(String carName){
this.carName = carName;
}
public List<BuyerDTO> getBuyerDTOList(){
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(){
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}"})
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<BuyerDTO> buyers = buyerService.findAllBuyers().stream()
.map(BuyerDTO::new).toList();
.map(BuyerDTO::new).toList();
List<StoreDTO> 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<BuyerDTO> buyers = buyerService.findAllBuyers().stream()
.map(BuyerDTO::new).toList();
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("carBuyers", carBuyers);
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("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) {

View File

@ -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<BuyerDTO> 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<StoreDTO> findAllStore() {
return storeService.findAllStores()
.stream()
.map(StoreDTO::new)
.toList();
return storeService.findAllStores().stream().map(StoreDTO::new).toList();
}
}

View File

@ -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<CarDTO> 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<CarDTO> getCarDTOList(){
return carDTOList;
}
}

View File

@ -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";
}
}

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>