Commit a0d112e1 by Patryk Czarnik

Edycja danych klienta

parent 55de9f88
package sklep.controller;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
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.server.ResponseStatusException;
import sklep.model.Customer;
import sklep.repository.CustomerRepository;
@Controller
@RequestMapping("/customers")
public class CustomerController {
@Autowired
private CustomerRepository customerRepository;
@GetMapping
public String listaKlientow(Model model) {
List<Customer> customers = customerRepository.findAll();
model.addAttribute("customers", customers);
return "customers";
}
@GetMapping("/{email}")
public String jedenKlient(Model model, @PathVariable("email") String email) {
Optional<Customer> customer = customerRepository.findById(email);
if(customer.isPresent()) {
model.addAttribute("customer", customer.get());
return "customer";
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nie ma klienta o mailu " + email);
}
}
@GetMapping("/new")
public String nowy(@ModelAttribute Customer customer) {
return "customer_form";
}
@GetMapping("/{email}/edit")
public String edytuj(Model model, @PathVariable("email") String email) {
Optional<Customer> customer = customerRepository.findById(email);
if(customer.isPresent()) {
model.addAttribute("customer", customer.get());
return "customer_form";
} else {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Nie ma klienta o mailu " + email);
}
}
@PostMapping({"/new", "/{email}/edit"})
public String zapisz(Model model,
@ModelAttribute @Valid Customer customer,
BindingResult bindingResult) {
if(bindingResult.hasErrors()) {
model.addAttribute("errors", bindingResult.getAllErrors());
return "customer_form";
} else {
customerRepository.save(customer);
// return "redirect:/customers";
return "customer_form";
}
}
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Customer;
@Repository
public interface CustomerRepository extends JpaRepository<Customer, String> {
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import sklep.model.Order;
@Repository
public interface OrderRepository extends JpaRepository<Order, Integer> {
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="f" uri="http://www.springframework.org/tags/form"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Edycja danych klienta</title>
<link rel="stylesheet" type="text/css" href="/styl.css"/>
</head>
<body>
<h1>Edycja danych klienta</h1>
<f:form id="product-form" method="post" modelAttribute="customer">
<table class="form-tab">
<tr>
<td><f:label path="customerEmail">Email:</f:label></td>
<td><f:input path="customerEmail" type="email"/>
<f:errors path="customerEmail" cssClass="form-error" element="div"/>
</td>
</tr>
<tr>
<td><f:label path="customerName">Nazwa / imię i nazwisko:</f:label></td>
<td><f:input path="customerName" placeholder="Ala Kowalska" type="text"/>
<f:errors path="customerName" cssClass="form-error" element="div"/>
</td>
</tr>
<tr>
<td><f:label path="phoneNumber">Telefon:</f:label></td>
<td><f:input path="phoneNumber" placeholder="123123123" type="text"/>
<f:errors path="phoneNumber" cssClass="form-error" element="div"/>
</td>
</tr>
<tr>
<td><f:label path="address">Adres:</f:label></td>
<td><f:textarea path="address" rows="2" cols="120"/></td>
</tr>
<tr>
<td><f:label path="postalCode">Kod pocztowy:</f:label></td>
<td><f:input path="postalCode" placeholder="12-345" type="text"/>
<f:errors path="postalCode" cssClass="form-error" element="div"/>
</td>
</tr>
<tr>
<td><f:label path="city">Miasto:</f:label></td>
<td><f:input path="city" placeholder="Kraków" type="text"/>
<f:errors path="city" cssClass="form-error" element="div"/>
</td>
</tr>
<tr>
<td><f:button>Zapisz</f:button></td>
</tr>
</table>
</f:form>
</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