Search in sources :

Example 16 with ManagedUserVM

use of com.baeldung.web.rest.vm.ManagedUserVM in project tutorials by eugenp.

the class AccountResource method registerAccount.

/**
 * POST  /register : register the user.
 *
 * @param managedUserVM the managed user View Model
 * @return the ResponseEntity with status 201 (Created) if the user is registered or 400 (Bad Request) if the login or e-mail is already in use
 */
@PostMapping(path = "/register", produces = { MediaType.APPLICATION_JSON_VALUE, MediaType.TEXT_PLAIN_VALUE })
@Timed
public ResponseEntity registerAccount(@Valid @RequestBody ManagedUserVM managedUserVM) {
    HttpHeaders textPlainHeaders = new HttpHeaders();
    textPlainHeaders.setContentType(MediaType.TEXT_PLAIN);
    return userRepository.findOneByLogin(managedUserVM.getLogin().toLowerCase()).map(user -> new ResponseEntity<>("login already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)).orElseGet(() -> userRepository.findOneByEmail(managedUserVM.getEmail()).map(user -> new ResponseEntity<>("e-mail address already in use", textPlainHeaders, HttpStatus.BAD_REQUEST)).orElseGet(() -> {
        User user = userService.createUser(managedUserVM.getLogin(), managedUserVM.getPassword(), managedUserVM.getFirstName(), managedUserVM.getLastName(), managedUserVM.getEmail().toLowerCase(), managedUserVM.getImageUrl(), managedUserVM.getLangKey());
        mailService.sendActivationEmail(user);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }));
}
Also used : UserDTO(com.baeldung.service.dto.UserDTO) java.util(java.util) Logger(org.slf4j.Logger) MailService(com.baeldung.service.MailService) HttpHeaders(org.springframework.http.HttpHeaders) MediaType(org.springframework.http.MediaType) User(com.baeldung.domain.User) LoggerFactory(org.slf4j.LoggerFactory) HeaderUtil(com.baeldung.web.rest.util.HeaderUtil) ManagedUserVM(com.baeldung.web.rest.vm.ManagedUserVM) StringUtils(org.apache.commons.lang3.StringUtils) Valid(javax.validation.Valid) Timed(com.codahale.metrics.annotation.Timed) HttpStatus(org.springframework.http.HttpStatus) HttpServletRequest(javax.servlet.http.HttpServletRequest) UserService(com.baeldung.service.UserService) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) UserRepository(com.baeldung.repository.UserRepository) KeyAndPasswordVM(com.baeldung.web.rest.vm.KeyAndPasswordVM) ResponseEntity(org.springframework.http.ResponseEntity) SecurityUtils(com.baeldung.security.SecurityUtils) HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) User(com.baeldung.domain.User) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

User (com.baeldung.domain.User)16 ManagedUserVM (com.baeldung.web.rest.vm.ManagedUserVM)16 Test (org.junit.Test)15 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)15 Transactional (org.springframework.transaction.annotation.Transactional)15 HashSet (java.util.HashSet)8 UserRepository (com.baeldung.repository.UserRepository)1 SecurityUtils (com.baeldung.security.SecurityUtils)1 MailService (com.baeldung.service.MailService)1 UserService (com.baeldung.service.UserService)1 UserDTO (com.baeldung.service.dto.UserDTO)1 HeaderUtil (com.baeldung.web.rest.util.HeaderUtil)1 KeyAndPasswordVM (com.baeldung.web.rest.vm.KeyAndPasswordVM)1 Timed (com.codahale.metrics.annotation.Timed)1 java.util (java.util)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 Valid (javax.validation.Valid)1 StringUtils (org.apache.commons.lang3.StringUtils)1 Logger (org.slf4j.Logger)1 LoggerFactory (org.slf4j.LoggerFactory)1