diff --git a/pom.xml b/pom.xml
index f4a4839..d0422f3 100644
--- a/pom.xml
+++ b/pom.xml
@@ -5,7 +5,7 @@
org.springframework.boot
spring-boot-starter-parent
- 3.0.0
+ 2.7.5
com.davidorellana
@@ -17,16 +17,43 @@
11
+
org.springframework.boot
spring-boot-starter-web
+
org.springframework.boot
spring-boot-starter-data-mongodb
+
+
+ jakarta.xml.bind
+ jakarta.xml.bind-api
+ 2.3.2
+
+
+
+ org.glassfish.jaxb
+ jaxb-runtime
+ 2.3.2
+
+
+
+ io.jsonwebtoken
+ jjwt
+ 0.9.1
+
+
+
+ org.springframework.boot
+ spring-boot-starter-security
+
+
+
org.springframework.boot
spring-boot-starter-test
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/configuration/ConfigurationBeans.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/configuration/ConfigurationBeans.java
new file mode 100644
index 0000000..5d52d06
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/configuration/ConfigurationBeans.java
@@ -0,0 +1,15 @@
+package com.davidorellana.bookingsystemrestapi.auth.configuration;
+
+import com.davidorellana.bookingsystemrestapi.auth.security.jwt.OperationJwt;
+import com.davidorellana.bookingsystemrestapi.auth.security.jwt.OperationJwtImpl;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+@Configuration
+public class ConfigurationBeans {
+
+ @Bean
+ public OperationJwt generationToken() {
+ return new OperationJwtImpl();
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/controller/AuthenticationController.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/controller/AuthenticationController.java
new file mode 100644
index 0000000..9d86420
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/controller/AuthenticationController.java
@@ -0,0 +1,43 @@
+package com.davidorellana.bookingsystemrestapi.auth.controller;
+
+import com.davidorellana.bookingsystemrestapi.auth.dto.LoginDto;
+import com.davidorellana.bookingsystemrestapi.auth.dto.TokenDto;
+import com.davidorellana.bookingsystemrestapi.auth.security.jwt.OperationJwt;
+import com.davidorellana.bookingsystemrestapi.user.model.data.User;
+import com.davidorellana.bookingsystemrestapi.user.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.web.bind.annotation.*;
+
+@CrossOrigin(origins = "*", allowedHeaders = "*")
+@RestController
+@RequestMapping("/v1/auth")
+public class AuthenticationController {
+
+ private final OperationJwt operationJwt;
+ private final UserService userService;
+
+ @Autowired
+ private AuthenticationManager authenticationManager;
+
+
+ @Autowired
+ public AuthenticationController(OperationJwt operationJwt, UserService userService) {
+ this.operationJwt = operationJwt;
+ this.userService = userService;
+ }
+
+ @PostMapping
+ public ResponseEntity generateJwt(@RequestBody LoginDto loginDto) {
+ User userFound = userService.findUserByEmail(loginDto.getEmail());
+ if (userFound != null) {
+ authenticationManager.authenticate(new UsernamePasswordAuthenticationToken(loginDto.getEmail(), loginDto.getPassword()));
+ TokenDto tokenDto = operationJwt.generateTokenDto(userFound);
+ return new ResponseEntity<>(tokenDto, HttpStatus.OK);
+ }
+ return new ResponseEntity("Entry denied, due to incorrect credentials.", HttpStatus.NOT_FOUND);
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/LoginDto.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/LoginDto.java
new file mode 100644
index 0000000..46bb74f
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/LoginDto.java
@@ -0,0 +1,35 @@
+package com.davidorellana.bookingsystemrestapi.auth.dto;
+
+import java.io.Serializable;
+
+public class LoginDto implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private String email;
+ private String password;
+
+ public LoginDto() {
+ }
+
+ public LoginDto(String email, String password) {
+ this.email = email;
+ this.password = password;
+ }
+
+ public String getEmail() {
+ return email;
+ }
+
+ public void setEmail(String email) {
+ this.email = email;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+}
\ No newline at end of file
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/TokenDto.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/TokenDto.java
new file mode 100644
index 0000000..8b7736e
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/dto/TokenDto.java
@@ -0,0 +1,35 @@
+package com.davidorellana.bookingsystemrestapi.auth.dto;
+
+import java.io.Serializable;
+import java.util.Date;
+
+public class TokenDto implements Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private String jwt;
+ private Date expirationJwt;
+
+ public TokenDto() { }
+
+ public TokenDto(String jwt, Date expirationJwt) {
+ this.jwt = jwt;
+ this.expirationJwt = expirationJwt;
+ }
+
+ public String getJwt() {
+ return jwt;
+ }
+
+ public void setJwt(String jwt) {
+ this.jwt = jwt;
+ }
+
+ public Date getExpirationJwt() {
+ return expirationJwt;
+ }
+
+ public void setExpirationJwt(Date expirationJwt) {
+ this.expirationJwt = expirationJwt;
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/SecurityConfiguration.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/SecurityConfiguration.java
new file mode 100644
index 0000000..37656d5
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/SecurityConfiguration.java
@@ -0,0 +1,50 @@
+package com.davidorellana.bookingsystemrestapi.auth.security;
+
+import com.davidorellana.bookingsystemrestapi.auth.security.jwt.JwtRequestFilter;
+import com.davidorellana.bookingsystemrestapi.auth.service.UserDetailServiceAuth;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.context.annotation.Bean;
+import org.springframework.http.HttpMethod;
+import org.springframework.security.authentication.AuthenticationManager;
+import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
+import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
+import org.springframework.security.config.annotation.web.builders.HttpSecurity;
+import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
+import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
+import org.springframework.security.config.http.SessionCreationPolicy;
+import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
+
+@EnableWebSecurity
+@EnableGlobalMethodSecurity(securedEnabled = true, jsr250Enabled = true, prePostEnabled = true)
+public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
+
+ @Autowired
+ private UserDetailServiceAuth userDetailServiceAuth;
+
+ @Autowired
+ private JwtRequestFilter jwtRequestFilter;
+
+ @Override
+ protected void configure(AuthenticationManagerBuilder auth) throws Exception {
+ auth.userDetailsService(userDetailServiceAuth);
+ }
+
+ @Override
+ protected void configure(HttpSecurity http) throws Exception {
+ http.csrf().disable()
+ .authorizeRequests()
+ .antMatchers(HttpMethod.POST,"/v1/users" ).permitAll()
+ .antMatchers(HttpMethod.POST, "/v1/auth").permitAll()
+ .anyRequest().authenticated()
+ .and()
+ .sessionManagement()
+ .sessionCreationPolicy(SessionCreationPolicy.STATELESS);
+ http.addFilterBefore(jwtRequestFilter, UsernamePasswordAuthenticationFilter.class);
+ }
+
+ @Bean
+ @Override
+ public AuthenticationManager authenticationManagerBean() throws Exception {
+ return super.authenticationManagerBean();
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/JwtRequestFilter.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/JwtRequestFilter.java
new file mode 100644
index 0000000..cfb69ea
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/JwtRequestFilter.java
@@ -0,0 +1,59 @@
+package com.davidorellana.bookingsystemrestapi.auth.security.jwt;
+
+import com.davidorellana.bookingsystemrestapi.auth.service.UserDetailServiceAuth;
+import com.davidorellana.bookingsystemrestapi.user.model.data.User;
+import com.davidorellana.bookingsystemrestapi.user.service.UserService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.http.HttpHeaders;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.web.authentication.WebAuthenticationDetailsSource;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+
+import javax.servlet.FilterChain;
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+
+@Component
+public class JwtRequestFilter extends OncePerRequestFilter {
+
+ @Autowired
+ private OperationJwt operationJwt;
+
+ @Autowired
+ private UserService userService;
+
+ @Autowired
+ private UserDetailServiceAuth userDetailServiceAuth;
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
+ String authorizationHeader = request.getHeader(HttpHeaders.AUTHORIZATION);
+ if (authorizationHeader != null && authorizationHeader.startsWith("Bearer")){
+ String jwt = authorizationHeader.substring(7);
+ String idUser = operationJwt.extractSubject(jwt);
+
+ if (idUser != null && SecurityContextHolder.getContext().getAuthentication() == null){
+ User userFound = userService.findUserById(idUser);
+
+ if (operationJwt.validateJwt(jwt, userFound)){
+ UserDetails userDetails = userDetailServiceAuth.loadUserByUsername(userFound.getEmail());
+ UsernamePasswordAuthenticationToken authenticationToken =
+ new UsernamePasswordAuthenticationToken(userDetails
+ ,null
+ ,userDetails.getAuthorities());
+ authenticationToken.setDetails(new WebAuthenticationDetailsSource()
+ .buildDetails(request));
+
+ SecurityContextHolder.getContext().setAuthentication(authenticationToken);
+ }
+ }
+ }
+
+ filterChain.doFilter(request, response);
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwt.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwt.java
new file mode 100644
index 0000000..6793c1d
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwt.java
@@ -0,0 +1,16 @@
+package com.davidorellana.bookingsystemrestapi.auth.security.jwt;
+
+import com.davidorellana.bookingsystemrestapi.auth.dto.TokenDto;
+import com.davidorellana.bookingsystemrestapi.user.model.data.User;
+import io.jsonwebtoken.Claims;
+
+import java.util.Calendar;
+
+public interface OperationJwt {
+
+ String generateJwt(User user, Calendar expirationDate);
+ TokenDto generateTokenDto(User user);
+ Boolean validateJwt(String jwt, User user);
+ Claims returnClaims(String jwt);
+ String extractSubject(String jwt);
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwtImpl.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwtImpl.java
new file mode 100644
index 0000000..7991767
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/security/jwt/OperationJwtImpl.java
@@ -0,0 +1,58 @@
+package com.davidorellana.bookingsystemrestapi.auth.security.jwt;
+
+import com.davidorellana.bookingsystemrestapi.auth.dto.TokenDto;
+import com.davidorellana.bookingsystemrestapi.user.model.data.User;
+import io.jsonwebtoken.Claims;
+import io.jsonwebtoken.Jwts;
+import io.jsonwebtoken.SignatureAlgorithm;
+import org.springframework.beans.factory.annotation.Value;
+
+import java.util.Calendar;
+import java.util.Date;
+
+public class OperationJwtImpl implements OperationJwt {
+
+ @Value("${KEY_SECRET}")
+ private String keySecret;
+
+ private final Integer MINUTES_JWT_EXPIRATION = 45;
+
+ @Override
+ public String generateJwt(User user, Calendar expirationDate) {
+
+ return Jwts.builder()
+ .setSubject(user.getId())
+ .claim("name", user.getName())
+ .setIssuedAt(new Date())
+ .setExpiration(expirationDate.getTime())
+ .signWith(SignatureAlgorithm.HS256, keySecret)
+ .compact();
+ }
+
+ @Override
+ public TokenDto generateTokenDto(User user) {
+ Calendar dateExpiration = Calendar.getInstance();
+ dateExpiration.add(Calendar.MINUTE, MINUTES_JWT_EXPIRATION);
+ String jwt = generateJwt(user, dateExpiration);
+ return new TokenDto(jwt, dateExpiration.getTime());
+ }
+
+ @Override
+ public Boolean validateJwt(String jwt, User user) {
+ Boolean isJwtExpired = returnClaims(jwt).getExpiration().before(new Date());
+ return user.getId().equals(extractSubject(jwt)) && !isJwtExpired;
+ }
+
+ @Override
+ public Claims returnClaims(String jwt) {
+ return Jwts.parser()
+ .setSigningKey(keySecret)
+ .parseClaimsJws(jwt)
+ .getBody();
+ }
+
+ @Override
+ public String extractSubject(String jwt) {
+ return returnClaims(jwt).getSubject();
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/auth/service/UserDetailServiceAuth.java b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/service/UserDetailServiceAuth.java
new file mode 100644
index 0000000..e5233c7
--- /dev/null
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/auth/service/UserDetailServiceAuth.java
@@ -0,0 +1,26 @@
+package com.davidorellana.bookingsystemrestapi.auth.service;
+
+import com.davidorellana.bookingsystemrestapi.user.model.data.User;
+import com.davidorellana.bookingsystemrestapi.user.repository.UserRepositoryDao;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.security.core.userdetails.UserDetails;
+import org.springframework.security.core.userdetails.UserDetailsService;
+import org.springframework.security.core.userdetails.UsernameNotFoundException;
+import org.springframework.stereotype.Service;
+
+import java.util.ArrayList;
+
+@Service
+public class UserDetailServiceAuth implements UserDetailsService {
+
+ @Autowired
+ private UserRepositoryDao userRepositoryDao;
+
+ @Override
+ public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
+ User userFound = userRepositoryDao.findUserByEmail(email);
+ return new org.springframework.security.core.userdetails.User(
+ userFound.getEmail(), "{noop}" + userFound.getPassword(), new ArrayList<>()
+ );
+ }
+}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/booking/repository/BookingRepositoryImpl.java b/src/main/java/com/davidorellana/bookingsystemrestapi/booking/repository/BookingRepositoryImpl.java
index 02d7372..0a7a2bb 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/booking/repository/BookingRepositoryImpl.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/booking/repository/BookingRepositoryImpl.java
@@ -36,10 +36,9 @@ public Booking findBookingById(String id) {
@Override
public Booking createBooking(BookingDto bookingDto) {
- if (bookingDto.getBookingStartDate().isAfter(LocalDate.now())) {
+ if (bookingDto.getBookingStartDate().isAfter(LocalDate.now().minusDays(1))) {
if (bookingDto.getBookingEndDate().isAfter(bookingDto.getBookingStartDate())) {
Booking booking = new Booking(bookingDto);
- bookingMongoRepository.insert(booking);
return bookingMongoRepository.save(booking);
}
}
@@ -50,7 +49,7 @@ public Booking createBooking(BookingDto bookingDto) {
public Booking updateBookingById(String id, BookingDto bookingDto) {
Booking bookingFound = findBookingById(id);
if (bookingFound != null) {
- Boolean comparisonStartDateNowDto = bookingDto.getBookingStartDate().isAfter(LocalDate.now());
+ Boolean comparisonStartDateNowDto = bookingDto.getBookingStartDate().isAfter(LocalDate.now().minusDays(1));
Boolean comparisonStartDateEndDto = bookingDto.getBookingEndDate().isAfter(bookingDto.getBookingStartDate());
Boolean comparisonEndDtoDateStartFound = bookingDto.getBookingEndDate().isAfter(bookingFound.getBookingStartDate());
if (comparisonStartDateNowDto && comparisonStartDateEndDto && comparisonEndDtoDateStartFound) {
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/booking/service/BookingServiceImpl.java b/src/main/java/com/davidorellana/bookingsystemrestapi/booking/service/BookingServiceImpl.java
index b50c454..5b71986 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/booking/service/BookingServiceImpl.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/booking/service/BookingServiceImpl.java
@@ -18,7 +18,6 @@ public BookingServiceImpl(BookingRepositoryDao bookingRepositoryDao) {
this.bookingRepositoryDao = bookingRepositoryDao;
}
-
@Override
public List findAllBookings() {
return bookingRepositoryDao.findAllBookings();
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/controller/UserController.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/controller/UserController.java
index 8a7b7c5..81758ce 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/controller/UserController.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/controller/UserController.java
@@ -10,7 +10,6 @@
import org.springframework.web.bind.annotation.*;
import java.util.List;
-import java.util.Optional;
@CrossOrigin(origins = "*", allowedHeaders = "*")
@RestController
@@ -87,12 +86,12 @@ public ResponseEntity> findUserByNameAndLastName(@PathVariable String
return new ResponseEntity("The user " + name + " " + lastName +", do not exist in the users collection.", HttpStatus.NOT_FOUND);
}
- @GetMapping("/email/{email}")
- public ResponseEntity> findUserByEmail(@PathVariable String email) {
- Optional userByEmailFound = userService.findUserByEmail(email);
- if (userByEmailFound.isPresent()) {
- return new ResponseEntity<>(userByEmailFound, HttpStatus.OK);
+ @GetMapping("/identityCard/{identityCard}")
+ public ResponseEntity findUserByIdentityCard(@PathVariable String identityCard) {
+ User userByIdentityCardFound = userService.findUserByIdentityCard(identityCard);
+ if (userByIdentityCardFound != null) {
+ return new ResponseEntity<>(userByIdentityCardFound, HttpStatus.OK);
}
- return new ResponseEntity("The email " + email + " is not related to any user.", HttpStatus.NOT_FOUND);
+ return new ResponseEntity("The identity Card " + identityCard + " is not related to any user.", HttpStatus.NOT_FOUND);
}
}
\ No newline at end of file
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/data/User.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/data/User.java
index 7e3d159..21bec3c 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/data/User.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/data/User.java
@@ -23,6 +23,7 @@ public class User implements Serializable {
private String identityCard;
@Indexed(unique = true)
private String email;
+ private String password;
public User() { }
@@ -32,6 +33,7 @@ public User(UserDto userDto) {
this.age = userDto.getAge();
this.identityCard = userDto.getIdentityCard();
this.email = userDto.getEmail();
+ this.password = userDto.getPassword();
}
public User(UserUpdatedDto userUpdatedDto) {
@@ -86,11 +88,20 @@ public void setEmail(String email) {
this.email = email;
}
+ public String getPassword() {
+ return password;
+ }
+
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
public void updateUserCollection(UserUpdatedDto userUpdatedDto) {
this.name = userUpdatedDto.getName();
this.lastName = userUpdatedDto.getLastName();
this.age = userUpdatedDto.getAge();
this.email = userUpdatedDto.getEmail();
+ this.password = userUpdatedDto.getPassword();
}
@Override
@@ -102,6 +113,7 @@ public String toString() {
", age=" + age +
", identityCard='" + identityCard + '\'' +
", email='" + email + '\'' +
+ ", password='" + password + '\'' +
'}';
}
@@ -110,11 +122,11 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
- return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(lastName, user.lastName) && Objects.equals(age, user.age) && Objects.equals(identityCard, user.identityCard) && Objects.equals(email, user.email);
+ return Objects.equals(id, user.id) && Objects.equals(name, user.name) && Objects.equals(lastName, user.lastName) && Objects.equals(age, user.age) && Objects.equals(identityCard, user.identityCard) && Objects.equals(email, user.email) && Objects.equals(password, user.password);
}
@Override
public int hashCode() {
- return Objects.hash(id, name, lastName, age, identityCard, email);
+ return Objects.hash(id, name, lastName, age, identityCard, email, password);
}
}
\ No newline at end of file
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserDto.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserDto.java
index 71c5b93..ab14a08 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserDto.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserDto.java
@@ -12,6 +12,7 @@ public class UserDto implements Serializable {
private Integer age;
private String identityCard;
private String email;
+ private String password;
public UserDto() { }
@@ -55,17 +56,12 @@ public void setEmail(String email) {
this.email = email;
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- UserDto userDto = (UserDto) o;
- return Objects.equals(name, userDto.name) && Objects.equals(lastName, userDto.lastName) && Objects.equals(age, userDto.age) && Objects.equals(identityCard, userDto.identityCard) && Objects.equals(email, userDto.email);
+ public String getPassword() {
+ return password;
}
- @Override
- public int hashCode() {
- return Objects.hash(name, lastName, age, identityCard, email);
+ public void setPassword(String password) {
+ this.password = password;
}
@Override
@@ -76,6 +72,20 @@ public String toString() {
", age=" + age +
", identityCard='" + identityCard + '\'' +
", email='" + email + '\'' +
+ ", password='" + password + '\'' +
'}';
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ UserDto userDto = (UserDto) o;
+ return Objects.equals(name, userDto.name) && Objects.equals(lastName, userDto.lastName) && Objects.equals(age, userDto.age) && Objects.equals(identityCard, userDto.identityCard) && Objects.equals(email, userDto.email) && Objects.equals(password, userDto.password);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, lastName, age, identityCard, email, password);
+ }
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserUpdatedDto.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserUpdatedDto.java
index 047f9f3..08d249d 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserUpdatedDto.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/model/dto/UserUpdatedDto.java
@@ -11,6 +11,7 @@ public class UserUpdatedDto implements Serializable {
private String lastName;
private Integer age;
private String email;
+ private String password;
public UserUpdatedDto() { }
@@ -46,26 +47,35 @@ public void setEmail(String email) {
this.email = email;
}
- @Override
- public boolean equals(Object o) {
- if (this == o) return true;
- if (o == null || getClass() != o.getClass()) return false;
- UserUpdatedDto userDto = (UserUpdatedDto) o;
- return Objects.equals(name, userDto.name) && Objects.equals(lastName, userDto.lastName) && Objects.equals(age, userDto.age) && Objects.equals(email, userDto.email);
+ public String getPassword() {
+ return password;
}
- @Override
- public int hashCode() {
- return Objects.hash(name, lastName, age, email);
+ public void setPassword(String password) {
+ this.password = password;
}
@Override
public String toString() {
- return "UserDto{" +
+ return "UserUpdatedDto{" +
"name='" + name + '\'' +
", lastName='" + lastName + '\'' +
", age=" + age +
", email='" + email + '\'' +
+ ", password='" + password + '\'' +
'}';
}
+
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) return true;
+ if (o == null || getClass() != o.getClass()) return false;
+ UserUpdatedDto that = (UserUpdatedDto) o;
+ return Objects.equals(name, that.name) && Objects.equals(lastName, that.lastName) && Objects.equals(age, that.age) && Objects.equals(email, that.email) && Objects.equals(password, that.password);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(name, lastName, age, email, password);
+ }
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryDao.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryDao.java
index cbaf220..c29fcb4 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryDao.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryDao.java
@@ -5,7 +5,6 @@
import com.davidorellana.bookingsystemrestapi.user.model.dto.UserUpdatedDto;
import java.util.List;
-import java.util.Optional;
public interface UserRepositoryDao {
@@ -16,5 +15,6 @@ public interface UserRepositoryDao {
Boolean deleteUserById(String id);
void deleteAllUsers();
List findUserByNameAndLastName(String name, String lastName);
- Optional findUserByEmail(String email);
+ User findUserByEmail(String email);
+ User findUserByIdentityCard(String identityCard);
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryImpl.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryImpl.java
index 150b59b..cb28a45 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryImpl.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/UserRepositoryImpl.java
@@ -37,8 +37,8 @@ public User findUserById(String id) {
@Override
public User createUser(UserDto userDto) {
- Optional user = Optional.of(new User(userDto));
- return userMongoRepository.save(user.get());
+ User user = new User(userDto);
+ return userMongoRepository.save(user);
}
@@ -79,11 +79,20 @@ public List findUserByNameAndLastName(String name, String lastName) {
}
@Override
- public Optional findUserByEmail(String email) {
+ public User findUserByEmail(String email) {
Optional userByEmailFound = userMongoRepository.findUserByEmail(email);
- if (userByEmailFound.isEmpty()) {
- return Optional.empty();
+ if (userByEmailFound.isPresent()) {
+ return userByEmailFound.get();
}
- return userByEmailFound;
+ return null;
+ }
+
+ @Override
+ public User findUserByIdentityCard(String identityCard) {
+ Optional userByEmailFound = userMongoRepository.findUserByIdentityCard(identityCard);
+ if (userByEmailFound.isPresent()) {
+ return userByEmailFound.get();
+ }
+ return null;
}
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/mongorepository/UserMongoRepository.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/mongorepository/UserMongoRepository.java
index 1c6d017..3467690 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/mongorepository/UserMongoRepository.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/repository/mongorepository/UserMongoRepository.java
@@ -14,4 +14,7 @@ public interface UserMongoRepository extends MongoRepository {
@Query(value = "{'email' : ?0}")
Optional findUserByEmail(String email);
+
+ @Query(value = "{'identityCard' : ?0}")
+ Optional findUserByIdentityCard(String identityCard);
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserService.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserService.java
index d736229..e0f2fa1 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserService.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserService.java
@@ -5,7 +5,6 @@
import com.davidorellana.bookingsystemrestapi.user.model.dto.UserUpdatedDto;
import java.util.List;
-import java.util.Optional;
public interface UserService {
@@ -16,5 +15,6 @@ public interface UserService {
Boolean deleteUserById(String id);
void deleteAllUsers();
List findUserByNameAndLastName(String name, String lastName);
- Optional findUserByEmail(String email);
+ User findUserByEmail(String email);
+ User findUserByIdentityCard(String identityCard);
}
diff --git a/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserServiceImpl.java b/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserServiceImpl.java
index 485b4bc..9abc952 100644
--- a/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserServiceImpl.java
+++ b/src/main/java/com/davidorellana/bookingsystemrestapi/user/service/UserServiceImpl.java
@@ -8,7 +8,6 @@
import org.springframework.stereotype.Service;
import java.util.List;
-import java.util.Optional;
@Service
public class UserServiceImpl implements UserService {
@@ -56,7 +55,12 @@ public List findUserByNameAndLastName(String name, String lastName) {
}
@Override
- public Optional findUserByEmail(String email) {
+ public User findUserByEmail(String email) {
return userRepositoryDao.findUserByEmail(email);
}
+
+ @Override
+ public User findUserByIdentityCard(String identityCard) {
+ return userRepositoryDao.findUserByIdentityCard(identityCard);
+ }
}