Commit 94c0cbcc by Patryk Czarnik

Edycja produktu - wyświelenie formularza

parent f426f4ad
...@@ -5,6 +5,7 @@ import org.springframework.stereotype.Controller; ...@@ -5,6 +5,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;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
...@@ -38,6 +39,27 @@ public class ProductController { ...@@ -38,6 +39,27 @@ public class ProductController {
} }
} }
@GetMapping("/{id}/edit")
public String editProduct(@PathVariable("id") Integer productId, Model model) {
Optional<Product> product = productRepository.findById(productId);
if(product.isPresent()) {
model.addAttribute("product", product.get());
return "product_form";
} else {
return "missing_product";
}
}
@GetMapping("/new")
public String newProduct() {
return "product_form";
}
@PostMapping({"/{id}/edit", "/new"})
public String saveProduct() {
return "product_form";
}
@GetMapping("/szukaj") @GetMapping("/szukaj")
public String szukaj(Model model, public String szukaj(Model model,
String name, String name,
......
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="jakarta.tags.core"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edycja danych produktu</title>
<link rel="stylesheet" type="text/css" href="/styl.css">
</head>
<body>
<h1>Edycja produktu</h1>
<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 vat" 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>
<div class="action"><a href="/products">powrót do listy produktów</a></div>
<div class="action"><a href="/">powrót do spisu treści</a></div>
</body>
</html>
...@@ -17,10 +17,11 @@ ...@@ -17,10 +17,11 @@
<p>Towar <a href="/products/${product.productId}" class="product-name">${product.productName}</a></p> <p>Towar <a href="/products/${product.productId}" class="product-name">${product.productName}</a></p>
<p>Cena: <span class="product-price">${product.price}</span></p> <p>Cena: <span class="product-price">${product.price}</span></p>
<p class="product-description">${product.description}</p> <p class="product-description">${product.description}</p>
<div class="action"><a href="/products/${product.productId}/edit">Edytuj</a></div>
</div> </div>
</c:forEach> </c:forEach>
<div><a href="/products/new">Dodaj nowy produkt</a></div>
<div><a href="/">Wróć na stronę główną</a></div> <div><a href="/">Wróć na stronę główną</a></div>
</body> </body>
......
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