From ba2b2a4345b62a43d69e452b9346c9f1afa6988d Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Thu, 8 Apr 2021 21:35:55 +0200 Subject: [PATCH 1/7] Ver, modificar y borrar de Usuario funcional. --- .../configuration/SecurityConfiguration.java | 1 + .../springframework/cheapy/model/User.java | 7 ++ .../springframework/cheapy/model/Usuario.java | 10 +-- .../cheapy/repository/NuOfferRepository.java | 1 - .../cheapy/repository/UsuarioRepository.java | 18 ++++ .../cheapy/service/UsuarioService.java | 35 ++++++++ .../cheapy/web/UsuarioController.java | 86 +++++++++++++++++++ src/main/resources/db/mysql/data.sql | 8 +- .../usuarios/createOrUpdateUsuariosForm.jsp | 45 ++++++++++ .../WEB-INF/jsp/usuarios/usuariosDisable.jsp | 26 ++++++ .../WEB-INF/jsp/usuarios/usuariosShow.jsp | 62 +++++++++++++ src/main/webapp/WEB-INF/jsp/welcome.jsp | 9 ++ 12 files changed, 298 insertions(+), 10 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java create mode 100644 src/main/java/org/springframework/cheapy/service/UsuarioService.java create mode 100644 src/main/java/org/springframework/cheapy/web/UsuarioController.java create mode 100644 src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/usuarios/usuariosDisable.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 542e41f26..1155bc49e 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -40,6 +40,7 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter { .antMatchers("/logout").authenticated() .antMatchers("/usuarios/new").permitAll() + .antMatchers("/usuarios/**").hasAnyAuthority("usuario") .antMatchers("/admin/**").hasAnyAuthority("admin") .antMatchers("/owners/**").hasAnyAuthority("owner", "admin") diff --git a/src/main/java/org/springframework/cheapy/model/User.java b/src/main/java/org/springframework/cheapy/model/User.java index e9c232562..81b834002 100644 --- a/src/main/java/org/springframework/cheapy/model/User.java +++ b/src/main/java/org/springframework/cheapy/model/User.java @@ -36,4 +36,11 @@ public class User{ this.password = password; } + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } } diff --git a/src/main/java/org/springframework/cheapy/model/Usuario.java b/src/main/java/org/springframework/cheapy/model/Usuario.java index 9079bc72e..6299ca2b5 100644 --- a/src/main/java/org/springframework/cheapy/model/Usuario.java +++ b/src/main/java/org/springframework/cheapy/model/Usuario.java @@ -12,9 +12,9 @@ import javax.validation.constraints.NotBlank; @Table(name = "usuarios") public class Usuario extends BaseEntity{ - /** nombre, apellidos, dni, direccion, telefono, email, username - * (id,nombre, apellidos, dni, direccion, telefono, email, usuar) - */ + /* nombre, apellidos, dni, direccion, telefono, email, username + (id,nombre, apellidos, dni, direccion, telefono, email, usuar)*/ + private static final long serialVersionUID = 1L; @NotBlank @@ -89,11 +89,11 @@ public class Usuario extends BaseEntity{ this.email = email; } - public User getUser() { + public User getUsuar() { return usuar; } - public void setUser(User username) { + public void setUsuar(User username) { this.usuar = username; } diff --git a/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java index d895c5916..1dd0a9296 100644 --- a/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/NuOfferRepository.java @@ -2,7 +2,6 @@ package org.springframework.cheapy.repository; import java.util.List; -import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.NuOffer; import org.springframework.cheapy.model.StatusOffer; import org.springframework.data.repository.Repository; diff --git a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java new file mode 100644 index 000000000..a472b2f17 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java @@ -0,0 +1,18 @@ +package org.springframework.cheapy.repository; + +import java.util.List; + +import org.springframework.cheapy.model.Usuario; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.Repository; +import org.springframework.transaction.annotation.Transactional; + +public interface UsuarioRepository extends Repository { + + @Query("SELECT usuario FROM Usuario usuario WHERE username =:username") + @Transactional(readOnly = true) + Usuario findByUsername(String username); + + void save(Usuario usuario); + +} diff --git a/src/main/java/org/springframework/cheapy/service/UsuarioService.java b/src/main/java/org/springframework/cheapy/service/UsuarioService.java new file mode 100644 index 000000000..9fb6002a8 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/service/UsuarioService.java @@ -0,0 +1,35 @@ + +package org.springframework.cheapy.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cheapy.model.Usuario; +import org.springframework.cheapy.repository.UsuarioRepository; +import org.springframework.dao.DataAccessException; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +public class UsuarioService { + + private UsuarioRepository usuarioRepository; + + + @Autowired + public UsuarioService(final UsuarioRepository usuarioRepository) { + this.usuarioRepository = usuarioRepository; + } + + @Transactional + public Usuario getCurrentUsuario() throws DataAccessException { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + String username = authentication.getName(); + return this.usuarioRepository.findByUsername(username); + } + + @Transactional + public void saveUsuario(final Usuario usuario) throws DataAccessException { + this.usuarioRepository.save(usuario); + } +} diff --git a/src/main/java/org/springframework/cheapy/web/UsuarioController.java b/src/main/java/org/springframework/cheapy/web/UsuarioController.java new file mode 100644 index 000000000..08d6185f8 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/UsuarioController.java @@ -0,0 +1,86 @@ +package org.springframework.cheapy.web; + +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.SpeedOffer; +import org.springframework.cheapy.model.StatusOffer; +import org.springframework.cheapy.model.Usuario; +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; + +@Controller +public class UsuarioController { + + private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuariosForm"; + + private final UsuarioService usuarioService; + + public UsuarioController(final UsuarioService usuarioService) { + this.usuarioService = usuarioService; + } + + @GetMapping("/usuarios/show") + public String processShowForm(Map model) { + Usuario usuario = this.usuarioService.getCurrentUsuario(); + model.put("usuario", usuario); + return "usuarios/usuariosShow"; + } + + @GetMapping(value = "/usuarios/edit") + public String updateUsuario(final ModelMap model, HttpServletRequest request) { + Usuario usuario = this.usuarioService.getCurrentUsuario(); + model.addAttribute("usuario", usuario); + return UsuarioController.VIEWS_USUARIO_CREATE_OR_UPDATE_FORM; + } + + @PostMapping(value = "/usuarios/edit") + public String updateUsuario(@Valid final Usuario usuarioEdit, final BindingResult result, + final ModelMap model, HttpServletRequest request) { + + Usuario usuario = this.usuarioService.getCurrentUsuario(); + BeanUtils.copyProperties(usuario, usuarioEdit, "nombre", "apellidos", "dni", "direccion", "telefono", "usuar"); + usuarioEdit.getUsuar().setUsername(usuario.getNombre()); + usuarioEdit.getUsuar().setEnabled(true); + this.usuarioService.saveUsuario(usuarioEdit); + return "redirect:/usuarios/show"; + } + + @GetMapping(value = "/usuarios/disable") + public String disableUsuario(final ModelMap model) { + + Usuario usuario = this.usuarioService.getCurrentUsuario(); + model.put("usuario", usuario); + return "usuarios/usuariosDisable"; + } + + @PostMapping(value = "/usuarios/disable") + public String disableUsuarioForm(final ModelMap model, final HttpServletRequest request) { + + Usuario usuario = this.usuarioService.getCurrentUsuario(); + usuario.getUsuar().setEnabled(false); + this.usuarioService.saveUsuario(usuario); + + try { + + request.logout(); + + } catch (ServletException e) { + + e.printStackTrace(); + + } + + return "redirect:/login"; + + } +} diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index c279eadf3..5f2794626 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -24,10 +24,10 @@ INSERT INTO authorities VALUES ('lolo','usuario'); INSERT INTO users (dtype,username,password,enabled) VALUES ('User','pepe','pepe', TRUE ); INSERT INTO authorities VALUES ('pepe','usuario'); -INSERT INTO usuarios VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin'); -INSERT INTO usuarios VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '666973647', 'Paco@gmail.com','paco'); -INSERT INTO usuarios VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo'); -INSERT INTO usuarios VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe'); +INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin'); +INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '666973647', 'Paco@gmail.com','paco'); +INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo'); +INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe'); INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (1,'bar manoli','manoli@gmail.com','C/Betis','10:00','22:00','608726190', 'description 1', 'code1', 'ESPAÑOLA','manoli'); INSERT INTO clients (id, name, email, address, init, finish, telephone, description, code, food, username) VALUES (2,'bar david','david@gmail.com','C/Sevilla','09:30','22:00','608726190', 'description 2', 'code2', 'americana','david'); diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp new file mode 100644 index 000000000..e63363513 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp @@ -0,0 +1,45 @@ +<%@ 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" %> + + + +

+ +

+ + +
+ + + + + + + +
+
+
+
+ + + + + + + + +
+
+
+
+ +
diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/usuariosDisable.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosDisable.jsp new file mode 100644 index 000000000..a728c3f55 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosDisable.jsp @@ -0,0 +1,26 @@ +<%@ 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" %> + + + + +

¿Está seguro de que quiere eliminar su cuenta?

+ + + +
+ + +
+
+ + +
+
diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp new file mode 100644 index 000000000..4e5e6b962 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp @@ -0,0 +1,62 @@ +<%@ 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" %> + + + + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + +
+ + + + + +
+
+
+ + +
diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index a2622ed5a..37142a352 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -33,6 +33,15 @@ + + + + +
+ +
From 44d03596b26caf8a847f3f2143e26d466655a975 Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Fri, 9 Apr 2021 00:15:15 +0200 Subject: [PATCH 2/7] =?UTF-8?q?A=C3=B1adida=20funcionalidad=20de=20ver,=20?= =?UTF-8?q?modificar=20y=20eliminar=20Cliente?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../configuration/SecurityConfiguration.java | 5 +- .../springframework/cheapy/model/Client.java | 1 - .../springframework/cheapy/model/User.java | 8 ++ .../cheapy/repository/ClientRepository.java | 6 +- .../cheapy/service/ClientService.java | 4 + .../cheapy/web/ClientController.java | 115 ++++++++++++++++++ .../cheapy/web/OfertaController.java | 3 - .../WEB-INF/jsp/clients/clientDisable.jsp | 25 ++++ .../webapp/WEB-INF/jsp/clients/clientShow.jsp | 78 ++++++++++++ .../jsp/clients/createOrUpdateClientForm.jsp | 51 ++++++++ src/main/webapp/WEB-INF/jsp/welcome.jsp | 5 + 11 files changed, 294 insertions(+), 7 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/web/ClientController.java create mode 100644 src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp create mode 100644 src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 542e41f26..25364f7b0 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -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() @@ -48,7 +52,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") diff --git a/src/main/java/org/springframework/cheapy/model/Client.java b/src/main/java/org/springframework/cheapy/model/Client.java index e3f939728..2981c9d98 100644 --- a/src/main/java/org/springframework/cheapy/model/Client.java +++ b/src/main/java/org/springframework/cheapy/model/Client.java @@ -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; diff --git a/src/main/java/org/springframework/cheapy/model/User.java b/src/main/java/org/springframework/cheapy/model/User.java index e9c232562..4ef2dec13 100644 --- a/src/main/java/org/springframework/cheapy/model/User.java +++ b/src/main/java/org/springframework/cheapy/model/User.java @@ -36,4 +36,12 @@ public class User{ this.password = password; } + public boolean getEnabled() { + return enabled; + } + + public void setEnabled(boolean enabled) { + this.enabled = enabled; + } + } diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java index 1e04f6f3a..54c696f00 100644 --- a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -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 { +public interface ClientRepository extends Repository { @Query("SELECT client FROM Client client WHERE username =:username") @Transactional(readOnly = true) Client findByUsername(String username); + void save(Client client); + } diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index d65649680..c8404afb9 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -26,4 +26,8 @@ public class ClientService { return this.clientRepository.findByUsername(username); } + public void saveClient(final Client client) throws DataAccessException { + this.clientRepository.save(client); + } + } diff --git a/src/main/java/org/springframework/cheapy/web/ClientController.java b/src/main/java/org/springframework/cheapy/web/ClientController.java new file mode 100644 index 000000000..47ba8a918 --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/ClientController.java @@ -0,0 +1,115 @@ + +package org.springframework.cheapy.web; + +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.service.ClientService; +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; + + public ClientController(final ClientService clientService) { + this.clientService = clientService; + } + + + + private boolean checkDates(final FoodOffer foodOffer) { + boolean res = false; + if(foodOffer.getEnd()==null || foodOffer.getStart()==null || foodOffer.getEnd().isAfter(foodOffer.getStart())) { + res = true; + } + return res; + } + + + + + @GetMapping("/clients/show") + public String processShowForm(Map 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(); + + client.getUsuar().setEnabled(false); + + this.clientService.saveClient(client); + + try { + request.logout(); + } catch (ServletException e) { + + } + return "redirect:/login"; + + } +} diff --git a/src/main/java/org/springframework/cheapy/web/OfertaController.java b/src/main/java/org/springframework/cheapy/web/OfertaController.java index 60567c491..996773042 100644 --- a/src/main/java/org/springframework/cheapy/web/OfertaController.java +++ b/src/main/java/org/springframework/cheapy/web/OfertaController.java @@ -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; diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp new file mode 100644 index 000000000..5961d075c --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/clientDisable.jsp @@ -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" %> + + + + +

¿Está seguro de que quiere eliminar su cuenta?

+ + + +
+ +
+
+ +
+ +
diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp new file mode 100644 index 000000000..398ba4d07 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp @@ -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" %> + + + + +

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ +
+ + + +
+ + + + + + + + + + +
+
+
+ + +
diff --git a/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp new file mode 100644 index 000000000..bd21204d0 --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/createOrUpdateClientForm.jsp @@ -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" %> + + + +

+ +

+ + +
+ + + + + + + + + + + + + +
+
+
+
+ + + + + + + + +
+
+
+
+ +
diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index a2622ed5a..c5bc812e3 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -33,6 +33,11 @@ +
+ +
From bc181eafca7067ce3fa15a1fb47ad922b6f9ecfc Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Fri, 9 Apr 2021 17:28:39 +0200 Subject: [PATCH 3/7] =?UTF-8?q?A=C3=B1adida=20la=20eliminaci=C3=B3n=20de?= =?UTF-8?q?=20las=20ofertas=20del=20cliente=20que=20se=20borre?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cheapy/web/ClientController.java | 51 +++++++++++++++---- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/src/main/java/org/springframework/cheapy/web/ClientController.java b/src/main/java/org/springframework/cheapy/web/ClientController.java index 47ba8a918..3cc9138b3 100644 --- a/src/main/java/org/springframework/cheapy/web/ClientController.java +++ b/src/main/java/org/springframework/cheapy/web/ClientController.java @@ -1,6 +1,7 @@ package org.springframework.cheapy.web; +import java.util.List; import java.util.Map; import javax.servlet.ServletException; @@ -10,7 +11,15 @@ 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; @@ -23,22 +32,27 @@ 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) { + 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; } - private boolean checkDates(final FoodOffer foodOffer) { - boolean res = false; - if(foodOffer.getEnd()==null || foodOffer.getStart()==null || foodOffer.getEnd().isAfter(foodOffer.getStart())) { - res = true; - } - return res; - } - - @GetMapping("/clients/show") @@ -100,8 +114,23 @@ public class ClientController { Client client = this.clientService.getCurrentClient(); + + + List foodOffers=this.foodOfferService.findFoodOfferByUserId(client.getId()); + List speedOffers=this.speedOfferService.findSpeedOfferByUserId(client.getId()); + List nuOffers=this.nuOfferService.findNuOfferByUserId(client.getId()); + List 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 { From 4571c4717845b6094e71f82393022349f0b62e75 Mon Sep 17 00:00:00 2001 From: Antonio Vidal Date: Fri, 9 Apr 2021 17:40:45 +0200 Subject: [PATCH 4/7] Retirados los imports innecesarios y algunas clases que se han ido arrastrando. --- .../ExceptionHandlerConfiguration.java | 5 - .../configuration/SecurityConfiguration.java | 1 + .../springframework/cheapy/model/Client.java | 1 - .../springframework/cheapy/model/Owner.java | 69 -------- .../cheapy/model/package-info.java | 1 - .../cheapy/repository/OwnerRepository.java | 65 ------- .../repository/SpeedOfferRepository.java | 1 - .../cheapy/repository/UsuarioRepository.java | 2 - .../cheapy/service/AuthoritiesService.java | 9 - .../cheapy/service/OwnerService.java | 34 ---- .../cheapy/service/ReviewService.java | 3 - .../cheapy/service/SpeedOfferService.java | 1 - .../cheapy/web/OfertaController.java | 3 - .../cheapy/web/OwnerController.java | 118 ------------- .../cheapy/web/UsuarioController.java | 3 - src/main/resources/db/mysql/data.sql | 11 -- src/main/resources/db/mysql/schema.sql | 9 - .../integration/CheapyIntegrationTests.java | 2 - .../cheapy/web/OwnerControllerTests.java | 162 ------------------ 19 files changed, 1 insertion(+), 499 deletions(-) delete mode 100644 src/main/java/org/springframework/cheapy/model/Owner.java delete mode 100644 src/main/java/org/springframework/cheapy/model/package-info.java delete mode 100644 src/main/java/org/springframework/cheapy/repository/OwnerRepository.java delete mode 100644 src/main/java/org/springframework/cheapy/service/OwnerService.java delete mode 100644 src/main/java/org/springframework/cheapy/web/OwnerController.java delete mode 100644 src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java diff --git a/src/main/java/org/springframework/cheapy/configuration/ExceptionHandlerConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/ExceptionHandlerConfiguration.java index e578e2a7e..fd8a8cf8c 100644 --- a/src/main/java/org/springframework/cheapy/configuration/ExceptionHandlerConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/ExceptionHandlerConfiguration.java @@ -1,7 +1,5 @@ package org.springframework.cheapy.configuration; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.ExceptionHandler; import javax.servlet.http.HttpServletRequest; @@ -14,9 +12,6 @@ import javax.servlet.http.HttpServletRequest; @ControllerAdvice public class ExceptionHandlerConfiguration { - @Autowired - private BasicErrorController errorController; - // add any exceptions/validations/binding problems @ExceptionHandler(Exception.class) public String defaultErrorHandler(HttpServletRequest request, Exception ex) { diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index 1155bc49e..8b7fff0aa 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -21,6 +21,7 @@ import org.springframework.security.crypto.password.PasswordEncoder; */ +@SuppressWarnings("deprecation") @Configuration @EnableWebSecurity public class SecurityConfiguration extends WebSecurityConfigurerAdapter { diff --git a/src/main/java/org/springframework/cheapy/model/Client.java b/src/main/java/org/springframework/cheapy/model/Client.java index e3f939728..2981c9d98 100644 --- a/src/main/java/org/springframework/cheapy/model/Client.java +++ b/src/main/java/org/springframework/cheapy/model/Client.java @@ -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; diff --git a/src/main/java/org/springframework/cheapy/model/Owner.java b/src/main/java/org/springframework/cheapy/model/Owner.java deleted file mode 100644 index 7a04f3434..000000000 --- a/src/main/java/org/springframework/cheapy/model/Owner.java +++ /dev/null @@ -1,69 +0,0 @@ -package org.springframework.cheapy.model; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.Table; -import javax.validation.constraints.Digits; -import javax.validation.constraints.NotEmpty; - -import org.springframework.core.style.ToStringCreator; - -@Entity -@Table(name = "owners") -public class Owner extends Person { - - /** - * - */ - private static final long serialVersionUID = 1L; - - @Column(name = "address") - @NotEmpty - private String address; - - @Column(name = "city") - @NotEmpty - private String city; - - @Column(name = "telephone") - @NotEmpty - @Digits(fraction = 0, integer = 10) - private String telephone; - - - public String getAddress() { - return this.address; - } - - public void setAddress(String address) { - this.address = address; - } - - public String getCity() { - return this.city; - } - - public void setCity(String city) { - this.city = city; - } - - public String getTelephone() { - return this.telephone; - } - - public void setTelephone(String telephone) { - this.telephone = telephone; - } - - - - @Override - public String toString() { - return new ToStringCreator(this) - - .append("id", this.getId()).append("new", this.isNew()).append("lastName", this.getLastName()) - .append("firstName", this.getFirstName()).append("address", this.address).append("city", this.city) - .append("telephone", this.telephone).toString(); - } - -} diff --git a/src/main/java/org/springframework/cheapy/model/package-info.java b/src/main/java/org/springframework/cheapy/model/package-info.java deleted file mode 100644 index 1bc54373f..000000000 --- a/src/main/java/org/springframework/cheapy/model/package-info.java +++ /dev/null @@ -1 +0,0 @@ -package org.springframework.cheapy.model; diff --git a/src/main/java/org/springframework/cheapy/repository/OwnerRepository.java b/src/main/java/org/springframework/cheapy/repository/OwnerRepository.java deleted file mode 100644 index 67c4d6a9a..000000000 --- a/src/main/java/org/springframework/cheapy/repository/OwnerRepository.java +++ /dev/null @@ -1,65 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.springframework.cheapy.repository; - -import java.util.Collection; - -import org.springframework.cheapy.model.Owner; -import org.springframework.data.jpa.repository.Query; -import org.springframework.data.repository.Repository; -import org.springframework.data.repository.query.Param; -import org.springframework.transaction.annotation.Transactional; - -/** - * Repository class for Owner domain objects All method names are compliant - * with Spring Data naming conventions so this interface can easily be extended for Spring - * Data. See: - * https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#repositories.query-methods.query-creation - * - * @author Ken Krebs - * @author Juergen Hoeller - * @author Sam Brannen - * @author Michael Isvy - */ -public interface OwnerRepository extends Repository { - - /** - * Retrieve {@link Owner}s from the data store by last name, returning all owners - * whose last name starts with the given name. - * @param lastName Value to search for - * @return a Collection of matching {@link Owner}s (or an empty Collection if none - * found) - */ - @Query("SELECT DISTINCT owner FROM Owner owner WHERE owner.lastName LIKE :lastName%") - @Transactional(readOnly = true) - Collection findByLastName(@Param("lastName") String lastName); - - /** - * Retrieve an {@link Owner} from the data store by id. - * @param id the id to search for - * @return the {@link Owner} if found - */ - @Query("SELECT owner FROM Owner owner WHERE id =:id") - @Transactional(readOnly = true) - Owner findById(@Param("id") Integer id); - - /** - * Save an {@link Owner} to the data store, either inserting or updating it. - * @param owner the {@link Owner} to save - */ - void save(Owner owner); - -} diff --git a/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java index 4c7465368..79eedf517 100644 --- a/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/SpeedOfferRepository.java @@ -2,7 +2,6 @@ package org.springframework.cheapy.repository; import java.util.List; -import org.springframework.cheapy.model.FoodOffer; import org.springframework.cheapy.model.SpeedOffer; import org.springframework.cheapy.model.StatusOffer; import org.springframework.data.jpa.repository.Query; diff --git a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java index a472b2f17..6f833665c 100644 --- a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java @@ -1,7 +1,5 @@ package org.springframework.cheapy.repository; -import java.util.List; - import org.springframework.cheapy.model.Usuario; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; diff --git a/src/main/java/org/springframework/cheapy/service/AuthoritiesService.java b/src/main/java/org/springframework/cheapy/service/AuthoritiesService.java index 7d0f8d7a8..9e91ce3b2 100644 --- a/src/main/java/org/springframework/cheapy/service/AuthoritiesService.java +++ b/src/main/java/org/springframework/cheapy/service/AuthoritiesService.java @@ -15,16 +15,7 @@ */ package org.springframework.cheapy.service; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.dao.DataAccessException; -import org.springframework.cheapy.model.Authorities; -import org.springframework.cheapy.model.User; -import org.springframework.cheapy.repository.AuthoritiesRepository; import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; @Service public class AuthoritiesService { diff --git a/src/main/java/org/springframework/cheapy/service/OwnerService.java b/src/main/java/org/springframework/cheapy/service/OwnerService.java deleted file mode 100644 index da5559f37..000000000 --- a/src/main/java/org/springframework/cheapy/service/OwnerService.java +++ /dev/null @@ -1,34 +0,0 @@ -package org.springframework.cheapy.service; - -import java.util.Collection; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cheapy.model.Owner; -import org.springframework.cheapy.repository.OwnerRepository; -import org.springframework.dao.DataAccessException; -import org.springframework.stereotype.Service; - -@Service -public class OwnerService { - private OwnerRepository ownerRepository; - - - @Autowired - public OwnerService(final OwnerRepository ownerRepository) { - this.ownerRepository = ownerRepository; - } - - public Owner findOwnerById(final int id) { - return this.ownerRepository.findById(id); - } - - public Collection findByLastName(final String lastname) { // - return this.ownerRepository.findByLastName(lastname); - - } - - public void saveOwner(final Owner owner) throws DataAccessException { // - this.ownerRepository.save(owner); - - } -} diff --git a/src/main/java/org/springframework/cheapy/service/ReviewService.java b/src/main/java/org/springframework/cheapy/service/ReviewService.java index 2b7bc9301..d07bf7e33 100644 --- a/src/main/java/org/springframework/cheapy/service/ReviewService.java +++ b/src/main/java/org/springframework/cheapy/service/ReviewService.java @@ -4,10 +4,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Review; -import org.springframework.cheapy.model.StatusOffer; -import org.springframework.cheapy.model.TimeOffer; import org.springframework.cheapy.repository.ReviewRepository; -import org.springframework.cheapy.repository.TimeOfferRepository; import org.springframework.dao.DataAccessException; import org.springframework.stereotype.Service; diff --git a/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java index 384ec046c..2784446b9 100644 --- a/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java +++ b/src/main/java/org/springframework/cheapy/service/SpeedOfferService.java @@ -3,7 +3,6 @@ package org.springframework.cheapy.service; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cheapy.model.NuOffer; import org.springframework.cheapy.model.SpeedOffer; import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.repository.SpeedOfferRepository; diff --git a/src/main/java/org/springframework/cheapy/web/OfertaController.java b/src/main/java/org/springframework/cheapy/web/OfertaController.java index 60567c491..996773042 100644 --- a/src/main/java/org/springframework/cheapy/web/OfertaController.java +++ b/src/main/java/org/springframework/cheapy/web/OfertaController.java @@ -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; diff --git a/src/main/java/org/springframework/cheapy/web/OwnerController.java b/src/main/java/org/springframework/cheapy/web/OwnerController.java deleted file mode 100644 index 229693415..000000000 --- a/src/main/java/org/springframework/cheapy/web/OwnerController.java +++ /dev/null @@ -1,118 +0,0 @@ -package org.springframework.cheapy.web; - -import java.util.Collection; -import java.util.Map; - -import javax.validation.Valid; - -import org.springframework.cheapy.model.Owner; -import org.springframework.cheapy.service.OwnerService; -import org.springframework.stereotype.Controller; -import org.springframework.ui.Model; -import org.springframework.validation.BindingResult; -import org.springframework.web.bind.WebDataBinder; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.InitBinder; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.servlet.ModelAndView; - -@Controller -public class OwnerController { - - private static final String VIEWS_OWNER_CREATE_OR_UPDATE_FORM = "owners/createOrUpdateOwnerForm"; - - private final OwnerService ownerService; - - - - public OwnerController(final OwnerService ownerService) { - this.ownerService = ownerService; - - } - - @InitBinder - public void setAllowedFields(WebDataBinder dataBinder) { - dataBinder.setDisallowedFields("id"); - } - - @GetMapping("/owners/new") - public String initCreationForm(Map model) { - Owner owner = new Owner(); - model.put("owner", owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @PostMapping("/owners/new") - public String processCreationForm(@Valid Owner owner, BindingResult result) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - else { - this.ownerService.saveOwner(owner); - return "redirect:/owners/" + owner.getId(); - } - } - - @GetMapping("/owners/find") - public String initFindForm(Map model) { - model.put("owner", new Owner()); - return "owners/findOwners"; - } - - @GetMapping("/owners") - public String processFindForm(Owner owner, BindingResult result, Map model) { - - // allow parameterless GET request for /owners to return all records - if (owner.getLastName() == null) { - owner.setLastName(""); // empty string signifies broadest possible search - } - - // find owners by last name - Collection results = this.ownerService.findByLastName(owner.getLastName()); - if (results.isEmpty()) { - // no owners found - result.rejectValue("lastName", "notFound", "not found"); - return "owners/findOwners"; - } - else if (results.size() == 1) { - // 1 owner found - owner = results.iterator().next(); - return "redirect:/owners/" + owner.getId(); - } - else { - // multiple owners found - model.put("selections", results); - return "owners/ownersList"; - } - } - - @GetMapping("/owners/{ownerId}/edit") - public String initUpdateOwnerForm(@PathVariable("ownerId") int ownerId, Model model) { - Owner owner = this.ownerService.findOwnerById(ownerId); - model.addAttribute(owner); - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - - @PostMapping("/owners/{ownerId}/edit") - public String processUpdateOwnerForm(@Valid Owner owner, BindingResult result, - @PathVariable("ownerId") int ownerId) { - if (result.hasErrors()) { - return VIEWS_OWNER_CREATE_OR_UPDATE_FORM; - } - else { - owner.setId(ownerId); - this.ownerService.saveOwner(owner); - return "redirect:/owners/{ownerId}"; - } - } - @GetMapping("/owners/{ownerId}") - public ModelAndView showOwner(@PathVariable("ownerId") int ownerId) { - ModelAndView mav = new ModelAndView("owners/ownerDetails"); - Owner owner = this.ownerService.findOwnerById(ownerId); - - mav.addObject(owner); - return mav; - } - -} diff --git a/src/main/java/org/springframework/cheapy/web/UsuarioController.java b/src/main/java/org/springframework/cheapy/web/UsuarioController.java index 08d6185f8..26991e812 100644 --- a/src/main/java/org/springframework/cheapy/web/UsuarioController.java +++ b/src/main/java/org/springframework/cheapy/web/UsuarioController.java @@ -7,15 +7,12 @@ import javax.servlet.http.HttpServletRequest; import javax.validation.Valid; import org.springframework.beans.BeanUtils; -import org.springframework.cheapy.model.SpeedOffer; -import org.springframework.cheapy.model.StatusOffer; import org.springframework.cheapy.model.Usuario; 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; @Controller diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 5f2794626..cfdb7d6aa 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -1,14 +1,3 @@ -INSERT INTO owners VALUES (1, 'Javi', 'Franklin', '110 W. Liberty St.', 'Madison', '6085551023'); -INSERT INTO owners VALUES (2, 'Betty', 'Davis', '638 Cardinal Ave.', 'Sun Prairie', '6085551749'); -INSERT INTO owners VALUES (3, 'Eduardo', 'Rodriquez', '2693 Commerce St.', 'McFarland', '6085558763'); -INSERT INTO owners VALUES (4, 'Harold', 'Davis', '563 Friendly St.', 'Windsor', '6085553198'); -INSERT INTO owners VALUES (5, 'Peter', 'McTavish', '2387 S. Fair Way', 'Madison', '6085552765'); -INSERT INTO owners VALUES (6, 'Jean', 'Coleman', '105 N. Lake St.', 'Monona', '6085552654'); -INSERT INTO owners VALUES (7, 'Jeff', 'Black', '1450 Oak Blvd.', 'Monona', '6085555387'); -INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', '6085557683'); -INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); -INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); - INSERT INTO users (dtype,username,password,enabled) VALUES ('User','admin','admin', TRUE ); INSERT INTO authorities VALUES ('admin','admin'); diff --git a/src/main/resources/db/mysql/schema.sql b/src/main/resources/db/mysql/schema.sql index 01f224382..e69de29bb 100644 --- a/src/main/resources/db/mysql/schema.sql +++ b/src/main/resources/db/mysql/schema.sql @@ -1,9 +0,0 @@ -CREATE TABLE IF NOT EXISTS owners ( - id INT(4) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, - first_name VARCHAR(30), - last_name VARCHAR(30), - address VARCHAR(255), - city VARCHAR(80), - telephone VARCHAR(20), - INDEX(last_name) -) engine=InnoDB; diff --git a/src/test/java/org/springframework/cheapy/integration/CheapyIntegrationTests.java b/src/test/java/org/springframework/cheapy/integration/CheapyIntegrationTests.java index 0c7fb3377..cadb75073 100644 --- a/src/test/java/org/springframework/cheapy/integration/CheapyIntegrationTests.java +++ b/src/test/java/org/springframework/cheapy/integration/CheapyIntegrationTests.java @@ -16,8 +16,6 @@ package org.springframework.cheapy.integration; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; diff --git a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java b/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java deleted file mode 100644 index c684187ca..000000000 --- a/src/test/java/org/springframework/cheapy/web/OwnerControllerTests.java +++ /dev/null @@ -1,162 +0,0 @@ -/* - * Copyright 2012-2019 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/* -package org.springframework.cheapy.web; - -import static org.hamcrest.Matchers.hasProperty; -import static org.hamcrest.Matchers.is; -import static org.mockito.BDDMockito.given; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; - -import org.assertj.core.util.Lists; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.cheapy.model.Owner; -import org.springframework.cheapy.service.OwnerService; -import org.springframework.test.web.servlet.MockMvc; - -/** - * Test class for {@link OwnerController} - * - * @author Colin But - */ -/* -@WebMvcTest(OwnerController.class) -class OwnerControllerTests { - - /*private static final int TEST_OWNER_ID = 1; - - @Autowired - private MockMvc mockMvc; - - @MockBean - private OwnerService ownerService; - - - private Owner george; - - @BeforeEach - void setup() { - george = new Owner(); - george.setId(TEST_OWNER_ID); - george.setFirstName("George"); - george.setLastName("Franklin"); - george.setAddress("110 W. Liberty St."); - george.setCity("Madison"); - george.setTelephone("6085551023"); - - given(this.ownerService.findOwnerById(TEST_OWNER_ID)).willReturn(george); - - - } - - @Test - void testInitCreationForm() throws Exception { - mockMvc.perform(get("/owners/new")).andExpect(status().isOk()).andExpect(model().attributeExists("owner")) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); - } - - @Test - void testProcessCreationFormSuccess() throws Exception { - mockMvc.perform(post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs") - .param("address", "123 Caramel Street").param("city", "London").param("telephone", "01316761638")) - .andExpect(status().is3xxRedirection()); - } - - @Test - void testProcessCreationFormHasErrors() throws Exception { - mockMvc.perform( - post("/owners/new").param("firstName", "Joe").param("lastName", "Bloggs").param("city", "London")) - .andExpect(status().isOk()).andExpect(model().attributeHasErrors("owner")) - .andExpect(model().attributeHasFieldErrors("owner", "address")) - .andExpect(model().attributeHasFieldErrors("owner", "telephone")) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); - } - - @Test - void testInitFindForm() throws Exception { - mockMvc.perform(get("/owners/find")).andExpect(status().isOk()).andExpect(model().attributeExists("owner")) - .andExpect(view().name("owners/findOwners")); - } - - @Test - void testProcessFindFormSuccess() throws Exception { - given(this.ownerService.findByLastName("")).willReturn(Lists.newArrayList(george, new Owner())); - mockMvc.perform(get("/owners")).andExpect(status().isOk()).andExpect(view().name("owners/ownersList")); - } - - @Test - void testProcessFindFormByLastName() throws Exception { - given(this.ownerService.findByLastName(george.getLastName())).willReturn(Lists.newArrayList(george)); - mockMvc.perform(get("/owners").param("lastName", "Franklin")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/" + TEST_OWNER_ID)); - } - - @Test - void testProcessFindFormNoOwnersFound() throws Exception { - mockMvc.perform(get("/owners").param("lastName", "Unknown Surname")).andExpect(status().isOk()) - .andExpect(model().attributeHasFieldErrors("owner", "lastName")) - .andExpect(model().attributeHasFieldErrorCode("owner", "lastName", "notFound")) - .andExpect(view().name("owners/findOwners")); - } - - @Test - void testInitUpdateOwnerForm() throws Exception { - mockMvc.perform(get("/owners/{ownerId}/edit", TEST_OWNER_ID)).andExpect(status().isOk()) - .andExpect(model().attributeExists("owner")) - .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) - .andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) - .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) - .andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) - .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); - } - - @Test - void testProcessUpdateOwnerFormSuccess() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe") - .param("lastName", "Bloggs").param("address", "123 Caramel Street").param("city", "London") - .param("telephone", "01616291589")).andExpect(status().is3xxRedirection()) - .andExpect(view().name("redirect:/owners/{ownerId}")); - } - - @Test - void testProcessUpdateOwnerFormHasErrors() throws Exception { - mockMvc.perform(post("/owners/{ownerId}/edit", TEST_OWNER_ID).param("firstName", "Joe") - .param("lastName", "Bloggs").param("city", "London")).andExpect(status().isOk()) - .andExpect(model().attributeHasErrors("owner")) - .andExpect(model().attributeHasFieldErrors("owner", "address")) - .andExpect(model().attributeHasFieldErrors("owner", "telephone")) - .andExpect(view().name("owners/createOrUpdateOwnerForm")); - } - - @Test - void testShowOwner() throws Exception { - mockMvc.perform(get("/owners/{ownerId}", TEST_OWNER_ID)).andExpect(status().isOk()) - .andExpect(model().attribute("owner", hasProperty("lastName", is("Franklin")))) - .andExpect(model().attribute("owner", hasProperty("firstName", is("George")))) - .andExpect(model().attribute("owner", hasProperty("address", is("110 W. Liberty St.")))) - .andExpect(model().attribute("owner", hasProperty("city", is("Madison")))) - .andExpect(model().attribute("owner", hasProperty("telephone", is("6085551023")))) - .andExpect(view().name("owners/ownerDetails")); - }*/ From 47232540b9ac4fb296e9db37b24aba1998f1008e Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:22:14 +0200 Subject: [PATCH 5/7] Ver, modificar y borrar de admin sobre usuarios funcional --- .../cheapy/model/Administrator.java | 16 ++++- .../cheapy/repository/UsuarioRepository.java | 4 ++ .../cheapy/service/UsuarioService.java | 12 ++++ .../cheapy/web/AdministratorController.java | 68 +++++++++++++++++++ .../cheapy/web/UsuarioController.java | 2 +- src/main/resources/db/mysql/data.sql | 15 ++-- ...Form.jsp => createOrUpdateUsuarioForm.jsp} | 0 .../WEB-INF/jsp/usuarios/usuariosList.jsp | 57 ++++++++++++++++ .../WEB-INF/jsp/usuarios/usuariosShow.jsp | 12 ++++ src/main/webapp/WEB-INF/jsp/welcome.jsp | 8 +++ 10 files changed, 185 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/springframework/cheapy/web/AdministratorController.java rename src/main/webapp/WEB-INF/jsp/usuarios/{createOrUpdateUsuariosForm.jsp => createOrUpdateUsuarioForm.jsp} (100%) create mode 100644 src/main/webapp/WEB-INF/jsp/usuarios/usuariosList.jsp diff --git a/src/main/java/org/springframework/cheapy/model/Administrator.java b/src/main/java/org/springframework/cheapy/model/Administrator.java index 44a3ffdf6..0eba5746d 100644 --- a/src/main/java/org/springframework/cheapy/model/Administrator.java +++ b/src/main/java/org/springframework/cheapy/model/Administrator.java @@ -1,15 +1,29 @@ package org.springframework.cheapy.model; +import javax.persistence.CascadeType; import javax.persistence.Entity; +import javax.persistence.JoinColumn; +import javax.persistence.OneToOne; import javax.persistence.Table; @Entity @Table(name = "administrators") -public class Administrator extends User{ +public class Administrator extends BaseEntity{ /** * */ private static final long serialVersionUID = 1L; + @OneToOne(cascade = CascadeType.ALL) + @JoinColumn(name = "username", referencedColumnName = "username") + private User usuar; + + public User getUsuar() { + return usuar; + } + + public void setUsuar(User usuar) { + this.usuar = usuar; + } } diff --git a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java index a472b2f17..87b11c04d 100644 --- a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java @@ -13,6 +13,10 @@ public interface UsuarioRepository extends Repository { @Transactional(readOnly = true) Usuario findByUsername(String username); + @Query("SELECT usuario FROM Usuario usuario") + @Transactional(readOnly = true) + List findAllUsuario(); + void save(Usuario usuario); } diff --git a/src/main/java/org/springframework/cheapy/service/UsuarioService.java b/src/main/java/org/springframework/cheapy/service/UsuarioService.java index 9fb6002a8..5c994342d 100644 --- a/src/main/java/org/springframework/cheapy/service/UsuarioService.java +++ b/src/main/java/org/springframework/cheapy/service/UsuarioService.java @@ -1,6 +1,8 @@ package org.springframework.cheapy.service; +import java.util.List; + import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cheapy.model.Usuario; import org.springframework.cheapy.repository.UsuarioRepository; @@ -27,6 +29,16 @@ public class UsuarioService { String username = authentication.getName(); return this.usuarioRepository.findByUsername(username); } + + @Transactional + public Usuario findByUsername(String username) throws DataAccessException { + return this.usuarioRepository.findByUsername(username); + } + + @Transactional + public List findAllUsuario() throws DataAccessException { + return this.usuarioRepository.findAllUsuario(); + } @Transactional public void saveUsuario(final Usuario usuario) throws DataAccessException { diff --git a/src/main/java/org/springframework/cheapy/web/AdministratorController.java b/src/main/java/org/springframework/cheapy/web/AdministratorController.java new file mode 100644 index 000000000..f1c9b77dc --- /dev/null +++ b/src/main/java/org/springframework/cheapy/web/AdministratorController.java @@ -0,0 +1,68 @@ +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.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; + +@Controller +public class AdministratorController { + + private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuarioForm"; + + private final UsuarioService usuarioService; + + public AdministratorController(final UsuarioService usuarioService, ClientService clientService) { + this.usuarioService = usuarioService; + } + + @GetMapping("/administrators/usuarios") + public String processFindUsuariosForm(Map model) { + List usuarioLs = this.usuarioService.findAllUsuario(); + model.put("usuarioLs", usuarioLs); + return "usuarios/usuariosList"; + } + + @GetMapping("/administrators/usuarios/{username}") + public String processShowForm(@PathVariable("username") String username, Map model) { + Usuario usuario = this.usuarioService.findByUsername(username); + model.put("usuario", usuario); + return "usuarios/usuariosShow"; + } + + @GetMapping(value = "/administrators/usuarios/{username}/disable") + public String disableUsuario(@PathVariable("username") final String username, final ModelMap model) { + + Usuario usuario = this.usuarioService.findByUsername(username); + 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) { + + Usuario usuario = this.usuarioService.findByUsername(username); + usuario.getUsuar().setEnabled(false); + this.usuarioService.saveUsuario(usuario); + return "redirect:/administrators/usuarios"; + } +} diff --git a/src/main/java/org/springframework/cheapy/web/UsuarioController.java b/src/main/java/org/springframework/cheapy/web/UsuarioController.java index 08d6185f8..4fffd6ff6 100644 --- a/src/main/java/org/springframework/cheapy/web/UsuarioController.java +++ b/src/main/java/org/springframework/cheapy/web/UsuarioController.java @@ -21,7 +21,7 @@ import org.springframework.web.bind.annotation.PostMapping; @Controller public class UsuarioController { - private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuariosForm"; + private static final String VIEWS_USUARIO_CREATE_OR_UPDATE_FORM = "usuarios/createOrUpdateUsuarioForm"; private final UsuarioService usuarioService; diff --git a/src/main/resources/db/mysql/data.sql b/src/main/resources/db/mysql/data.sql index 5f2794626..dae325921 100644 --- a/src/main/resources/db/mysql/data.sql +++ b/src/main/resources/db/mysql/data.sql @@ -9,22 +9,23 @@ INSERT INTO owners VALUES (8, 'Maria', 'Escobito', '345 Maple St.', 'Madison', ' INSERT INTO owners VALUES (9, 'David', 'Schroeder', '2749 Blackhawk Trail', 'Madison', '6085559435'); INSERT INTO owners VALUES (10, 'Carlos', 'Estaban', '2335 Independence La.', 'Waunakee', '6085555487'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','admin','admin', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('admin','admin', TRUE ); INSERT INTO authorities VALUES ('admin','admin'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','manoli','manoli', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('manoli','manoli', TRUE ); INSERT INTO authorities VALUES ('manoli','client'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','david','david', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('david','david', TRUE ); INSERT INTO authorities VALUES ('david','client'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','paco','paco', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('paco','paco', TRUE ); INSERT INTO authorities VALUES ('paco','usuario'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','lolo','lolo', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('lolo','lolo', TRUE ); INSERT INTO authorities VALUES ('lolo','usuario'); -INSERT INTO users (dtype,username,password,enabled) VALUES ('User','pepe','pepe', TRUE ); +INSERT INTO users (username,password,enabled) VALUES ('pepe','pepe', TRUE ); INSERT INTO authorities VALUES ('pepe','usuario'); -INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (1, 'admin', 'admin', 'admin', 'C/admin', '000000000', 'admin@gmail.com','admin'); +INSERT INTO administrators (id, username) VALUES (1, 'admin'); + INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (2, 'Paco', 'Naranjo', '21154416G', 'C/Esperanza', '666973647', 'Paco@gmail.com','paco'); INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (3, 'Lolo', 'Lopez', '25486596L', 'C/Macarena', '690670547' ,'Lolo@gmail.com','lolo'); INSERT INTO usuarios (id, nombre, apellidos, dni, direccion, telefono, email, username) VALUES (4, 'Pepe', 'Lopez', '12456776V', 'C/Macarena', '690670547', 'Pepe@gmail.com','pepe'); diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuarioForm.jsp similarity index 100% rename from src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuariosForm.jsp rename to src/main/webapp/WEB-INF/jsp/usuarios/createOrUpdateUsuarioForm.jsp diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/usuariosList.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosList.jsp new file mode 100644 index 000000000..297978c7e --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosList.jsp @@ -0,0 +1,57 @@ +<%@ 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" %> + + + +

+ + +

No hay ningun usuario.

+
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + + + +
+ +
+
+
+
diff --git a/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp index 4e5e6b962..2feff3623 100644 --- a/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/usuarios/usuariosShow.jsp @@ -56,6 +56,18 @@ Desactivar usuario + + +
+ + + + + +
+
diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index 37142a352..39548df42 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -43,6 +43,14 @@ + + +
+ +
+
From b6ac934baeec978ef5ddec654641dbcaff734c2c Mon Sep 17 00:00:00 2001 From: Thiloparn <48439369+Thiloparn@users.noreply.github.com> Date: Fri, 9 Apr 2021 18:47:37 +0200 Subject: [PATCH 6/7] Arreglo de fallo en travis --- .../springframework/cheapy/repository/UsuarioRepository.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java index aac72ae94..87b11c04d 100644 --- a/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/UsuarioRepository.java @@ -1,5 +1,7 @@ package org.springframework.cheapy.repository; +import java.util.List; + import org.springframework.cheapy.model.Usuario; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.Repository; From c51cd0acf78a9f47922a12a5cf7d6bf2e67c3a06 Mon Sep 17 00:00:00 2001 From: abemorcardc Date: Fri, 9 Apr 2021 20:52:22 +0200 Subject: [PATCH 7/7] Administrador lista, muestra y borra clientes funcional --- .../configuration/SecurityConfiguration.java | 2 +- .../cheapy/repository/ClientRepository.java | 7 +++ .../cheapy/service/ClientService.java | 12 +++- .../cheapy/web/AdministratorController.java | 46 ++++++++++++---- .../webapp/WEB-INF/jsp/clients/clientShow.jsp | 15 ++++- .../WEB-INF/jsp/clients/clientsList.jsp | 55 +++++++++++++++++++ src/main/webapp/WEB-INF/jsp/welcome.jsp | 8 --- src/main/webapp/WEB-INF/tags/menu.tag | 14 +++++ 8 files changed, 135 insertions(+), 24 deletions(-) create mode 100644 src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp diff --git a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java index abf0c8cd3..651272a81 100644 --- a/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java +++ b/src/main/java/org/springframework/cheapy/configuration/SecurityConfiguration.java @@ -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") diff --git a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java index 54c696f00..ac016a82c 100644 --- a/src/main/java/org/springframework/cheapy/repository/ClientRepository.java +++ b/src/main/java/org/springframework/cheapy/repository/ClientRepository.java @@ -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 { @Transactional(readOnly = true) Client findByUsername(String username); + @Query("SELECT client FROM Client client") + @Transactional(readOnly = true) + List findAllClient(); + + void save(Client client); } diff --git a/src/main/java/org/springframework/cheapy/service/ClientService.java b/src/main/java/org/springframework/cheapy/service/ClientService.java index c8404afb9..a92d25a75 100644 --- a/src/main/java/org/springframework/cheapy/service/ClientService.java +++ b/src/main/java/org/springframework/cheapy/service/ClientService.java @@ -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 findAllClient() throws DataAccessException { + return this.clientRepository.findAllClient(); + } } diff --git a/src/main/java/org/springframework/cheapy/web/AdministratorController.java b/src/main/java/org/springframework/cheapy/web/AdministratorController.java index f1c9b77dc..d5e001f77 100644 --- a/src/main/java/org/springframework/cheapy/web/AdministratorController.java +++ b/src/main/java/org/springframework/cheapy/web/AdministratorController.java @@ -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 model) { + List clientLs = this.clientService.findAllClient(); + model.put("clientLs", clientLs); + return "clients/clientsList"; + } @GetMapping("/administrators/usuarios/{username}") - public String processShowForm(@PathVariable("username") String username, Map model) { + public String processUsuarioShowForm(@PathVariable("username") String username, Map 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 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"; + } } diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp index 398ba4d07..66ff86179 100644 --- a/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp +++ b/src/main/webapp/WEB-INF/jsp/clients/clientShow.jsp @@ -67,11 +67,20 @@ - - - + + +
+ + + + + +
+
diff --git a/src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp b/src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp new file mode 100644 index 000000000..464b7a0ec --- /dev/null +++ b/src/main/webapp/WEB-INF/jsp/clients/clientsList.jsp @@ -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" %> + + + +

+ + +

No hay ningun Cliente.

+
+ + + + + + + + + + + + + + + + + + + + + + +
+ + + + + + + + + +
+ +
+
+
+
diff --git a/src/main/webapp/WEB-INF/jsp/welcome.jsp b/src/main/webapp/WEB-INF/jsp/welcome.jsp index 580f03987..43be3e422 100644 --- a/src/main/webapp/WEB-INF/jsp/welcome.jsp +++ b/src/main/webapp/WEB-INF/jsp/welcome.jsp @@ -48,14 +48,6 @@ - - -
- -
-
diff --git a/src/main/webapp/WEB-INF/tags/menu.tag b/src/main/webapp/WEB-INF/tags/menu.tag index 40f426e7e..97f1d6ced 100644 --- a/src/main/webapp/WEB-INF/tags/menu.tag +++ b/src/main/webapp/WEB-INF/tags/menu.tag @@ -39,6 +39,20 @@ Mis ofertas + + + + + Clientes + + + + + + + Usuarios + +