Registro de cliente completo y funcional

This commit is contained in:
Abraham 2021-04-11 14:19:45 +02:00
parent 1f6805e89c
commit f654cb3d13
9 changed files with 483 additions and 13 deletions

View file

@ -5,6 +5,8 @@ import java.util.Set;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
@ -30,6 +32,9 @@ public class Client extends BaseEntity {
@NotEmpty
private String address;
@Enumerated(value = EnumType.STRING)
private Municipio municipio;
// Hora de apertura del local
@NotBlank
@ -47,8 +52,8 @@ public class Client extends BaseEntity {
private String description;
// Codigo de activacion de cuenta
@NotEmpty
private String code;
// @NotEmpty
// private String code;
@NotEmpty
private String food;
@ -56,6 +61,10 @@ public class Client extends BaseEntity {
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "username", referencedColumnName = "username")
private User usuar;
@OneToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "code", referencedColumnName = "code")
private Code cod;
@OneToMany
private List<FoodOffer> foodOffers;
@ -125,12 +134,20 @@ public class Client extends BaseEntity {
this.description = description;
}
public String getCode() {
return code;
public Municipio getMunicipio() {
return municipio;
}
public void setCode(String code) {
this.code = code;
public void setMunicipio(Municipio municipio) {
this.municipio = municipio;
}
public Code getCode() {
return cod;
}
public void setCode(Code code) {
this.cod = code;
}
public String getFood() {

View file

@ -0,0 +1,39 @@
package org.springframework.cheapy.model;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
@Entity
@Table(name = "codes")
public class Code extends BaseEntity{
/**
*
*/
private static final long serialVersionUID = 1L;
@NotNull
String code;
Boolean activo;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public Boolean getActivo() {
return activo;
}
public void setActivo(Boolean activo) {
this.activo = activo;
}
}

View file

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

View file

@ -0,0 +1,19 @@
package org.springframework.cheapy.repository;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.Code;
import org.springframework.cheapy.model.Usuario;
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 CodeRepository extends Repository<Code, Integer> {
void save(Code code);
@Query("SELECT code FROM Code code WHERE code.code =:code")
@Transactional(readOnly = true)
Code findCodeByCode(String code);
}

View file

@ -1,8 +1,13 @@
package org.springframework.cheapy.service;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.Code;
import org.springframework.cheapy.model.Usuario;
import org.springframework.cheapy.repository.ClientRepository;
import org.springframework.cheapy.repository.CodeRepository;
import org.springframework.dao.DataAccessException;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
@ -13,10 +18,12 @@ import org.springframework.transaction.annotation.Transactional;
public class ClientService {
private ClientRepository clientRepository;
private CodeRepository codeRepository;
@Autowired
public ClientService(final ClientRepository clientRepository) {
public ClientService(final ClientRepository clientRepository, CodeRepository codeRepository) {
this.clientRepository = clientRepository;
this.codeRepository = codeRepository;
}
@Transactional
@ -25,5 +32,18 @@ public class ClientService {
String username = authentication.getName();
return this.clientRepository.findByUsername(username);
}
public void saveClient(final Client client) throws DataAccessException {
this.clientRepository.save(client);
}
public void saveCode(Code code) throws DataAccessException{
this.codeRepository.save(code);
}
public Code findCodeByCode(String code) {
return this.codeRepository.findCodeByCode(code);
}
}

View file

@ -7,6 +7,8 @@ import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cheapy.model.Authorities;
import org.springframework.cheapy.model.Client;
import org.springframework.cheapy.model.Code;
import org.springframework.cheapy.model.Municipio;
import org.springframework.cheapy.model.Owner;
import org.springframework.cheapy.model.User;
@ -56,7 +58,7 @@ public class SingUpController {
}
@GetMapping("/users/new")
public String initCreationForm(Map<String, Object> model) {
public String singUpUserForm(Map<String, Object> model) {
Usuario usuario = new Usuario();
User user=new User();
@ -69,7 +71,7 @@ public class SingUpController {
}
@PostMapping("/users/new")
public String processCreationForm(/*@Valid User user,*/ @Valid Usuario usuario, BindingResult result) {
public String singUpUserForm(/*@Valid User user,*/ @Valid Usuario usuario, BindingResult result) {
Authorities auth=new Authorities();
User user= usuario.getUsuar();
user.setEnabled(true);
@ -90,6 +92,48 @@ public class SingUpController {
}
}
@GetMapping("/clients/new")
public String singUpClientForm(Map<String, Object> model) {
Client cliente = new Client();
User user=new User();
cliente.setUsuar(user);
model.put("municipio", Municipio.values());
model.put("cliente", cliente);
//model.put("user", user);
return "singup/singUpClient";
}
@PostMapping("/clients/new")
public String singUpClientForm(/*@Valid User user,*/ @Valid Client cliente, BindingResult result) {
Authorities auth=new Authorities();
System.out.println(cliente.getCode().getCode());
String cod=cliente.getCode().getCode();
Code code=this.clientService.findCodeByCode(cod);
User user= cliente.getUsuar();
user.setEnabled(true);
cliente.setUsuar(user);
auth.setUsername(user.getUsername());
auth.setAuthority("client");
if (result.hasErrors()) {
return "singup/singUpClient";
}else if(code.getActivo().equals(false)) {
return "error";
}else {
//auth.setId(1);
//this.authoritiesService.saveAuthorities(auth);
code.setActivo(false);
this.clientService.saveCode(code);
cliente.setCode(code);
this.clientService.saveClient(cliente);
this.userService.saveUser(user);
this.authoritiesService.saveAuthorities(cliente.getUsuar().getUsername(), "client");
return "redirect:/";
}
}
// @GetMapping("/owners/find")
// public String initFindForm(Map<String, Object> model) {
// model.put("owner", new Owner());

View file

@ -45,8 +45,13 @@ INSERT INTO usuarios (id, nombre, apellidos, direccion, municipio, email, userna
INSERT INTO usuarios (id, nombre, apellidos, direccion, municipio, email, username) VALUES (3, 'Lolo', 'Lopez', 'C/Macarena', 'dosHermanas', 'Lolo@gmail.com','lolo');
INSERT INTO usuarios (id, nombre, apellidos, direccion, municipio, email, username) VALUES (4, 'Pepe', 'Lopez', 'C/Macarena', 'carmona', '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');
INSERT INTO codes (id,code,activo) VALUES (1,'code1',FALSE);
INSERT INTO codes (id,code,activo) VALUES (2,'code2',FALSE);
INSERT INTO codes (id,code,activo) VALUES (3,'code3',TRUE);
INSERT INTO codes (id,code,activo) VALUES (4,'code4',TRUE);
INSERT INTO clients (id, name, email, address, municipio, init, finish, telephone, description, food, username, code) VALUES (1,'bar manoli','manoli@gmail.com','C/Betis', 'sevilla','10:00','22:00','608726190', 'description 1', 'ESPAÑOLA','manoli', 'code1');
INSERT INTO clients (id, name, email, address, init, municipio, finish, telephone, description, food, username, code) VALUES (2,'bar david','david@gmail.com','C/Sevilla', 'dosHermanas','09:30','22:00','608726190', 'description 2', 'americana','david', 'code2');
INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-14 12:00:00', '2021-08-15 12:00:00', 'FO-1', 'inactive', 1, 'macarrones', 15);
INSERT INTO food_offers(start, end, code, status, client_id, food, discount) VALUES ('2021-08-15 12:00:00', '2021-08-16 12:00:00', 'FO-2', 'active', 1, 'macarrones con tomate', 10);

View file

@ -0,0 +1,317 @@
<%@ 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" %>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<link href='https://fonts.googleapis.com/css?family=Lobster' rel='stylesheet'>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<!-- %@ taglib prefix="sec" uri="http://www.springframework.org/security/tags" %-->
<style>
body {
font-family: "montserratregular", sans-serif;
height: 100%;
}
a {
color: #92badd;
display:inline-block;
text-decoration: none;
font-weight: 400;
}
h2 {
text-align: center;
font-size: 16px;
font-weight: 600;
text-transform: uppercase;
display:inline-block;
margin: 40px 8px 10px 8px;
color: #cccccc;
}
/* STRUCTURE */
.wrapper {
display: flex;
align-items: center;
flex-direction: column;
justify-content: center;
width: 100%;
min-height: 100%;
padding: 20px;
}
#formContent {
-webkit-border-radius: 10px 10px 10px 10px;
border-radius: 10px 10px 10px 10px;
background: #fff;
padding: 30px;
width: 90%;
max-width: 450px;
position: relative;
padding: 0px;
-webkit-box-shadow: 0 30px 60px 0 rgba(0,0,0,0.3);
box-shadow: 0 30px 60px 0 rgba(0,0,0,0.3);
text-align: center;
}
#formFooter {
background-color: #f6f6f6;
border-top: 1px solid #dce8f1;
padding: 25px;
text-align: center;
-webkit-border-radius: 0 0 10px 10px;
border-radius: 0 0 10px 10px;
}
/* TABS */
h2.inactive {
color: #cccccc;
}
h2.active {
color: #0d0d0d;
border-bottom: 2px solid #5fbae9;
}
/* FORM TYPOGRAPHY*/
input[type=button], input[type=submit], input[type=reset] {
background-color: #56baed;
border: none;
color: white;
padding: 15px 80px;
text-align: center;
text-decoration: none;
display: inline-block;
text-transform: uppercase;
font-size: 13px;
-webkit-box-shadow: 0 10px 30px 0 rgba(95,186,233,0.4);
box-shadow: 0 10px 30px 0 rgba(95,186,233,0.4);
-webkit-border-radius: 5px 5px 5px 5px;
border-radius: 5px 5px 5px 5px;
margin: 5px 20px 40px 20px;
-webkit-transition: all 0.3s ease-in-out;
-moz-transition: all 0.3s ease-in-out;
-ms-transition: all 0.3s ease-in-out;
-o-transition: all 0.3s ease-in-out;
transition: all 0.3s ease-in-out;
}
input[type=button]:hover, input[type=submit]:hover, input[type=reset]:hover {
background-color: #39ace7;
}
input[type=button]:active, input[type=submit]:active, input[type=reset]:active {
-moz-transform: scale(0.95);
-webkit-transform: scale(0.95);
-o-transform: scale(0.95);
-ms-transform: scale(0.95);
transform: scale(0.95);
}
input[type=text], input[type=password] {
background-color: #f6f6f6;
border: none;
color: #0d0d0d;
padding: 15px 32px;
text-align: center;
text-decoration: none;
display: inline-block;
font-size: 16px;
margin: 5px;
width: 85%;
border: 2px solid #f6f6f6;
-webkit-transition: all 0.5s ease-in-out;
-moz-transition: all 0.5s ease-in-out;
-ms-transition: all 0.5s ease-in-out;
-o-transition: all 0.5s ease-in-out;
transition: all 0.5s ease-in-out;
-webkit-border-radius: 5px 5px 5px 5px;
border-radius: 5px 5px 5px 5px;
}
input[type=text]:focus {
background-color: #fff;
border-bottom: 2px solid #5fbae9;
}
input[type=text]:placeholder {
color: #cccccc;
}
/* ANIMATIONS */
/* Simple CSS3 Fade-in-down Animation */
.fadeInDown {
-webkit-animation-name: fadeInDown;
animation-name: fadeInDown;
-webkit-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
animation-fill-mode: both;
}
@-webkit-keyframes fadeInDown {
0% {
opacity: 0;
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
@keyframes fadeInDown {
0% {
opacity: 0;
-webkit-transform: translate3d(0, -100%, 0);
transform: translate3d(0, -100%, 0);
}
100% {
opacity: 1;
-webkit-transform: none;
transform: none;
}
}
/* Simple CSS3 Fade-in Animation */
@-webkit-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@-moz-keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
@keyframes fadeIn { from { opacity:0; } to { opacity:1; } }
.fadeIn {
opacity:0;
-webkit-animation:fadeIn ease-in 1;
-moz-animation:fadeIn ease-in 1;
animation:fadeIn ease-in 1;
-webkit-animation-fill-mode:forwards;
-moz-animation-fill-mode:forwards;
animation-fill-mode:forwards;
-webkit-animation-duration:1s;
-moz-animation-duration:1s;
animation-duration:1s;
}
.fadeIn.first {
-webkit-animation-delay: 0.4s;
-moz-animation-delay: 0.4s;
animation-delay: 0.4s;
}
.fadeIn.second {
-webkit-animation-delay: 0.6s;
-moz-animation-delay: 0.6s;
animation-delay: 0.6s;
}
.fadeIn.third {
-webkit-animation-delay: 0.8s;
-moz-animation-delay: 0.8s;
animation-delay: 0.8s;
}
.fadeIn.fourth {
-webkit-animation-delay: 1s;
-moz-animation-delay: 1s;
animation-delay: 1s;
}
/* Simple CSS3 Fade-in Animation */
.underlineHover:after {
display: block;
left: 0;
bottom: -10px;
width: 0;
height: 2px;
background-color: #56baed;
content: "";
transition: width 0.2s;
}
.underlineHover:hover {
color: #0d0d0d;
}
.underlineHover:hover:after{
width: 100%;
}
/* OTHERS */
*:focus {
outline: none;
}
#icon {
width:60%;
}
</style>
<cheapy:layout pageName="singUp">
<h2 style="text-align:center;padding:5px">
<fmt:message key="new"/><fmt:message key="user"/>
</h2>
<form:form modelAttribute="cliente" class="form-horizontal"
id="add-foodOffer-form">
<div class="form-group has-feedback">
<cheapy:inputField label="Nombre" placeholder="Ponga aqui su nombre"
name="name" />
<cheapy:inputField label="Direccion" placeholder="Ponga aqui su dirección"
name="address" />
<div class="form-group">
<label>Municipio: </label>
<select name="municipio">
<c:forEach items="${municipio}" var="entry">
<option value="${entry}">${entry}</option>
</c:forEach>
</select>
</div>
<cheapy:inputField label="Hora de apertura" placeholder="Ponga aqui su hora de apertura"
name="init" />
<cheapy:inputField label="Hora de cierre" placeholder="Ponga aqui su hora de cierre"
name="finish" />
<cheapy:inputField label="Teléfono" placeholder="Ponga aqui el teléfono del local"
name="telephone" />
<cheapy:inputField label="Descripción" placeholder="Ponga aqui su descripción"
name="description" />
<cheapy:inputField label="Email" placeholder="Ponga aqui su email"
name="email" />
<cheapy:inputField label="Tipo de comida" placeholder="Indique que tipo de comida sirve su negocio"
name="food" />
<cheapy:inputField label="Nombre de usuario" placeholder="Ponga aqui su nombre de usuario"
name="usuar.username" />
<cheapy:inputField label="Contraseña" placeholder="Ponga aqui su contraseña"
name="usuar.password" />
<cheapy:inputField label="Código de activación" placeholder="Ponga aqui el código que se le suministro al firmar el contrato"
name="code.code" />
<input type="submit" class="fadeIn fourth" value="Registrarse">
</div>
</form:form>
</cheapy:layout>

View file

@ -64,7 +64,11 @@
<!--<li><a href="<c:url value="/users/new" />">Register</a></li>-->
</sec:authorize>
<sec:authorize access="!isAuthenticated()">
<li><a href="<c:url value="/users/new" />">Registrarse</a></li>
<li><a href="<c:url value="/users/new" />">Registrarse Usuario</a></li>
<!--<li><a href="<c:url value="/users/new" />">Register</a></li>-->
</sec:authorize>
<sec:authorize access="!isAuthenticated()">
<li><a href="<c:url value="/clients/new" />">Registrarse Cliente</a></li>
<!--<li><a href="<c:url value="/users/new" />">Register</a></li>-->
</sec:authorize>
<sec:authorize access="isAuthenticated()">