Ejemplo n.º 1
0
 /** PUT /users -> Updates an existing User. */
 @RequestMapping(
     value = "/users",
     method = RequestMethod.PUT,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 @Transactional
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<ManagedUserDTO> updateUser(@RequestBody ManagedUserDTO managedUserDTO)
     throws URISyntaxException {
   log.debug("REST request to update User : {}", managedUserDTO);
   return Optional.of(userRepository.findOne(managedUserDTO.getId()))
       .map(
           user -> {
             user.setLogin(managedUserDTO.getLogin());
             user.setFirstName(managedUserDTO.getFirstName());
             user.setLastName(managedUserDTO.getLastName());
             user.setEmail(managedUserDTO.getEmail());
             user.setActivated(managedUserDTO.isActivated());
             user.setLangKey(managedUserDTO.getLangKey());
             Set<Authority> authorities = user.getAuthorities();
             authorities.clear();
             managedUserDTO
                 .getAuthorities()
                 .stream()
                 .forEach(authority -> authorities.add(authorityRepository.findOne(authority)));
             return ResponseEntity.ok()
                 .headers(HeaderUtil.createEntityUpdateAlert("user", managedUserDTO.getLogin()))
                 .body(new ManagedUserDTO(userRepository.findOne(managedUserDTO.getId())));
           })
       .orElseGet(() -> new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR));
 }
Ejemplo n.º 2
0
 /** POST /users -> Create a new user. */
 @RequestMapping(
     value = "/users",
     method = RequestMethod.POST,
     produces = MediaType.APPLICATION_JSON_VALUE)
 @Timed
 @Secured(AuthoritiesConstants.ADMIN)
 public ResponseEntity<User> createUser(@RequestBody User user) throws URISyntaxException {
   log.debug("REST request to save User : {}", user);
   if (user.getId() != null) {
     return ResponseEntity.badRequest()
         .header("Failure", "A new user cannot already have an ID")
         .body(null);
   }
   User result = userRepository.save(user);
   return ResponseEntity.created(new URI("/api/users/" + result.getId()))
       .headers(HeaderUtil.createEntityCreationAlert("user", result.getId().toString()))
       .body(result);
 }