Search in sources :

Example 6 with User

use of com.rafaelvieira.letmebuy.entities.User 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)

Example 7 with User

use of com.rafaelvieira.letmebuy.entities.User in project letmebuy by rafaelrok.

the class UserService method save.

@Transactional
public UserDTO save(UserInsertDTO dto) {
    User entity = new User();
    copyDtoToEntity(dto, entity);
    entity.setPassword(passwordEncoder.encode(dto.getPassword()));
    entity = repository.save(entity);
    return new UserDTO(entity);
}
Also used : User(com.rafaelvieira.letmebuy.entities.User) UserDTO(com.rafaelvieira.letmebuy.dto.UserDTO) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

User (com.rafaelvieira.letmebuy.entities.User)7 UserDTO (com.rafaelvieira.letmebuy.dto.UserDTO)3 Transactional (org.springframework.transaction.annotation.Transactional)3 FieldMessage (com.rafaelvieira.letmebuy.controllers.exceptions.FieldMessage)2 ResourceNotFoundException (com.rafaelvieira.letmebuy.services.handlers.ResourceNotFoundException)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 EntityNotFoundException (javax.persistence.EntityNotFoundException)1 UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)1 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)1