/**
  * Add user to room's users
  *
  * @param roomId
  * @param userId
  * @return HttpEntity<Room> - room that user joined to
  */
 @RequestMapping(value = "/join/{id}", method = RequestMethod.POST)
 public HttpEntity<Room> joinRoom(@PathVariable("id") int roomId, @RequestBody int userId)
     throws AlreadyExistsException {
   Room room = roomService.findOne(roomId);
   User user = userService.findOne(userId);
   if (user == null || room == null || room.getType() == Room.CLOSE_TYPE) {
     return new ResponseEntity(HttpStatus.BAD_REQUEST);
   }
   room = roomService.joinRoom(roomId, user);
   return new ResponseEntity(room, HttpStatus.OK);
 }
  /**
   * Get room by its id. Only room owner can do this request.
   *
   * @param id
   * @return HttpEntity<Room> - room
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  public HttpEntity<Room> getRoom(@PathVariable("id") int id) {
    Room room = roomService.findOne(id);
    if (room != null) {
      UserResource currentUser = accessService.getCurrentUser();
      if (currentUser == null || currentUser.getId() != room.getOwner().getId()) {
        return new ResponseEntity(HttpStatus.FORBIDDEN);
      }

      return new ResponseEntity(room, HttpStatus.OK);
    }

    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
  /**
   * Delete room. Only room owner can do this request.
   *
   * @param id
   * @return HttpEntity<String> - if all is ok then HttpStatus.NO_CONTENT else
   *     HttpStatus.BAD_REQUEST
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  public HttpEntity<String> deleteRoom(@PathVariable("id") int id) {
    Room room = roomService.findOne(id);
    if (room != null) {
      UserResource currentUser = accessService.getCurrentUser();
      if (currentUser == null || currentUser.getId() != room.getOwner().getId()) {
        return new ResponseEntity(HttpStatus.FORBIDDEN);
      }

      roomService.delete(room);
      return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
  /**
   * Get room's users by its id. Only room owner can do this request.
   *
   * @param id
   * @return HttpEntity<List<User>> - list of users
   */
  @RequestMapping(value = "/{id}/users", method = RequestMethod.GET)
  public HttpEntity<List<User>> getRoomUsers(@PathVariable("id") int id)
      throws ObjectNotFoundException {
    Room room = roomService.findOne(id);
    if (room != null) {
      UserResource currentUser = accessService.getCurrentUser();
      if (currentUser == null || currentUser.getId() != room.getOwner().getId()) {
        return new ResponseEntity(HttpStatus.FORBIDDEN);
      }

      List<User> users = roomService.getRoomUsers(id);
      return new ResponseEntity(users, HttpStatus.OK);
    }
    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
  /**
   * Remove a list of users from room's users. Only room owner can do this request.
   *
   * @param roomId
   * @param usersToRemove
   * @return HttpEntity<Room> - HttpStatus.NO_CONTENT when all is ok
   */
  @RequestMapping(value = "/removeUsers/{id}", method = RequestMethod.POST)
  public HttpEntity<Room> removeUsers(
      @PathVariable("id") int roomId, @RequestBody List<User> usersToRemove)
      throws ObjectNotFoundException {
    Room room = roomService.findOne(roomId);
    if (room != null) {
      UserResource currentUser = accessService.getCurrentUser();
      if (currentUser == null || currentUser.getId() != room.getOwner().getId()) {
        return new ResponseEntity(HttpStatus.FORBIDDEN);
      }
      roomService.removeUsersFromRoom(roomId, usersToRemove);
      return new ResponseEntity(HttpStatus.NO_CONTENT);
    }

    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }
  /**
   * Update room. Only room owner can do this request.
   *
   * @param room
   * @return HttpEntity<Room> - updated room
   */
  @RequestMapping(value = "/{id}", method = RequestMethod.PUT)
  public HttpEntity<Room> updateRoom(@RequestBody Room room) {
    UserResource currentUser = accessService.getCurrentUser();
    if (currentUser == null || currentUser.getId() != room.getOwner().getId()) {
      return new ResponseEntity(HttpStatus.FORBIDDEN);
    }

    roomService.update(room);
    if (room != null) {
      return new ResponseEntity(room, HttpStatus.OK);
    }
    return new ResponseEntity(HttpStatus.BAD_REQUEST);
  }