Commit 2adddba3 by Patryk Czarnik

Formularz edycji danych - pierwsza wersja

parent a5711c3e
......@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import sklep.model.Product;
......@@ -19,7 +20,7 @@ import sklep.repository.ProductRepository;
public class ProductController {
@Autowired
private ProductRepository productRepository;
@GetMapping
public String wszystkieProdukty(Model model) {
List<Product> products = productRepository.findAll();
......@@ -38,17 +39,14 @@ public class ProductController {
return "missing_product";
}
}
@GetMapping("/szukaj")
public String wyszukiwarka(Model model,
String name,
BigDecimal min,
BigDecimal max) {
public String wyszukiwarka(Model model, String name, BigDecimal min, BigDecimal max) {
List<Product> products = List.of();
List<Product> products = List.of();
if(name != null && !name.isEmpty() && min == null && max == null) {
products = productRepository.findByProductNameContainingIgnoringCase(name);
} else if((name == null || name.isEmpty()) && (min != null || max != null)) {
} else if ((name == null || name.isEmpty()) && (min != null || max != null)) {
if(min == null) {
min = BigDecimal.ZERO;
}
......@@ -56,16 +54,45 @@ public class ProductController {
max = BigDecimal.valueOf(1000_000_000);
}
products = productRepository.findByPriceBetween(min, max);
} else if(name != null && !name.isEmpty() && (min != null || max != null)) {
} else if (name != null && !name.isEmpty() && (min != null || max != null)) {
if(min == null) {
min = BigDecimal.ZERO;
}
if(max == null) {
max = BigDecimal.valueOf(1000_000_000);
}
products = productRepository.findByProductNameContainingIgnoringCaseAndPriceBetween(name, min, max);
products = productRepository.findByProductNameContainingIgnoringCaseAndPriceBetween(name, min, max);
}
model.addAttribute("products", products);
return "wyszukiwarka";
}
@GetMapping("/new")
public String newProduct() {
return "product_form";
}
@GetMapping("/{id}/edit")
public String editProduct(@PathVariable int id, Model model) {
Optional<Product> product = productRepository.findById(id);
if(product.isPresent()) {
model.addAttribute("product", product.get());
return "product_form";
} else {
model.addAttribute("product_id", id);
return "missing_product";
}
}
@PostMapping({ "/new", "/{id}/edit" })
public String saveProduct(Model model, Product product) {
// W tej wersji dane z wypełnionego formularza odbieramy w postaci jednego obiektu Product.
// Spring sam wpisze dane do pól o takich samych nazwach.
// Taki parametr od razu staje się częścią modelu (to jest tzw. ModelAttribute)
// i nie trzeba dodawać go w osobnym poleceniu.
productRepository.save(product);
return "product_form";
}
}
......@@ -12,6 +12,8 @@ public class SecurityConfig {
SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeHttpRequests()
.anyRequest().permitAll()
.and()
.csrf().disable()
;
return httpSecurity.build();
}
......
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edycja towaru</title>
<link rel="stylesheet" type="text/css" href="/styl.css">
</head>
<body>
<c:choose>
<c:when test="${not empty(product.productId)}">
<h1>Edycja produktu nr ${product.productId}</h1>
</c:when>
<c:otherwise>
<h1>Edycja nowego produktu</h1>
</c:otherwise>
</c:choose>
<form id="product-form" method="post">
<table class="form">
<tr>
<td><label for="productId">Numer:</label></td>
<td><input name="productId" placeholder="brak" type="number" readonly="readonly" value="${product.productId}"/></td>
</tr>
<tr>
<td><label for="productName">Nazwa towaru:</label></td>
<td><input name="productName" placeholder="nazwa..." type="text" value="${product.productName}"/>
</td>
</tr>
<tr>
<td><label for="price">Cena:</label></td>
<td><input name="price" placeholder="12.90" title="tu wpisz cenę" type="number" step="0.01" value="${product.price}"/>
</td>
</tr>
<tr>
<td><label for="vat">Stawka VAT:</label></td>
<td><input name="vat" placeholder="0.23" title="tu wpisz cenę" type="number" step="0.01" value="${product.vat}"/>
</td>
</tr>
<tr>
<td><label for="description">Opis:</label></td>
<td><textarea name="description" rows="10" cols="120">${product.description}</textarea></td>
</tr>
<tr>
<td><button>Zapisz</button></td>
</tr>
</table>
</form>
<c:if test="${not empty(errors)}">
<div class="error">
<h4>Błędy:</h4>
<ul>
<c:forEach var="error" items="${errors}">
<li>${error}</li>
</c:forEach>
</ul>
</div>
</c:if>
<c:if test="${saved}">
<div class="info">Zapisano produkt nr ${product.productId}</div>
</c:if>
<p><a href="/products">przejdź do listy produktów</a></p>
<p><a href="/">strona główna</a></p>
</body>
</html>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment