diff --git a/src/main/java/org/example/je/jdbc/exeption/ValidationExeption.java b/src/main/java/org/example/je/jdbc/exeption/ValidationExeption.java new file mode 100644 index 0000000..9eb69c3 --- /dev/null +++ b/src/main/java/org/example/je/jdbc/exeption/ValidationExeption.java @@ -0,0 +1,15 @@ +package org.example.je.jdbc.exeption; + +import lombok.Getter; +import org.example.je.jdbc.validator.Error; + +import java.util.List; + +public class ValidationExeption extends RuntimeException { + @Getter + private final List errors; + + public ValidationExeption(List errors) { + this.errors = errors; + } +} diff --git a/src/main/java/org/example/je/jdbc/service/UserService.java b/src/main/java/org/example/je/jdbc/service/UserService.java index b04c8fc..b224bac 100644 --- a/src/main/java/org/example/je/jdbc/service/UserService.java +++ b/src/main/java/org/example/je/jdbc/service/UserService.java @@ -1,13 +1,15 @@ package org.example.je.jdbc.service; -import org.example.je.jdbc.dao.TicketDao; import org.example.je.jdbc.dao.UserDao; import org.example.je.jdbc.dto.CreateUserDto; +import org.example.je.jdbc.exeption.ValidationExeption; import org.example.je.jdbc.mapper.CreateUserMapper; +import org.example.je.jdbc.validator.CreateUserValidator; public class UserService { private static final UserService INSTANCE = new UserService(); private static final UserDao userDao = UserDao.getInstance(); + private static final CreateUserValidator createUserValidator = CreateUserValidator.getInstance(); private static final CreateUserMapper createUserMapper = CreateUserMapper.getInstance(); @@ -16,6 +18,12 @@ public class UserService { } public Long create(CreateUserDto createUserDto ){ + var validationResult = createUserValidator.isValid(createUserDto); + + if(!validationResult.isValid()){ + throw new ValidationExeption(validationResult.getErrors()); + } + var user = createUserMapper.mapFrom(createUserDto); userDao.save(user); diff --git a/src/main/java/org/example/je/jdbc/servlet/RegistrationServlet.java b/src/main/java/org/example/je/jdbc/servlet/RegistrationServlet.java index b39769d..f2005e1 100644 --- a/src/main/java/org/example/je/jdbc/servlet/RegistrationServlet.java +++ b/src/main/java/org/example/je/jdbc/servlet/RegistrationServlet.java @@ -6,22 +6,22 @@ import jakarta.servlet.http.HttpServlet; import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletResponse; import org.example.je.jdbc.dto.CreateUserDto; -import org.example.je.jdbc.mapper.CreateUserMapper; +import org.example.je.jdbc.entity.Gender; +import org.example.je.jdbc.entity.Role; +import org.example.je.jdbc.exeption.ValidationExeption; import org.example.je.jdbc.service.UserService; import utils.JspHelper; import java.io.IOException; -import java.nio.charset.StandardCharsets; -import java.util.List; @WebServlet("/registration") public class RegistrationServlet extends HttpServlet { - private static UserService userService = UserService.getInstance(); + private static final UserService userService = UserService.getInstance(); @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { - req.setAttribute("roles", List.of("ADMIN","USER")); - req.setAttribute("genders", List.of("MALE","FEMALE")); + req.setAttribute("roles", Role.values()); + req.setAttribute("genders", Gender.values()); req.getRequestDispatcher(JspHelper.getPath("registration")).forward(req, resp); } @@ -36,6 +36,12 @@ public class RegistrationServlet extends HttpServlet { .password(req.getParameter("password")) .build(); - userService.create(userDto); + try { + var id = userService.create(userDto); + resp.sendRedirect("/login"); + } catch (ValidationExeption e) { + req.setAttribute("errors", e.getErrors()); + doGet(req, resp); + } } } diff --git a/src/main/java/org/example/je/jdbc/validator/CreateUserValidator.java b/src/main/java/org/example/je/jdbc/validator/CreateUserValidator.java new file mode 100644 index 0000000..9826786 --- /dev/null +++ b/src/main/java/org/example/je/jdbc/validator/CreateUserValidator.java @@ -0,0 +1,37 @@ +package org.example.je.jdbc.validator; + +import org.example.je.jdbc.dto.CreateUserDto; +import utils.LocalDateTimeFormatter; + +public class CreateUserValidator implements Validator{ + private static final CreateUserValidator INSTANCE = new CreateUserValidator(); + public static CreateUserValidator getInstance(){ + return INSTANCE; + } + + @Override + public ValidationResult isValid(CreateUserDto userDto){ + var validationResult = new ValidationResult(); + + if(!LocalDateTimeFormatter.isValid(userDto.getBirthdate())){ + validationResult.add(Error.of("invalid.birthday","Birthday is invalid")); + } + + if(userDto.getGender().isEmpty()){ + validationResult.add(Error.of("invalid.gender","Gender is invalid")); + } + + if(userDto.getRole().isEmpty()){ + validationResult.add(Error.of("invalid.role","Role is invalid")); + } + + if(userDto.getName().isEmpty()){ + validationResult.add(Error.of("invalid.name","Name is invalid")); + } + + return validationResult; + } + + private CreateUserValidator() { + } +} diff --git a/src/main/java/org/example/je/jdbc/validator/Error.java b/src/main/java/org/example/je/jdbc/validator/Error.java new file mode 100644 index 0000000..8b68827 --- /dev/null +++ b/src/main/java/org/example/je/jdbc/validator/Error.java @@ -0,0 +1,9 @@ +package org.example.je.jdbc.validator; + +import lombok.Value; + +@Value(staticConstructor = "of") +public class Error { + String code; + String message ; +} diff --git a/src/main/java/org/example/je/jdbc/validator/ValidationResult.java b/src/main/java/org/example/je/jdbc/validator/ValidationResult.java new file mode 100644 index 0000000..d68bf17 --- /dev/null +++ b/src/main/java/org/example/je/jdbc/validator/ValidationResult.java @@ -0,0 +1,19 @@ +package org.example.je.jdbc.validator; + +import lombok.Getter; + +import java.util.ArrayList; +import java.util.List; + +public class ValidationResult { + @Getter + private final List errors = new ArrayList<>(); + + public void add(Error error){ + this.errors.add(error); + } + + public boolean isValid(){ + return this.errors.isEmpty(); + } +} diff --git a/src/main/java/org/example/je/jdbc/validator/Validator.java b/src/main/java/org/example/je/jdbc/validator/Validator.java new file mode 100644 index 0000000..a5b6f52 --- /dev/null +++ b/src/main/java/org/example/je/jdbc/validator/Validator.java @@ -0,0 +1,5 @@ +package org.example.je.jdbc.validator; + +public interface Validator { + ValidationResult isValid(T object); +} diff --git a/src/main/webapp/WEB-INF/jsp/registration.jsp b/src/main/webapp/WEB-INF/jsp/registration.jsp index fa24e63..8a14cac 100644 --- a/src/main/webapp/WEB-INF/jsp/registration.jsp +++ b/src/main/webapp/WEB-INF/jsp/registration.jsp @@ -42,5 +42,14 @@ + +
+ + ${error} +
+
+
+
+ \ No newline at end of file