016. Ver, Modificar y eliminar cliente#64
This commit is contained in:
abemorcardc 2021-04-09 17:44:03 +02:00 committed by GitHub
commit 1a8ce34aa1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 315 additions and 7 deletions

View file

@ -35,6 +35,10 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
http.authorizeRequests().antMatchers("/resources/**", "/webjars/**", "/h2-console/**").permitAll()
.antMatchers(HttpMethod.GET, "/", "/oups").permitAll()
.antMatchers("/users/new").permitAll()
.antMatchers("/clients/new").permitAll()
.antMatchers("/clients/edit").hasAnyAuthority("client")
.antMatchers("/clients/disable").hasAnyAuthority("client")
.antMatchers("/login/**").anonymous()
.antMatchers("/logout").authenticated()
@ -49,7 +53,6 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
.antMatchers("/offers/**/new").hasAnyAuthority("admin", "client")
.antMatchers("/offers/**/activate").hasAnyAuthority("admin","client")
.antMatchers("/clients/new").permitAll()
.antMatchers("/offers").permitAll()
.antMatchers("/offersCreate").hasAuthority("client")

View file

@ -1,7 +1,6 @@
package org.springframework.cheapy.model;
import java.util.List;
import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;

View file

@ -2,13 +2,15 @@ package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.Client;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.Repository;
import org.springframework.transaction.annotation.Transactional;
public interface ClientRepository extends CrudRepository<Client, String> {
public interface ClientRepository extends Repository<Client, String> {
@Query("SELECT client FROM Client client WHERE username =:username")
@Transactional(readOnly = true)
Client findByUsername(String username);
void save(Client client);
}

View file

@ -26,4 +26,8 @@ public class ClientService {
return this.clientRepository.findByUsername(username);
}
public void saveClient(final Client client) throws DataAccessException {
this.clientRepository.save(client);
}
}

View file

@ -0,0 +1,144 @@
package org.springframework.cheapy.web;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.FoodOffer;
import org.springframework.cheapy.model.NuOffer;
import org.springframework.cheapy.model.SpeedOffer;
import org.springframework.cheapy.model.StatusOffer;
import org.springframework.cheapy.model.TimeOffer;
import org.springframework.cheapy.service.ClientService;
import org.springframework.cheapy.service.FoodOfferService;
import org.springframework.cheapy.service.NuOfferService;
import org.springframework.cheapy.service.SpeedOfferService;
import org.springframework.cheapy.service.TimeOfferService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
@Controller
public class ClientController {
private static final String VIEWS_CREATE_OR_UPDATE_CLIENT = "clients/createOrUpdateClientForm";
private final ClientService clientService;
private final FoodOfferService foodOfferService;
private final SpeedOfferService speedOfferService;
private final NuOfferService nuOfferService;
private final TimeOfferService timeOfferService;
public ClientController(final ClientService clientService, FoodOfferService foodOfferService,
SpeedOfferService speedOfferService, NuOfferService nuOfferService, TimeOfferService timeOfferService) {
this.clientService = clientService;
this.foodOfferService=foodOfferService;
this.speedOfferService=speedOfferService;
this.nuOfferService=nuOfferService;
this.timeOfferService=timeOfferService;
}
@GetMapping("/clients/show")
public String processShowForm(Map<String, Object> model) {
Client client = this.clientService.getCurrentClient();
model.put("client", client);
return "clients/clientShow";
}
@GetMapping(value = "/clients/edit")
public String updateClient( final ModelMap model, HttpServletRequest request) {
Client client = this.clientService.getCurrentClient();
model.addAttribute("client", client);
return ClientController.VIEWS_CREATE_OR_UPDATE_CLIENT;
}
@PostMapping(value = "/clients/edit")
public String updateClient(@Valid final Client clientEdit, final BindingResult result,
final ModelMap model, HttpServletRequest request) {
Client clienteSesion = this.clientService.getCurrentClient();
if (result.hasErrors()) {
model.addAttribute("client", clientEdit);
return ClientController.VIEWS_CREATE_OR_UPDATE_CLIENT;
}
BeanUtils.copyProperties(clienteSesion, clientEdit, "name", "email", "address","init", "finish","telephone", "description","food","usuar");
clientEdit.getUsuar().setUsername(clienteSesion.getUsuar().getUsername());
clientEdit.getUsuar().setEnabled(true);
this.clientService.saveClient(clientEdit);
return "redirect:/clients/show";
}
@GetMapping(value = "/clients/disable")
public String disableClient(final ModelMap model) {
Client client = this.clientService.getCurrentClient();
model.put("client", client);
return "/clients/clientDisable";
}
@PostMapping(value = "/clients/disable")
public String disableClientForm(final ModelMap model, HttpServletRequest request) {
Client client = this.clientService.getCurrentClient();
List<FoodOffer> foodOffers=this.foodOfferService.findFoodOfferByUserId(client.getId());
List<SpeedOffer> speedOffers=this.speedOfferService.findSpeedOfferByUserId(client.getId());
List<NuOffer> nuOffers=this.nuOfferService.findNuOfferByUserId(client.getId());
List<TimeOffer> timeOffers=this.timeOfferService.findTimeOfferByUserId(client.getId());
foodOffers.stream().forEach(f->f.setStatus(StatusOffer.inactive));
speedOffers.stream().forEach(s->s.setStatus(StatusOffer.inactive));
nuOffers.stream().forEach(n->n.setStatus(StatusOffer.inactive));
timeOffers.stream().forEach(t->t.setStatus(StatusOffer.inactive));
client.getUsuar().setEnabled(false);
this.clientService.saveClient(client);
try {
request.logout();
} catch (ServletException e) {
}
return "redirect:/login";
}
}

View file

@ -13,9 +13,6 @@ import org.springframework.cheapy.service.FoodOfferService;
import org.springframework.cheapy.service.NuOfferService;
import org.springframework.cheapy.service.SpeedOfferService;
import org.springframework.cheapy.service.TimeOfferService;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

View file

@ -0,0 +1,25 @@
<%@ page session="false" trimDirectiveWhitespaces="true"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<cheapy:layout pageName="client">
<jsp:body>
<h2 class="text-center" style="font-family: 'Lobster'; font-size: 30px; color: rgb(0, 64, 128); padding:30px"><em>¿Está seguro de que quiere eliminar su cuenta?</em></h2>
<form:form modelAttribute="client" class="form-horizontal">
<div class="btns-edit2">
<button type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-trash" aria-hidden="true" style="padding: 5px"> </span>
Dar de baja</button>
</div>
</form:form>
</jsp:body>
</cheapy:layout>

View file

@ -0,0 +1,78 @@
<%@ page session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<cheapy:layout pageName="client">
<h2 style="text-align:center;padding:5px"><fmt:message key="client"/></h2>
<table class="table table-striped" id="clientTable">
<thead>
<tr>
<th><fmt:message key="clientInit"/></th>
<td><c:out value="${client.init}"/></td>
</tr>
<tr>
<th><fmt:message key="clientFinish"/></th>
<td><c:out value="${client.finish}"/></td>
</tr>
<tr>
<th><fmt:message key="nameClient"/></th>
<td><c:out value="${client.name}"/></td>
</tr>
<tr>
<th><fmt:message key="emailClient"/></th>
<td><c:out value="${client.email}%"/> </td>
</tr>
<tr>
<th><fmt:message key="addressClient"/></th>
<td><c:out value="${client.address}%"/> </td>
</tr><tr>
<th><fmt:message key="telephoneClient"/></th>
<td><c:out value="${client.telephone}%"/> </td>
</tr><tr>
<th><fmt:message key="descriptionClient"/></th>
<td><c:out value="${client.description}%"/> </td>
</tr><tr>
<th><fmt:message key="foodClient"/></th>
<td><c:out value="${client.food}%"/> </td>
</tr>
</thead>
</table>
<div class="btn-menu">
<sec:authorize access="hasAnyAuthority('client')">
<sec:authentication var="principal" property="principal" />
<div class="btns-edit">
<spring:url value="edit" var="editUrl"/>
<button type="button" role="link" onclick="window.location='${fn:escapeXml(editUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-edit" aria-hidden="true" style="padding: 5px"> </span>
Editar cliente</button>
<spring:url value="disable" var="disableUrl"/>
<button type="button" role="link" onclick="window.location='${fn:escapeXml(disableUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon glyphicon-edit" aria-hidden="true" style="padding: 5px"> </span>
Borrar cliente</button>
</div>
</sec:authorize>
</div>
</cheapy:layout>

View file

@ -0,0 +1,51 @@
<%@ page session="false" trimDirectiveWhitespaces="true" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%@ taglib prefix="cheapy" tagdir="/WEB-INF/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<cheapy:layout pageName="clients">
<h2 style="text-align:center;padding:5px">
<c:if test="${client['new']}"><fmt:message key="new"/> </c:if> <fmt:message key="client"/>
</h2>
<form:form modelAttribute="client" class="form-horizontal" id="add-client-form">
<div class="form-group has-feedback">
<form:hidden path="code"/>
<cheapy:inputField label="Contraseña" placeholder="Restaurante pepito" name="usuar.password"/>
<cheapy:inputField label="Hora de inicio" placeholder="HH:mm" name="init"/>
<cheapy:inputField label="Hora de fin" placeholder="HH:mm" name="finish"/>
<cheapy:inputField label="Name" placeholder="Restaurante pepito" name="name"/>
<cheapy:inputField label="Email" placeholder="" name="email"/>
<cheapy:inputField label="Dirección" placeholder="" name="address"/>
<cheapy:inputField label="telephone" placeholder="" name="telephone"/>
<cheapy:inputField label="description" placeholder="" name="description"/>
<cheapy:inputField label="food" placeholder="food" name="food"/>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="btn-mod">
<c:choose>
<c:when test="${client['new']}">
<button class="btn btn-default" type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true" style="padding: 5px"> </span>
Crear cliente</button>
</c:when>
<c:otherwise>
<button class="btn btn-default" type="submit" style="font-family: 'Lobster'; font-size: 20px;">
<span class="glyphicon glyphicon-floppy-save" aria-hidden="true" style="padding: 5px"> </span>
Modificar</button>
</c:otherwise>
</c:choose>
</div>
</div>
</div>
</form:form>
</cheapy:layout>

View file

@ -33,6 +33,11 @@
<span class="glyphicon glyphicon-cutlery" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="createOffers"/> </button>
</div>
<div class="btn-home">
<button type="button" role="link" onclick="window.location='/clients/show'" style="font-family: 'Lobster'; font-size: 20px;margin:5px;">
<span class="glyphicon glyphicon-cutlery" aria-hidden="true" style="padding: 5px"> </span>
<fmt:message key="clientShow"/> </button>
</div>
</sec:authorize>