/** * Retrieve user by id * * @param userId the user identifier * @return the user corresponding to the specified user identifier */ @RequestMapping(method = RequestMethod.GET, value = "/{userId}") public UserResource userGet( @PathVariable("userId") Long userId, HttpServletRequest httpServletRequest) { User requestBo = null; try { requestBo = userRepo.findOne(userId); } catch (DataAccessException e) { String msg = "Can't retrieve asked users from DB"; logger.error(msg, e); throw new DefaultSportManagerException( new ErrorResource("db error", msg, HttpStatus.INTERNAL_SERVER_ERROR)); } if (requestBo == null) { String msg = "User with id " + userId + " not found"; throw new DefaultSportManagerException( new ErrorResource("not found", msg, HttpStatus.NOT_FOUND)); } UserResource resource = userResourceAssembler.toResource(requestBo); if (httpServletRequest.isUserInRole("AK_ADMIN")) { resource.add( linkTo(methodOn(UserController.class).userGet(userId, null)) .withRel(ActionsConstants.UPDATE_VIA_PUT)); resource.add( linkTo(methodOn(UserController.class).userGet(userId, null)) .withRel(ActionsConstants.DELETE_VIA_DELETE)); } return resource; }
/** * Put a user * * @param p the {@link UserResourceBean} * @param userId the user identifier * @return {@link UserResourceBean} */ @RequestMapping( method = RequestMethod.PUT, value = "/{userId}", consumes = "application/json; charset=utf-8") @PreAuthorize(value = "hasRole('AK_ADMIN')") public UserResource userUpdate( @Valid @RequestBody UserResource p, @PathVariable("userId") Long userId) { try { User userToUpdate = new User(); dozerBeanMapper.map(p, userToUpdate); User userBo = userRepo.saveAndFlush(userToUpdate); return userResourceAssembler.toResource(userBo); } catch (Exception e) { String msg = "Can't update user with id " + userId + " from DB"; logger.error(msg, e); throw new DefaultSportManagerException( new ErrorResource("db error", msg, HttpStatus.INTERNAL_SERVER_ERROR)); } }