feat/add validation
This commit is contained in:
parent
957338e676
commit
14b5d9bccf
|
|
@ -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<Error> errors;
|
||||||
|
|
||||||
|
public ValidationExeption(List<Error> errors) {
|
||||||
|
this.errors = errors;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,13 +1,15 @@
|
||||||
package org.example.je.jdbc.service;
|
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.dao.UserDao;
|
||||||
import org.example.je.jdbc.dto.CreateUserDto;
|
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.mapper.CreateUserMapper;
|
||||||
|
import org.example.je.jdbc.validator.CreateUserValidator;
|
||||||
|
|
||||||
public class UserService {
|
public class UserService {
|
||||||
private static final UserService INSTANCE = new UserService();
|
private static final UserService INSTANCE = new UserService();
|
||||||
private static final UserDao userDao = UserDao.getInstance();
|
private static final UserDao userDao = UserDao.getInstance();
|
||||||
|
private static final CreateUserValidator createUserValidator = CreateUserValidator.getInstance();
|
||||||
|
|
||||||
private static final CreateUserMapper createUserMapper = CreateUserMapper.getInstance();
|
private static final CreateUserMapper createUserMapper = CreateUserMapper.getInstance();
|
||||||
|
|
||||||
|
|
@ -16,6 +18,12 @@ public class UserService {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long create(CreateUserDto createUserDto ){
|
public Long create(CreateUserDto createUserDto ){
|
||||||
|
var validationResult = createUserValidator.isValid(createUserDto);
|
||||||
|
|
||||||
|
if(!validationResult.isValid()){
|
||||||
|
throw new ValidationExeption(validationResult.getErrors());
|
||||||
|
}
|
||||||
|
|
||||||
var user = createUserMapper.mapFrom(createUserDto);
|
var user = createUserMapper.mapFrom(createUserDto);
|
||||||
userDao.save(user);
|
userDao.save(user);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -6,22 +6,22 @@ import jakarta.servlet.http.HttpServlet;
|
||||||
import jakarta.servlet.http.HttpServletRequest;
|
import jakarta.servlet.http.HttpServletRequest;
|
||||||
import jakarta.servlet.http.HttpServletResponse;
|
import jakarta.servlet.http.HttpServletResponse;
|
||||||
import org.example.je.jdbc.dto.CreateUserDto;
|
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 org.example.je.jdbc.service.UserService;
|
||||||
import utils.JspHelper;
|
import utils.JspHelper;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
@WebServlet("/registration")
|
@WebServlet("/registration")
|
||||||
public class RegistrationServlet extends HttpServlet {
|
public class RegistrationServlet extends HttpServlet {
|
||||||
private static UserService userService = UserService.getInstance();
|
private static final UserService userService = UserService.getInstance();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||||
req.setAttribute("roles", List.of("ADMIN","USER"));
|
req.setAttribute("roles", Role.values());
|
||||||
req.setAttribute("genders", List.of("MALE","FEMALE"));
|
req.setAttribute("genders", Gender.values());
|
||||||
req.getRequestDispatcher(JspHelper.getPath("registration")).forward(req, resp);
|
req.getRequestDispatcher(JspHelper.getPath("registration")).forward(req, resp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -36,6 +36,12 @@ public class RegistrationServlet extends HttpServlet {
|
||||||
.password(req.getParameter("password"))
|
.password(req.getParameter("password"))
|
||||||
.build();
|
.build();
|
||||||
|
|
||||||
userService.create(userDto);
|
try {
|
||||||
|
var id = userService.create(userDto);
|
||||||
|
resp.sendRedirect("/login");
|
||||||
|
} catch (ValidationExeption e) {
|
||||||
|
req.setAttribute("errors", e.getErrors());
|
||||||
|
doGet(req, resp);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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<CreateUserDto>{
|
||||||
|
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() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
package org.example.je.jdbc.validator;
|
||||||
|
|
||||||
|
import lombok.Value;
|
||||||
|
|
||||||
|
@Value(staticConstructor = "of")
|
||||||
|
public class Error {
|
||||||
|
String code;
|
||||||
|
String message ;
|
||||||
|
}
|
||||||
|
|
@ -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<Error> errors = new ArrayList<>();
|
||||||
|
|
||||||
|
public void add(Error error){
|
||||||
|
this.errors.add(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isValid(){
|
||||||
|
return this.errors.isEmpty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
package org.example.je.jdbc.validator;
|
||||||
|
|
||||||
|
public interface Validator <T>{
|
||||||
|
ValidationResult isValid(T object);
|
||||||
|
}
|
||||||
|
|
@ -42,5 +42,14 @@
|
||||||
<button type="submit">Send </button>
|
<button type="submit">Send </button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
<c:if test="${not empty requestScope.errors}">
|
||||||
|
<div style="color:red">
|
||||||
|
<c:forEach var="error" items="${requestScope.errors}">
|
||||||
|
<span>${error}</span>
|
||||||
|
<br>
|
||||||
|
</c:forEach>
|
||||||
|
</div>
|
||||||
|
</c:if>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
Loading…
Reference in New Issue