Search in sources :

Example 1 with FieldMessage

use of com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage in project letmebuy by rafaelrok.

the class UserUpdateValidator method isValid.

@Override
public boolean isValid(UserUpdateDTO dto, ConstraintValidatorContext context) {
    @SuppressWarnings("unchecked") var uriVars = (Map<String, String>) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
    long userId = Long.parseLong(uriVars.get("id"));
    List<FieldMessage> list = new ArrayList<>();
    User user = repository.findByEmail(dto.getEmail());
    if (user != null && userId != user.getId()) {
        list.add(new FieldMessage("email", "Email já existe"));
    }
    for (FieldMessage e : list) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(e.getMessage()).addPropertyNode(e.getFieldName()).addConstraintViolation();
    }
    return list.isEmpty();
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) ArrayList(java.util.ArrayList) Map(java.util.Map) FieldMessage(com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)

Example 2 with FieldMessage

use of com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage in project letmebuy by rafaelrok.

the class UserInsertValidator method isValid.

@Override
public boolean isValid(UserInsertDTO dto, ConstraintValidatorContext context) {
    List<FieldMessage> list = new ArrayList<>();
    // Area onde efetua os testes de validação
    User user = repository.findByEmail(dto.getEmail());
    if (user != null) {
        list.add(new FieldMessage("email", "Email já existe"));
    }
    // Implementa no fieldmensage os erros de validação
    for (FieldMessage e : list) {
        context.disableDefaultConstraintViolation();
        context.buildConstraintViolationWithTemplate(e.getMessage()).addPropertyNode(e.getFieldName()).addConstraintViolation();
    }
    return list.isEmpty();
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) ArrayList(java.util.ArrayList) FieldMessage(com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)

Aggregations

FieldMessage (com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)2 User (com.rafaelvieira.letmebuy.entities.User)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)1