Commit f115bfc8 by Patryk Czarnik

Zapis danych formularza - oddzielne parametry

parent 0d9be63c
package sklep.controller; package sklep.controller;
import java.math.BigDecimal;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
...@@ -9,6 +10,7 @@ import org.springframework.stereotype.Controller; ...@@ -9,6 +10,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 sklep.model.Product; import sklep.model.Product;
import sklep.repository.ProductRepository; import sklep.repository.ProductRepository;
...@@ -54,5 +56,31 @@ public class ProductController { ...@@ -54,5 +56,31 @@ public class ProductController {
return "missing_product"; return "missing_product";
} }
} }
@PostMapping({"/products/new", "/products/{id}/edit"})
public String saveProduct(
Integer productId,
String productName,
BigDecimal price,
BigDecimal vat,
String description,
Model model) {
// W tej wersji dane z wypełnionego formularza odbieramy w postaci oddzielnych parametrów metody.
// Na podstawie przysłanych parametrów tworzę obiekt Product
Product product = new Product();
product.setProductId(productId);
product.setProductName(productName);
product.setPrice(price);
product.setVat(vat);
product.setDescription(description);
System.out.println("Produkt przed zapisem: " + product);
productRepository.save(product);
System.out.println("Produkt po zapisie : " + product);
// Aby w formularzu było widać dane produktu, dodajemy go też do modelu
model.addAttribute("product", product);
return "product_form";
}
} }
...@@ -82,4 +82,10 @@ public class Product implements Serializable { ...@@ -82,4 +82,10 @@ public class Product implements Serializable {
this.vat = vat; this.vat = vat;
} }
@Override
public String toString() {
return "Product [productId=" + productId + ", description=" + description + ", price=" + price
+ ", productName=" + productName + ", vat=" + vat + "]";
}
} }
\ No newline at end of file
...@@ -13,7 +13,8 @@ public class SecurityConfig { ...@@ -13,7 +13,8 @@ public class SecurityConfig {
@Bean @Bean
SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception { SecurityFilterChain setHttpSecurity(HttpSecurity httpSecurity) throws Exception {
httpSecurity.authorizeRequests() httpSecurity.authorizeRequests()
.anyRequest().permitAll(); .anyRequest().permitAll()
.and().csrf().disable();
return httpSecurity.build(); return httpSecurity.build();
} }
......
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