<commit_before>var _ = require('underscore');
var Room = require('../../models').room;
var UserModel = require('../../models').user;

module.exports = function(server){
  server.sock.when('CREATE /api/rooms', createRoom);
  server.sock.when('READ /api/rooms', readRooms);
  server.sock.when('READ /api/room/:id', readRoom);
  server.sock.when('UPDATE /api/room/:id', server.sock.can('updateRoom'), updateRoom);
  server.sock.when('DELETE /api/room/:id', server.sock.can('deleteRoom'), deleteRoom);
  server.sock.when('JOIN /api/room/:id', joinRoom);
  server.sock.when('LEAVE /api/room/:id', leaveRoom);
}

function joinRoom(socket, data, next){
  var user = UserModel.user(socket.user);
  user.joinRoom(socket.params['id'], function(err, result){
    if(err){
      return next(err);
    }
    socket.join(socket.params['id']);
    socket.json({result: result});
  });
}

function leaveRoom(socket, data, next){
  //TODO: implement
  // remove from DB
  //remove from socket
}

function createRoom(socket, data){
  var newRoom = _(data).pick("description", "name");
  newRoom.owner = socket.user._id;

  Room.create(newRoom, function(err, room, next){
    if(err){
      return next(err);
    }
    socket.json(room);
  });
}

function readRooms(socket, data, next){
  Room.getAll(socket.user._id, function(err, rooms, next) {
    if(err){
      return next(err);
    }
    socket.json({statusCode: 200, rooms: rooms});
  });
}

function readRoom(socket){
  Room.getById(socket.params['id'], function(err, room, next){
    if(err){
      return next(err);
    }
    room ? socket.json(room) : socket.json({err: 'Room not found', statusCode: 404});
  });
}

function updateRoom(socket, data, next){
  var updatedRoom = _(data).pick("description", "name");
  updatedRoom.owner = socket.user._id;

  Room.updateById(socket.params['id'], updatedRoom, function(err, room){
    if(err){
      return next(err);
    }
    socket.json(room);
  });
}

function deleteRoom(socket, data, next){
  Room.deleteById(socket.params['id'], function(err){
    if(err){
      return next(err);
    }
    socket.json({statusCode: 200});
  });
}
<commit_msg>Fixed missed error handling in rooms socket flow processor<commit_after>var _ = require('underscore');
var Room = require('../../models').room;
var UserModel = require('../../models').user;

module.exports = function(server){
  server.sock.when('CREATE /api/rooms', createRoom);
  server.sock.when('READ /api/rooms', readRooms);
  server.sock.when('READ /api/room/:id', readRoom);
  server.sock.when('UPDATE /api/room/:id', server.sock.can('updateRoom'), updateRoom);
  server.sock.when('DELETE /api/room/:id', server.sock.can('deleteRoom'), deleteRoom);
  server.sock.when('JOIN /api/room/:id', joinRoom);
  server.sock.when('LEAVE /api/room/:id', leaveRoom);
}

function joinRoom(socket, data, next){
  var user = UserModel.user(socket.user);
  user.joinRoom(socket.params['id'], function(err, result){
    if(err){
      return next(err);
    }
    socket.join(socket.params['id']);
    socket.json({result: result});
  });
}

function leaveRoom(socket, data, next){
  //TODO: implement
  // remove from DB
  //remove from socket
}

function createRoom(socket, data, next){
  var newRoom = _(data).pick("description", "name");
  newRoom.owner = socket.user._id;

  Room.create(newRoom, function(err, room, next){
    if(err){
      return next(err);
    }
    socket.json(room);
  });
}

function readRooms(socket, data, next){
  Room.getAll(socket.user._id, function(err, rooms, next) {
    if(err){
      return next(err);
    }
    socket.json({statusCode: 200, rooms: rooms});
  });
}

function readRoom(socket, data, next){
  Room.getById(socket.params['id'], function(err, room, next){
    if(err){
      return next(err);
    }
    room ? socket.json(room) : socket.json({err: 'Room not found', statusCode: 404});
  });
}

function updateRoom(socket, data, next){
  var updatedRoom = _(data).pick("description", "name");
  updatedRoom.owner = socket.user._id;

  Room.updateById(socket.params['id'], updatedRoom, function(err, room){
    if(err){
      return next(err);
    }
    socket.json(room);
  });
}

function deleteRoom(socket, data, next){
  Room.deleteById(socket.params['id'], function(err){
    if(err){
      return next(err);
    }
    socket.json({statusCode: 200});
  });
}

{'fraction_non_alphanumeric': 0.18690942613505415, 'fraction_numerical': 0.004148421295229315, 'mean_word_length': 3.8873873873873874, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2099374', 'n_tokens_mistral': 1459, 'n_tokens_neox': 1442, 'n_words': 289}