Пятая лабораторная работа. Начала работы с MVC + CustomerMVCController.

This commit is contained in:
abazov73 2023-05-02 01:29:14 +04:00
parent d29a9a5e25
commit 4a27990626
26 changed files with 645 additions and 73 deletions

View File

@ -23,6 +23,13 @@ dependencies {
testImplementation 'org.springframework.boot:spring-boot-starter-test' testImplementation 'org.springframework.boot:spring-boot-starter-test'
implementation 'org.hibernate.validator:hibernate-validator' implementation 'org.hibernate.validator:hibernate-validator'
implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
implementation 'org.springframework.boot:spring-boot-devtools'
implementation 'nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect'
implementation 'org.webjars:bootstrap:5.1.3'
implementation 'org.webjars:jquery:3.6.0'
implementation 'org.webjars:font-awesome:6.1.0'
} }
tasks.named('test') { tasks.named('test') {

Binary file not shown.

View File

@ -3,12 +3,14 @@ package com.example.ipLab.StoreDataBase.Controllers;
import com.example.ipLab.StoreDataBase.DTO.CustomerDTO; import com.example.ipLab.StoreDataBase.DTO.CustomerDTO;
import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Customer;
import com.example.ipLab.StoreDataBase.Service.CustomerService; import com.example.ipLab.StoreDataBase.Service.CustomerService;
import com.example.ipLab.WebConfiguration;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/customer") @RequestMapping(WebConfiguration.REST_API + "/customer")
public class CustomerController { public class CustomerController {
private final CustomerService customerService; private final CustomerService customerService;
@ -29,19 +31,15 @@ public class CustomerController {
} }
@PostMapping @PostMapping
public CustomerDTO createCustomer(@RequestParam("customerLastName") String customerLastName, public CustomerDTO createCustomer(@RequestBody @Valid CustomerDTO customerDTO){
@RequestParam("customerFirstName") String customerFirstName, final Customer customer = customerService.addCustomer(customerDTO.getlastName(), customerDTO.getfirstName(), customerDTO.getmiddleName());
@RequestParam("customerMiddleName") String customerMiddleName){
final Customer customer = customerService.addCustomer(customerLastName, customerFirstName, customerMiddleName);
return new CustomerDTO(customer); return new CustomerDTO(customer);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public CustomerDTO updateCustomer(@RequestParam("customerLastName") String customerLastName, public CustomerDTO updateCustomer(@RequestBody @Valid CustomerDTO customerDTO,
@RequestParam("customerFirstName") String customerFirstName,
@RequestParam("customerMiddleName") String customerMiddleName,
@PathVariable Long id){ @PathVariable Long id){
return new CustomerDTO(customerService.updateCustomer(id, customerLastName, customerFirstName, customerMiddleName)); return new CustomerDTO(customerService.updateCustomer(id, customerDTO.getlastName(), customerDTO.getfirstName(), customerDTO.getmiddleName()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -5,12 +5,14 @@ import com.example.ipLab.StoreDataBase.Model.Ordered;
import com.example.ipLab.StoreDataBase.Service.CustomerService; import com.example.ipLab.StoreDataBase.Service.CustomerService;
import com.example.ipLab.StoreDataBase.Service.OrderService; import com.example.ipLab.StoreDataBase.Service.OrderService;
import com.example.ipLab.StoreDataBase.Service.ProductService; import com.example.ipLab.StoreDataBase.Service.ProductService;
import com.example.ipLab.WebConfiguration;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/order") @RequestMapping(WebConfiguration.REST_API + "/order")
public class OrderedController { public class OrderedController {
private final OrderService orderedService; private final OrderService orderedService;
private final ProductService productService; private final ProductService productService;
@ -35,17 +37,15 @@ public class OrderedController {
} }
@PostMapping @PostMapping
public OrderedDTO createOrdered(@RequestParam("productId") Long productId, public OrderedDTO createOrdered(@RequestBody @Valid OrderedDTO orderedDTO){
@RequestParam("customerId") Long customerId, final Ordered ordered = orderedService.addOrder(productService.getProduct(orderedDTO.getProductId()), customerService.getCustomer(orderedDTO.getCustomerId()), orderedDTO.quantity);
@RequestParam("quantity") Integer quantity){
final Ordered ordered = orderedService.addOrder(productService.getProduct(productId), customerService.getCustomer(customerId), quantity);
return new OrderedDTO(ordered); return new OrderedDTO(ordered);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public OrderedDTO updateOrdered(@RequestParam("quantity") Integer quantity, public OrderedDTO updateOrdered(@RequestBody @Valid OrderedDTO orderedDTO,
@PathVariable Long id){ @PathVariable Long id){
return new OrderedDTO(orderedService.updateOrder(id, quantity)); return new OrderedDTO(orderedService.updateOrder(id, orderedDTO.quantity));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -3,12 +3,14 @@ package com.example.ipLab.StoreDataBase.Controllers;
import com.example.ipLab.StoreDataBase.DTO.ProductDTO; import com.example.ipLab.StoreDataBase.DTO.ProductDTO;
import com.example.ipLab.StoreDataBase.Model.Product; import com.example.ipLab.StoreDataBase.Model.Product;
import com.example.ipLab.StoreDataBase.Service.ProductService; import com.example.ipLab.StoreDataBase.Service.ProductService;
import com.example.ipLab.WebConfiguration;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/product") @RequestMapping(WebConfiguration.REST_API + "/product")
public class ProductController { public class ProductController {
private final ProductService productService; private final ProductService productService;
@ -43,15 +45,15 @@ public class ProductController {
} }
@PostMapping @PostMapping
public ProductDTO createProduct(@RequestParam("productName") String productName){ public ProductDTO createProduct(@RequestBody @Valid ProductDTO productDTO){
final Product product = productService.addProduct(productName); final Product product = productService.addProduct(productDTO.getName());
return new ProductDTO(product); return new ProductDTO(product);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public ProductDTO updateProduct(@RequestParam("productName") String productName, public ProductDTO updateProduct(@RequestBody @Valid ProductDTO productDTO,
@PathVariable Long id){ @PathVariable Long id){
return new ProductDTO(productService.updateProduct(id, productName)); return new ProductDTO(productService.updateProduct(id, productDTO.getName()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -7,12 +7,14 @@ import com.example.ipLab.StoreDataBase.Model.Customer;
import com.example.ipLab.StoreDataBase.Model.Store; import com.example.ipLab.StoreDataBase.Model.Store;
import com.example.ipLab.StoreDataBase.Service.CustomerService; import com.example.ipLab.StoreDataBase.Service.CustomerService;
import com.example.ipLab.StoreDataBase.Service.StoreService; import com.example.ipLab.StoreDataBase.Service.StoreService;
import com.example.ipLab.WebConfiguration;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.List; import java.util.List;
@RestController @RestController
@RequestMapping("/store") @RequestMapping(WebConfiguration.REST_API + "/store")
public class StoreController { public class StoreController {
private final StoreService storeService; private final StoreService storeService;
@ -33,21 +35,21 @@ public class StoreController {
} }
@PostMapping @PostMapping
public StoreDTO createStore(@RequestParam("storeName") String storeName){ public StoreDTO createStore(@RequestBody @Valid StoreDTO storeDTO){
final Store store = storeService.addStore(storeName); final Store store = storeService.addStore(storeDTO.getStoreName());
return new StoreDTO(store); return new StoreDTO(store);
} }
@PutMapping("/{id}") @PutMapping("/{id}")
public StoreDTO updateStore(@RequestParam("storeName") String storeName, public StoreDTO updateStore(@RequestBody @Valid StoreDTO storeDTO,
@PathVariable Long id){ @PathVariable Long id){
return new StoreDTO(storeService.updateStore(id, storeName)); return new StoreDTO(storeService.updateStore(id, storeDTO.getStoreName()));
} }
@PutMapping("/add") @PutMapping("{id}/add")
public ProductDTO addProduct(@RequestParam("storeId") Long storeId, public ProductDTO addProduct(@PathVariable Long id,
@RequestParam("productId") Long productId){ @RequestBody @Valid CustomerDTO customerDTO){
return new ProductDTO(storeService.addProduct(storeId, productId)); return new ProductDTO(storeService.addProduct(id, customerDTO.getId()));
} }
@DeleteMapping("/{id}") @DeleteMapping("/{id}")

View File

@ -1,14 +1,20 @@
package com.example.ipLab.StoreDataBase.DTO; package com.example.ipLab.StoreDataBase.DTO;
import com.example.ipLab.StoreDataBase.Model.Customer; import com.example.ipLab.StoreDataBase.Model.Customer;
import jakarta.validation.constraints.NotBlank;
import java.util.List;
public class CustomerDTO { public class CustomerDTO {
public final Long id; public Long id;
public final String lastName; @NotBlank(message = "lastName can't be null or empty")
public final String firstName; public String lastName;
public final String middleName; @NotBlank(message = "firstName can't be null or empty")
public String firstName;
@NotBlank(message = "middleName can't be null or empty")
public String middleName;
public CustomerDTO(){
}
public CustomerDTO(Customer customer){ public CustomerDTO(Customer customer){
this.id = customer.getId(); this.id = customer.getId();
@ -20,16 +26,25 @@ public class CustomerDTO {
public Long getId() { public Long getId() {
return id; return id;
} }
public void setId(Long id) {
public String getLastname() { this.id = id;
}
public String getlastName() {
return lastName; return lastName;
} }
public void setlastName(String lastName) {
public String getFirstname() { this.lastName = lastName;
}
public String getfirstName() {
return firstName; return firstName;
} }
public void setfirstName(String firstName) {
public String getMiddleName() { this.firstName = firstName;
}
public String getmiddleName() {
return middleName; return middleName;
} }
public void setmiddleName(String middleName) {
this.middleName = middleName;
}
} }

View File

@ -3,11 +3,17 @@ package com.example.ipLab.StoreDataBase.DTO;
import com.example.ipLab.StoreDataBase.Model.Ordered; import com.example.ipLab.StoreDataBase.Model.Ordered;
public class OrderedDTO { public class OrderedDTO {
public final Long id; public Long id;
public final int quantity; public int quantity;
public final String productName; public String productName;
public final String customerFIO; public String customerFIO;
public final String storeName; public String storeName;
public Long customerId;
public Long productId;
public OrderedDTO(){
}
public OrderedDTO(Ordered ordered){ public OrderedDTO(Ordered ordered){
this.id = ordered.getId(); this.id = ordered.getId();
@ -15,6 +21,8 @@ public class OrderedDTO {
this.productName = ordered.getProduct().getName(); this.productName = ordered.getProduct().getName();
this.storeName = ordered.getProduct().getStore().getStoreName(); this.storeName = ordered.getProduct().getStore().getStoreName();
this.customerFIO = ordered.getCustomer().getLastName() + " " + ordered.getCustomer().getFirstName() + " " + ordered.getCustomer().getMiddleName(); this.customerFIO = ordered.getCustomer().getLastName() + " " + ordered.getCustomer().getFirstName() + " " + ordered.getCustomer().getMiddleName();
this.customerId = ordered.getProduct().getId();
this.productId = ordered.getProduct().getId();
} }
public Long getId() { public Long getId() {
@ -36,4 +44,12 @@ public class OrderedDTO {
public String getStoreName() { public String getStoreName() {
return storeName; return storeName;
} }
public Long getCustomerId() {
return customerId;
}
public Long getProductId() {
return productId;
}
} }

View File

@ -5,9 +5,12 @@ import com.example.ipLab.StoreDataBase.Model.Product;
import java.util.List; import java.util.List;
public class ProductDTO { public class ProductDTO {
public final Long id; public Long id;
public final String productName; public String productName;
public final String storeName; public String storeName;
public ProductDTO(){
}
public ProductDTO(Product product){ public ProductDTO(Product product){
this.id = product.getId(); this.id = product.getId();

View File

@ -5,9 +5,13 @@ import com.example.ipLab.StoreDataBase.Model.Store;
import java.util.List; import java.util.List;
public class StoreDTO { public class StoreDTO {
public final Long id; public Long id;
public final String storeName; public String storeName;
public final List<ProductDTO> products; public List<ProductDTO> products;
public StoreDTO(){
}
public StoreDTO(Store store){ public StoreDTO(Store store){
this.id = store.getId(); this.id = store.getId();

View File

@ -0,0 +1,63 @@
package com.example.ipLab.StoreDataBase.MVC;
import com.example.ipLab.StoreDataBase.DTO.CustomerDTO;
import com.example.ipLab.StoreDataBase.Service.CustomerService;
import jakarta.validation.Valid;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
@Controller
@RequestMapping("/customer")
public class CustomerMVCController {
private final CustomerService customerService;
public CustomerMVCController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping
public String getCustomers(Model model) {
model.addAttribute("customers",
customerService.getAllCustomers().stream()
.map(CustomerDTO::new)
.toList());
return "customer";
}
@GetMapping(value = {"/edit/", "/edit/{id}"})
public String editCustomer(@PathVariable(required = false) Long id,
Model model) {
if (id == null || id <= 0) {
model.addAttribute("customerDTO", new CustomerDTO());
} else {
model.addAttribute("customerId", id);
model.addAttribute("customerDTO", new CustomerDTO(customerService.getCustomer(id)));
}
return "customer-edit";
}
@PostMapping(value = {"/", "/{id}"})
public String saveCustomer(@PathVariable(required = false) Long id,
@ModelAttribute @Valid CustomerDTO customerDto,
BindingResult bindingResult,
Model model) {
if (bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "customer-edit";
}
if (id == null || id <= 0) {
customerService.addCustomer(customerDto.getlastName(), customerDto.getfirstName(), customerDto.getmiddleName());
} else {
customerService.updateCustomer(id, customerDto.getlastName(), customerDto.getfirstName(), customerDto.getmiddleName());
}
return "redirect:/customer";
}
@PostMapping("/delete/{id}")
public String deleteCustomer(@PathVariable Long id) {
customerService.deleteCustomer(id);
return "redirect:/customer";
}
}

View File

@ -11,10 +11,11 @@ import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ControllerAdvice @ControllerAdvice(annotations = RestController.class)
public class AdviceController { public class AdviceController {
@ExceptionHandler({ @ExceptionHandler({
CustomerNotFoundException.class, CustomerNotFoundException.class,

View File

@ -6,6 +6,7 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration @Configuration
public class WebConfiguration implements WebMvcConfigurer { public class WebConfiguration implements WebMvcConfigurer {
public static final String REST_API = "/api";
@Override @Override
public void addCorsMappings(CorsRegistry registry){ public void addCorsMappings(CorsRegistry registry){
registry.addMapping("/**").allowedMethods("*"); registry.addMapping("/**").allowedMethods("*");

View File

@ -0,0 +1,284 @@
header{
background-color: #9094c1;
flex-direction: row;
}
header nav{
font-family: Segoe UI;
font-weight: bold;
font-size: 42px;
}
header nav a{
color: inherit;
}
header nav a:hover{
text-decoration: underline;
}
.navigationCaption{
font-family: Segoe UI;
font-weight: bold;
font-size: 42px;
}
.headNav{
color: inherit;
}
a{
color: inherit;
text-decoration: none;
}
td, th{
border: 1px solid black;
font-family: Segoe UI;
text-align: center;
font-size: 28px;
}
table tbody td a:hover{
text-decoration: underline;
color: inherit;
}
footer{
background-color: #707be5;
max-height: 110px;
}
.mainPage a:hover{
text-decoration: underline;
color: inherit;
}
.popularCaption{
font-family: Segoe UI;
font-size: 24px;
}
.discountsCaption{
font-family: Segoe UI;
font-size: 24px;
}
.item{
font-family: Segoe UI;
font-size: 18px;
}
.item img{
width: 200px;
height: 200px;
}
.tableMy table caption{
font-family: Segoe UI;
font-weight: bold;
font-size: 45px;
}
.tableMy table tbody td a:hover{
text-decoration: underline;
color: inherit !important;
}
.tableCart table caption{
font-family: Segoe UI;
font-weight: bold;
font-size: 32px;
}
.cartInfo{
margin-top: 320px;
margin-right: 400px;
font-family: Segoe UI;
font-size: 45px;
}
.buttonOrder{
background-color: #4d89d9;
}
.itemCaption{
font-family: Segoe UI;
font-size: 32px;
}
#itemPhoto{
margin-left: 50px;
}
.itemInfo{
font-family: Segoe UI;
font-size: 45px;
margin-left: 85px;
}
.itemInfo li{
list-style-type: none;
}
.buttonAdd{
font-family: Segoe UI;
font-size: 45px;
background-color: #4d89d9;
margin-left: 35px;
}
.companyName{
font-family: Segoe UI;
font-size: 45px;
}
.longText{
font-family: Segoe UI;
font-size: 25px;
}
#logoName{
font-family: Rockwell Condensed;
font-size: 64px;
font-weight: bold;
font-stretch: condensed;
text-decoration: none;
}
#logoName a:hover{
text-decoration: none;
color: inherit;
}
.footerLeft{
margin-bottom: 10px;
font-family: Segoe UI;
font-size: 16px;
}
.footerBottom{
font-family: Segoe UI;
font-size: 28px;
}
.hide{
display: none;
}
.active{
display: block;
}
.active img{
width: 200px;
height: 100px;
}
@media (max-width: 1080px){
header{
flex-direction: column;
}
header nav{
text-align: center;
}
.headerContainer{
flex-direction: column !important;
}
.itemContent{
flex-direction: column !important;
justify-content: center !important;
}
#itemPhoto{
margin-left: auto !important;
margin-right: auto ;
}
.itemInfo{
margin-bottom: 10px !important;
margin-left: 10px!important;
}
#cartContainer{
flex-direction: column !important;
}
.cartInfo{
margin-top: 0px !important;
margin-left: 5px;
margin-right: auto;
margin-bottom: 10px;
}
#tableCart{
width: auto;
}
.mainPage{
flex-direction: column !important;
justify-content: center;
}
.tablePopular{
margin-left: auto !important;
margin-right: auto !important;
}
.tableDiscounts{
margin-top: 30px;
margin-left: auto !important;
margin-right: auto !important;
}
}
@media (max-width: 767px){
.tableMy table thead th{
font-size: 12px !important;
}
.tableMy table tr td{
font-size: 12px !important;
}
.tableCart table thead th{
font-size: 18px !important;
}
.tableCart table tr td{
font-size: 18px !important;
}
.cartInfo{
font-size: 18px !important;
}
.itemInfo{
font-size: 18px !important;
}
.buttonAdd{
font-size: 18px !important
}
.footerLeft{
font-size: 12px !important;
}
.footerBottom{
font-size: 16px !important;
}
.footerRight img{
width: 55px;
height: 27px;
}
.mainPage img{
width: 100px !important;
height: 100px !important;
}
.popularCaption{
font-size: 18px !important;
}
.discountsCaption{
font-size: 18px !important;
}
#itemIcon{
width: 250px !important;
height: 250px !important;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

View File

@ -0,0 +1,35 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div th:text="${errors}" class="margin-bottom alert-danger"></div>
<form action="#" th:action="@{/customer/{id}(id=${id})}" th:object="${customerDTO}" method="post">
<div class="mb-3">
<label for="lastName" class="form-label">Фамилия</label>
<input type="text" class="form-control" id="lastName" th:field="${customerDTO.lastName}" required="true">
</div>
<div class="mb-3">
<label for="firstName" class="form-label">Имя</label>
<input type="text" class="form-control" id="firstName" th:field="${customerDTO.firstName}" required="true">
</div>
<div class="mb-3">
<label for="middleName" class="form-label">Отчество</label>
<input type="text" class="form-control" id="middleName" th:field="${customerDTO.middleName}" required="true">
</div>
<div class="mb-3">
<button type="submit" class="btn btn-primary button-fixed">
<span th:if="${id == null}">Добавить</span>
<span th:if="${id != null}">Обновить</span>
</button>
<a class="btn btn-secondary button-fixed" th:href="@{/customer}">
Назад
</a>
</div>
</form>
</div>
</body>
</html>

View File

@ -0,0 +1,55 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div>
<a class="btn btn-success button-fixed"
th:href="@{/customer/edit/}">
<i class="fa-solid fa-plus"></i> Добавить
</a>
</div>
<div class="table-responsive">
<table class="table table-success table-hover">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Фамилия</th>
<th scope="col">Имя</th>
<th scope="col">Отчество</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<tr th:each="customer, iterator: ${customers}">
<th scope="row" th:text="${iterator.index} + 1"/>
<td th:text="${customer.lastName}"/>
<td th:text="${customer.firstName}"/>
<td th:text="${customer.middleName}"/>
<td style="width: 10%">
<div class="btn-group" role="group" aria-label="Basic example">
<a class="btn btn-warning button-fixed button-sm"
th:href="@{/customer/edit/{id}(id=${customer.id})}">
<i class="fa fa-pencil" aria-hidden="true"></i> Изменить
</a>
<button type="button" class="btn btn-danger button-fixed button-sm"
th:attr="onclick=|confirm('Удалить запись?') && document.getElementById('remove-${customer.id}').click()|">
<i class="fa fa-trash" aria-hidden="true"></i> Удалить
</button>
</div>
<form th:action="@{/customer/delete/{id}(id=${customer.id})}" method="post">
<button th:id="'remove-' + ${customer.id}" type="submit" style="display: none">
Удалить
</button>
</form>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>

View File

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="ru"
xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
<meta charset="UTF-8"/>
<title>IP Example</title>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<link rel="icon" href="/favicon.ico">
<script type="text/javascript" src="/webjars/bootstrap/5.1.3/js/bootstrap.bundle.min.js"></script>
<link rel="stylesheet" href="/webjars/bootstrap/5.1.3/css/bootstrap.min.css"/>
<link rel="stylesheet" href="/webjars/font-awesome/6.1.0/css/all.min.css"/>
<link rel="stylesheet" href="/css/styleSite.css"/>
</head>
<body>
<header>
<div class="d-flex flex-row headerContainer">
<nav class="navbar navbar-expand-md">
<div class="container-fluid" id="navigationMenu">
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"
aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="navbar-collapse collapse justify-content-end" id="navbarNav">
<ul class="navbar-nav" id="headerNavigation">
<a class="nav-link headNav" href="/customer"
th:classappend="${#strings.equals(activeLink, '/customer')} ? 'active' : ''">Клиенты</a>
<a class="nav-link headNav" href="/store"
th:classappend="${#strings.equals(activeLink, '/store')} ? 'active' : ''">Магазины</a>
<a class="nav-link headNav" href="/product"
th:classappend="${#strings.equals(activeLink, '/product')} ? 'active' : ''">Товары</a>
<a class="nav-link headNav" href="/order"
th:classappend="${#strings.equals(activeLink, '/order')} ? 'active' : ''">Заказы</a>
</ul>
</div>
</div>
</nav>
</div>
</header>
<div class="container-fluid">
<div class="container container-padding" layout:fragment="content">
</div>
</div>
</body>
<th:block layout:fragment="scripts">
</th:block>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div><span th:text="${error}"></span></div>
<a href="/">На главную</a>
</div>
</body>
</html>

View File

@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
layout:decorate="~{default}">
<head>
</head>
<body>
<div layout:fragment="content">
<div>It's works!</div>
<a href="123">ERROR</a>
</div>
</body>
</html>

View File

@ -23,14 +23,15 @@ export default function CustomerTable(props){
} }
function saveItem() { function saveItem() {
let customer = {
lastName: props.data.lastName,
firstName: props.data.firstName,
middleName: props.data.middleName
}
if (!isEdit) { if (!isEdit) {
DataService.create(props.url, "?customerLastName=" + props.data.lastName DataService.create(props.url, customer).then(() => loadItems());
+ "&customerFirstName=" + props.data.firstName
+ "&customerMiddleName=" + props.data.middleName).then(() => loadItems());
} else { } else {
DataService.update(props.getUrl + props.data.id, "?customerLastName=" + props.data.lastName DataService.update(props.getUrl + props.data.id, customer).then(() => loadItems());
+ "&customerFirstName=" + props.data.firstName
+ "&customerMiddleName=" + props.data.middleName).then(() => loadItems());
} }
} }

View File

@ -28,10 +28,15 @@ export default function CustomerTable(props){
} }
function saveItem() { function saveItem() {
let ordered = {
productId: props.data.productId,
customerId: props.data.customerId,
quantity: props.data.quantity
}
if (!isEdit) { if (!isEdit) {
DataService.create(props.url, "?productId=" + props.data.productId + "&customerId=" + props.data.customerId + "&quantity=" + props.data.quantity).then(() => loadItems()); DataService.create(props.url, ordered).then(() => loadItems());
} else { } else {
DataService.update(props.getUrl + props.data.id, "?productId=" + props.data.productId + "&customerId=" + props.data.customerId + "&quantity=" + props.data.quantity).then(() => loadItems()); DataService.update(props.getUrl + props.data.id, ordered).then(() => loadItems());
} }
} }

View File

@ -23,10 +23,13 @@ export default function CustomerTable(props){
} }
function saveItem() { function saveItem() {
let product = {
productName: props.data.productName
}
if (!isEdit) { if (!isEdit) {
DataService.create(props.url, "?productName=" + props.data.productName).then(() => loadItems()); DataService.create(props.url, product).then(() => loadItems());
} else { } else {
DataService.update(props.getUrl + props.data.id, "?productName=" + props.data.productName).then(() => loadItems()); DataService.update(props.getUrl + props.data.id, product).then(() => loadItems());
} }
} }

View File

@ -23,10 +23,13 @@ export default function CustomerTable(props){
} }
function saveItem() { function saveItem() {
let store = {
storeName: props.data.storeName
}
if (!isEdit) { if (!isEdit) {
DataService.create(props.url, "?storeName=" + props.data.storeName).then(() => loadItems()); DataService.create(props.url, store).then(() => loadItems());
} else { } else {
DataService.update(props.getUrl + props.data.id, "?storeName=" + props.data.storeName).then(() => loadItems()); DataService.update(props.getUrl + props.data.id, store).then(() => loadItems());
} }
} }

View File

@ -6,7 +6,7 @@ import { useState, useEffect} from "react";
export default function AddToStorePage(){ export default function AddToStorePage(){
const getStoreUrl = 'store'; const getStoreUrl = 'store';
const getProductUrl = 'product/getWithoutStores' const getProductUrl = 'product/getWithoutStores'
const url = 'store/add' const url = 'store/'
const [storeOptions, setStoreOptions] = useState([]) const [storeOptions, setStoreOptions] = useState([])
const [productOptions, setProductOptions] = useState([]) const [productOptions, setProductOptions] = useState([])
const transformerProduct = (data) => new Product(data); const transformerProduct = (data) => new Product(data);
@ -44,8 +44,10 @@ export default function AddToStorePage(){
function add(){ function add(){
var storeId = document.getElementById("storeId").value; var storeId = document.getElementById("storeId").value;
var productId = document.getElementById("productId").value; var productId = document.getElementById("productId").value;
let product = {
DataService.update(url, "?storeId=" + storeId + "&productId=" + productId); id: productId
}
DataService.update(url + storeId + "/add", product);
window.location.replace("/product"); window.location.replace("/product");
} }

View File

@ -13,7 +13,7 @@ function toJSON(data) {
} }
export default class DataService { export default class DataService {
static dataUrlPrefix = 'http://localhost:8080/'; static dataUrlPrefix = 'http://localhost:8080/api/';
static async readAll(url, transformer) { static async readAll(url, transformer) {
const response = await axios.get(this.dataUrlPrefix + url); const response = await axios.get(this.dataUrlPrefix + url);
@ -26,13 +26,12 @@ export default class DataService {
} }
static async create(url, data) { static async create(url, data) {
console.log("Test create " + this.dataUrlPrefix + url + data); const response = await axios.post(this.dataUrlPrefix + url, data);
const response = await axios.post(this.dataUrlPrefix + url + data);
return true; return true;
} }
static async update(url, data) { static async update(url, data) {
const response = await axios.put(this.dataUrlPrefix + url + data); const response = await axios.put(this.dataUrlPrefix + url, data);
return true; return true;
} }