Commit 7c9ac9f9 by Patryk Czarnik

Edycja produktu - zapis

parent a7000ab1
...@@ -33,7 +33,7 @@ public class AddToBasket extends HttpServlet { ...@@ -33,7 +33,7 @@ public class AddToBasket extends HttpServlet {
// ignorujemy błędy // ignorujemy błędy
} }
// Przekierowanie - każemy przeglądarce wejść pod ten adres. // Przekierowanie - każemy przeglądarce wejść pod ten adres.
response.sendRedirect("products8.jsp"); response.sendRedirect("products9.jsp");
} }
} }
package sklep.web; package sklep.web;
import java.io.IOException; import java.io.IOException;
import java.math.BigDecimal;
import jakarta.servlet.RequestDispatcher; import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException; import jakarta.servlet.ServletException;
...@@ -55,4 +56,33 @@ public class EditProduct extends HttpServlet { ...@@ -55,4 +56,33 @@ public class EditProduct extends HttpServlet {
dispatcher.forward(request, response); dispatcher.forward(request, response);
} }
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// W tej wersji nie obsługujemy błędów - w razie błędu wyświetli się strona z wyjątkiem
// W przypadku braku ID zostanie utworzony nowy produkt, a w przypadku podania ID (gdy to była edycja istniejącego) - zostanie zastąpiony.
request.setCharacterEncoding("UTF-8");
String parametrId = request.getParameter("productId");
Integer productId = (parametrId == null || parametrId.isEmpty()) ? null : Integer.valueOf(parametrId);
String parametrPrice = request.getParameter("price");
BigDecimal price = new BigDecimal(parametrPrice);
String parametrVat = request.getParameter("vat");
BigDecimal vat = (parametrVat == null || parametrVat.isEmpty()) ? null : new BigDecimal(parametrVat);
String name = request.getParameter("productName");
String description = request.getParameter("description");
Product product = new Product(productId, name, price, vat, description);
try(DBConnection db = DBConnection.open()) {
ProductDAO productDAO = db.productDAO();
productDAO.save(product);
db.commit();
// Gdy udało się zapisać, to przejdziemy z powrotem do listy.
response.sendRedirect("products9.jsp");
} catch (DBException e) {
e.printStackTrace();
}
}
} }
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