Skip to content

Commit

Permalink
indent
Browse files Browse the repository at this point in the history
  • Loading branch information
Marek Mitis committed Dec 20, 2015
1 parent d4b9c26 commit f144c8a
Show file tree
Hide file tree
Showing 23 changed files with 587 additions and 589 deletions.
31 changes: 18 additions & 13 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
{
"rules": {
"quotes": [
2,
"single"
],
"linebreak-style": [
2,
"unix"
],
"semi": [
2,
"always"
]
},
"env": {
"browser": true,
"amd": true,
"es6": true,
"mocha": true
"node": true
},
"parser": "babel-eslint",
"rules": {
"strict": 0,
"comma-dangle": [
2,
"always-multiline"
],
"no-underscore-dangle": 0
"ecmaFeatures": {
"modules": true
},
"globals": {
"expect": true
}
"extends": "eslint:recommended"
}
108 changes: 54 additions & 54 deletions server/api/thing/thing.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,90 +13,90 @@ import _ from 'lodash';
var Thing = require('./thing.model');

function handleError(res, statusCode) {
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
statusCode = statusCode || 500;
return function(err) {
res.status(statusCode).send(err);
};
}

function responseWithResult(res, statusCode) {
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
statusCode = statusCode || 200;
return function(entity) {
if (entity) {
res.status(statusCode).json(entity);
}
};
}

function handleEntityNotFound(res) {
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
return function(entity) {
if (!entity) {
res.status(404).end();
return null;
}
return entity;
};
}

function saveUpdates(updates) {
return function(entity) {
var updated = _.merge(entity, updates);
return updated.saveAsync()
.spread(updated => {
return updated;
});
};
return function(entity) {
var updated = _.merge(entity, updates);
return updated.saveAsync()
.spread(updated => {
return updated;
});
};
}

function removeEntity(res) {
return function(entity) {
if (entity) {
return entity.removeAsync()
.then(() => {
res.status(204).end();
});
}
};
return function(entity) {
if (entity) {
return entity.removeAsync()
.then(() => {
res.status(204).end();
});
}
};
}

// Gets a list of Things
export function index(req, res) {
Thing.findAsync()
.then(responseWithResult(res))
.catch(handleError(res));
Thing.findAsync()
.then(responseWithResult(res))
.catch(handleError(res));
}

// Gets a single Thing from the DB
export function show(req, res) {
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.catch(handleError(res));
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(responseWithResult(res))
.catch(handleError(res));
}

// Creates a new Thing in the DB
export function create(req, res) {
Thing.createAsync(req.body)
.then(responseWithResult(res, 201))
.catch(handleError(res));
Thing.createAsync(req.body)
.then(responseWithResult(res, 201))
.catch(handleError(res));
}

// Updates an existing Thing in the DB
export function update(req, res) {
if (req.body._id) {
delete req.body._id;
}
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.catch(handleError(res));
if (req.body._id) {
delete req.body._id;
}
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(saveUpdates(req.body))
.then(responseWithResult(res))
.catch(handleError(res));
}

// Deletes a Thing from the DB
export function destroy(req, res) {
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
Thing.findByIdAsync(req.params.id)
.then(handleEntityNotFound(res))
.then(removeEntity(res))
.catch(handleError(res));
}
16 changes: 8 additions & 8 deletions server/api/thing/thing.events.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ ThingEvents.setMaxListeners(0);

// Model events
var events = {
'save': 'save',
'remove': 'remove'
'save': 'save',
'remove': 'remove'
};

// Register the event emitter to the model events
for (var e in events) {
var event = events[e];
Thing.schema.post(e, emitEvent(event));
var event = events[e];
Thing.schema.post(e, emitEvent(event));
}

function emitEvent(event) {
return function(doc) {
ThingEvents.emit(event + ':' + doc._id, doc);
ThingEvents.emit(event, doc);
}
return function(doc) {
ThingEvents.emit(event + ':' + doc._id, doc);
ThingEvents.emit(event, doc);
}
}

export default ThingEvents;
6 changes: 3 additions & 3 deletions server/api/thing/thing.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
var mongoose = require('bluebird').promisifyAll(require('mongoose'));

var ThingSchema = new mongoose.Schema({
name: String,
info: String,
active: Boolean
name: String,
info: String,
active: Boolean
});

export default mongoose.model('Thing', ThingSchema);
28 changes: 14 additions & 14 deletions server/api/thing/thing.socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ var ThingEvents = require('./thing.events');
var events = ['save', 'remove'];

export function register(socket) {
// Bind model events to socket events
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
var event = events[i];
var listener = createListener('thing:' + event, socket);

ThingEvents.on(event, listener);
socket.on('disconnect', removeListener(event, listener));
}
// Bind model events to socket events
for (var i = 0, eventsLength = events.length; i < eventsLength; i++) {
var event = events[i];
var listener = createListener('thing:' + event, socket);

ThingEvents.on(event, listener);
socket.on('disconnect', removeListener(event, listener));
}
}


function createListener(event, socket) {
return function(doc) {
socket.emit(event, doc);
};
return function(doc) {
socket.emit(event, doc);
};
}

function removeListener(event, listener) {
return function() {
ThingEvents.removeListener(event, listener);
};
return function() {
ThingEvents.removeListener(event, listener);
};
}
Loading

0 comments on commit f144c8a

Please sign in to comment.