Commit 2adddba3 by Patryk Czarnik

Formularz edycji danych - pierwsza wersja

parent a5711c3e
...@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller; ...@@ -9,6 +9,7 @@ import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import sklep.model.Product; import sklep.model.Product;
...@@ -19,7 +20,7 @@ import sklep.repository.ProductRepository; ...@@ -19,7 +20,7 @@ import sklep.repository.ProductRepository;
public class ProductController { public class ProductController {
@Autowired @Autowired
private ProductRepository productRepository; private ProductRepository productRepository;
@GetMapping @GetMapping
public String wszystkieProdukty(Model model) { public String wszystkieProdukty(Model model) {
List<Product> products = productRepository.findAll(); List<Product> products = productRepository.findAll();
...@@ -38,17 +39,14 @@ public class ProductController { ...@@ -38,17 +39,14 @@ public class ProductController {
return "missing_product"; return "missing_product";
} }
} }
@GetMapping("/szukaj") @GetMapping("/szukaj")
public String wyszukiwarka(Model model, public String wyszukiwarka(Model model, String name, BigDecimal min, BigDecimal max) {
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) { if(name != null && !name.isEmpty() && min == null && max == null) {
products = productRepository.findByProductNameContainingIgnoringCase(name); 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) { if(min == null) {
min = BigDecimal.ZERO; min = BigDecimal.ZERO;
} }
...@@ -56,16 +54,45 @@ public class ProductController { ...@@ -56,16 +54,45 @@ public class ProductController {
max = BigDecimal.valueOf(1000_000_000); max = BigDecimal.valueOf(1000_000_000);
} }
products = productRepository.findByPriceBetween(min, max); 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) { if(min == null) {
min = BigDecimal.ZERO; min = BigDecimal.ZERO;
} }
if(max == null) { if(max == null) {
max = BigDecimal.valueOf(1000_000_000); max = BigDecimal.valueOf(1000_000_000);
} }
products = productRepository.findByProductNameContainingIgnoringCaseAndPriceBetween(name, min, max); products = productRepository.findByProductNameContainingIgnoringCaseAndPriceBetween(name, min, max);
} }
model.addAttribute("products", products); model.addAttribute("products", products);
return "wyszukiwarka"; 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 { ...@@ -12,6 +12,8 @@ public class SecurityConfig {
SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception { SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeHttpRequests() httpSecurity.authorizeHttpRequests()
.anyRequest().permitAll() .anyRequest().permitAll()
.and()
.csrf().disable()
; ;
return httpSecurity.build(); 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