Commit dfb125f7 by Patryk Czarnik

Strony dot. Customer

parent 74c23399
package sklep.controller;
import java.util.List;
import java.util.Optional;
import jakarta.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 sklep.model.Customer;
public interface CustomerRepository extends JpaRepository<Customer, String> {
}
package sklep.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import sklep.model.Order;
public interface OrderRepository extends JpaRepository<Order, Integer> {
}
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Klient ${customer.customerName}</title>
<c:url var="style_url" value="/styl.css"/>
<link rel="stylesheet" type="text/css" href="${style_url}"/>
</head>
<body>
<h1>Klient ${customer.customerName}</h1>
<table class="data-table">
<tr><th>Email:</th><td>${customer.customerEmail}</td></tr>
<tr><th>Nazwa:</th><td>${customer.customerName}</td></tr>
<tr><th>Numer tel:</th><td>${customer.phoneNumber}</td></tr>
<tr><th>Adres:</th><td>${customer.address}</td></tr>
<tr><th>Miasto:</th><td>${customer.postalCode} ${customer.city}</td></tr>
</table>
<c:url var="adres_do_edycji" value="/customers/${customer.customerEmail}/edit"/>
<div class="action"><a href="${adres_edycji}">Edytuj</a></div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.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>
<c:url var="style_url" value="/styl.css"/>
<link rel="stylesheet" type="text/css" href="${style_url}"/>
</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="Jan Kowalski" 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>
<div><a href="${pageContext.request.contextPath}/customers">powrót do listy klientów</a></div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="jakarta.tags.core" %>
<%@ taglib prefix="s" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<%-- W tym pliku używam tagów c:url do generowania adresów, które są poprawne niezależnie od miejsca zdeployowania aplikacji.
Obojętnie, czy aplikacja działabezpośrednio pod adresem localhost:8080/ , czy w jakimś podkatalogu (ma niepusty "context-path"),
- adresy są odpowiednio poprawiane.
Jest to jednak nieco pracochłonne w zapisie, dlatego jest to tylko w tym jednym pliku.
--%>
<html>
<head>
<meta charset="UTF-8">
<title>Lista klientów</title>
<c:url var="style_url" value="/styl.css"/>
<link rel="stylesheet" type="text/css" href="${style_url}"/>
</head>
<body>
<h1>Lista klientów</h1>
<table class="data-table">
<tr><th>Email</th><th>Nazwa</th><th>Telefon</th><th>Adres</th><th>Miasto</th><th/></tr>
<c:forEach var="customer" items="${customers}">
<c:url var="adr" value="/customers/${customer.customerEmail}"/>
<tr>
<td><a href="${adr}">${customer.customerEmail}</a></td>
<td>${customer.customerName}</td>
<td>${customer.phoneNumber}</td>
<td>${customer.address}</td>
<td>${customer.postalCode} ${customer.city}</td>
<td>
<c:url var="adres_do_edycji" value="/customers/${customer.customerEmail}/edit"/>
<div class="action"><a href="${adres_do_edycji}">Edytuj</a></div>
</td>
</tr>
</c:forEach>
</table>
<c:url var="adres_new" value="/customers/new"/>
<div class="action"><a href="${adres_new}">Dodaj nowego klienta</a></div>
</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