mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-23 15:55:49 +00:00
Merge pull request #78 from cheapy-ispp/024-ver-modificar-eliminar-Admin-Cliente-#76
Administrador lista, muestra y borra clientes funcional
This commit is contained in:
commit
4d6bb124b0
8 changed files with 135 additions and 24 deletions
|
@ -46,7 +46,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
|||
|
||||
.antMatchers("/usuarios/new").permitAll()
|
||||
.antMatchers("/usuarios/**").hasAnyAuthority("usuario")
|
||||
.antMatchers("/admin/**").hasAnyAuthority("admin")
|
||||
.antMatchers("/administrators/**").hasAnyAuthority("admin")
|
||||
|
||||
.antMatchers("/owners/**").hasAnyAuthority("owner", "admin")
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.springframework.cheapy.repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
@ -11,6 +13,11 @@ public interface ClientRepository extends Repository<Client, String> {
|
|||
@Transactional(readOnly = true)
|
||||
Client findByUsername(String username);
|
||||
|
||||
@Query("SELECT client FROM Client client")
|
||||
@Transactional(readOnly = true)
|
||||
List<Client> findAllClient();
|
||||
|
||||
|
||||
void save(Client client);
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.springframework.cheapy.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cheapy.model.Client;
|
||||
import org.springframework.cheapy.repository.ClientRepository;
|
||||
|
@ -26,8 +28,16 @@ public class ClientService {
|
|||
return this.clientRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public Client findByUsername(String username) throws DataAccessException {
|
||||
return this.clientRepository.findByUsername(username);
|
||||
}
|
||||
|
||||
public void saveClient(final Client client) throws DataAccessException {
|
||||
this.clientRepository.save(client);
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public List<Client> findAllClient() throws DataAccessException {
|
||||
return this.clientRepository.findAllClient();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,25 +1,16 @@
|
|||
package org.springframework.cheapy.web;
|
||||
|
||||
import java.time.format.DateTimeFormatter;
|
||||
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.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.model.Client;
|
||||
import org.springframework.cheapy.model.Usuario;
|
||||
import org.springframework.cheapy.service.ClientService;
|
||||
import org.springframework.cheapy.service.UsuarioService;
|
||||
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.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
|
@ -30,9 +21,11 @@ public class AdministratorController {
|
|||
private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuarioForm";
|
||||
|
||||
private final UsuarioService usuarioService;
|
||||
private final ClientService clientService;
|
||||
|
||||
public AdministratorController(final UsuarioService usuarioService, ClientService clientService) {
|
||||
this.usuarioService = usuarioService;
|
||||
this.clientService=clientService;
|
||||
}
|
||||
|
||||
@GetMapping("/administrators/usuarios")
|
||||
|
@ -42,13 +35,26 @@ public class AdministratorController {
|
|||
return "usuarios/usuariosList";
|
||||
}
|
||||
|
||||
@GetMapping("/administrators/clients")
|
||||
public String processFindClientesForm(Map<String, Object> model) {
|
||||
List<Client> clientLs = this.clientService.findAllClient();
|
||||
model.put("clientLs", clientLs);
|
||||
return "clients/clientsList";
|
||||
}
|
||||
@GetMapping("/administrators/usuarios/{username}")
|
||||
public String processShowForm(@PathVariable("username") String username, Map<String, Object> model) {
|
||||
public String processUsuarioShowForm(@PathVariable("username") String username, Map<String, Object> model) {
|
||||
Usuario usuario = this.usuarioService.findByUsername(username);
|
||||
model.put("usuario", usuario);
|
||||
return "usuarios/usuariosShow";
|
||||
}
|
||||
|
||||
@GetMapping("/administrators/clients/{username}")
|
||||
public String processClientShowForm(@PathVariable("username") String username, Map<String, Object> model) {
|
||||
Client client = this.clientService.findByUsername(username);
|
||||
model.put("client", client);
|
||||
return "clients/clientShow";
|
||||
}
|
||||
|
||||
@GetMapping(value = "/administrators/usuarios/{username}/disable")
|
||||
public String disableUsuario(@PathVariable("username") final String username, final ModelMap model) {
|
||||
|
||||
|
@ -56,6 +62,7 @@ public class AdministratorController {
|
|||
model.put("usuario", usuario);
|
||||
return "usuarios/usuariosDisable";
|
||||
}
|
||||
|
||||
|
||||
@PostMapping(value = "/administrators/usuarios/{username}/disable")
|
||||
public String disableUsuarioForm(@PathVariable("username") final String username, final ModelMap model, final HttpServletRequest request) {
|
||||
|
@ -65,4 +72,21 @@ public class AdministratorController {
|
|||
this.usuarioService.saveUsuario(usuario);
|
||||
return "redirect:/administrators/usuarios";
|
||||
}
|
||||
|
||||
|
||||
@GetMapping(value = "/administrators/clients/{username}/disable")
|
||||
public String disableClient(@PathVariable("username") final String username, final ModelMap model) {
|
||||
|
||||
Client client = this.clientService.findByUsername(username);
|
||||
model.put("client", client);
|
||||
return "clients/clientDisable";
|
||||
}
|
||||
@PostMapping(value = "/administrators/clients/{username}/disable")
|
||||
public String disableClientForm(@PathVariable("username") final String username, final ModelMap model, final HttpServletRequest request) {
|
||||
|
||||
Client client = this.clientService.findByUsername(username);
|
||||
client.getUsuar().setEnabled(false);
|
||||
this.clientService.saveClient(client);
|
||||
return "redirect:/administrators/clients";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -67,11 +67,20 @@
|
|||
<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>
|
||||
<sec:authorize access="hasAnyAuthority('admin')">
|
||||
<sec:authentication var="principal" property="principal" />
|
||||
<div class="btns-edit">
|
||||
|
||||
<spring:url value="/administrators/clients/{username}/disable" var="deactivateUrl">
|
||||
<spring:param name="username" value="${client.usuar.username}"/>
|
||||
</spring:url>
|
||||
<button type="button" role="link" onclick="window.location='${fn:escapeXml(deactivateUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon glyphicon-trash" aria-hidden="true" style="padding: 5px"> </span>
|
||||
Desactivar cliente</button>
|
||||
</div>
|
||||
</sec:authorize>
|
||||
</div>
|
||||
|
||||
|
||||
|
|
55
src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp
Normal file
55
src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp
Normal file
|
@ -0,0 +1,55 @@
|
|||
<%@ 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="clients">
|
||||
<h2 style="text-align:center;padding:5px"><fmt:message key="clients"/></h2>
|
||||
|
||||
<c:if test="${empty clientLs }">
|
||||
<p id="vacio" >No hay ningun Cliente.</p>
|
||||
</c:if>
|
||||
<c:if test="${not empty clientLs }">
|
||||
<table id="clientTable" class="table table-striped">
|
||||
<thead>
|
||||
<tr>
|
||||
|
||||
<th><fmt:message key="nameClient"/></th>
|
||||
<th><fmt:message key="username"/></th>
|
||||
<th><fmt:message key="enabled"/></th>
|
||||
<th> </th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<c:forEach items="${clientLs}" var="client">
|
||||
<tr>
|
||||
|
||||
<td>
|
||||
<c:out value="${client.name}"/>
|
||||
</td>
|
||||
<td>
|
||||
<c:out value="${client.usuar.username}"/>
|
||||
</td>
|
||||
<td>
|
||||
<c:out value="${client.usuar.enabled}"/>
|
||||
</td>
|
||||
<td>
|
||||
<spring:url value="/administrators/clients/{username}" var="clientUrl">
|
||||
<spring:param name="username" value="${client.usuar.username}"/>
|
||||
</spring:url>
|
||||
<div class="btn-detalles">
|
||||
<button type="button" role="link" onclick="window.location='${fn:escapeXml(clientUrl)}'" style="font-family: 'Lobster'; font-size: 20px;">
|
||||
<span class="glyphicon glyphicon-info-sign" aria-hidden="true" style="padding: 5px"> </span>
|
||||
<fmt:message key="details"/></button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
</c:forEach>
|
||||
</tbody>
|
||||
</table>
|
||||
</c:if>
|
||||
</cheapy:layout>
|
|
@ -48,14 +48,6 @@
|
|||
<fmt:message key="showUsuario"/> </button>
|
||||
</div>
|
||||
</sec:authorize>
|
||||
|
||||
<sec:authorize access="hasAnyAuthority('admin')">
|
||||
<div class="btn-home">
|
||||
<button type="button" role="link" onclick="window.location='/administrators/usuarios'" style="font-family: 'Lobster'; font-size: 20px;margin:5px;">
|
||||
<span class="glyphicon glyphicon-cutlery" aria-hidden="true" style="padding: 5px"> </span>
|
||||
<fmt:message key="showUsuarios"/> </button>
|
||||
</div>
|
||||
</sec:authorize>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -39,6 +39,20 @@
|
|||
<span>Mis ofertas</span>
|
||||
</cheapy:menuItem>
|
||||
</sec:authorize>
|
||||
|
||||
<sec:authorize access="hasAnyAuthority('admin')">
|
||||
<cheapy:menuItem active="${name eq 'clientes'}" url="/administrators/clients" title="clients">
|
||||
<span class="glyphicon " aria-hidden="true"></span>
|
||||
<span>Clientes</span>
|
||||
</cheapy:menuItem>
|
||||
</sec:authorize>
|
||||
|
||||
<sec:authorize access="hasAnyAuthority('admin')">
|
||||
<cheapy:menuItem active="${name eq 'usuarios'}" url="/administrators/usuarios" title="usuarios">
|
||||
<span class="glyphicon " aria-hidden="true"></span>
|
||||
<span>Usuarios</span>
|
||||
</cheapy:menuItem>
|
||||
</sec:authorize>
|
||||
<!--
|
||||
<cheapy:menuItem active="${name eq 'contactanos'}" url="/contactanos"
|
||||
title="contactanos">
|
||||
|
|
Loading…
Reference in a new issue