ParticipationHandler.prototype.createParticipation = function (socketData, callback) {
	var socket = this.socket;

	// TODO validate incoming id (is it <person>_<expense>?)
	Q.nmcall(this.pool, 'getConnection')
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'insert into participations (person,expense,amount,participating) values (?, ?, ?, ?)',
					[socketData.person, socketData.expense, socketData.amount, socketData.participating]
				)
				.then(function (dbData) {
					var json = {
						person: socketData.person,
						expense: socketData.expense,
						amount: socketData.amount,
						participating: socketData.participating
					};
					json.id = ParticipationHandler.generateId(json);

					socket.broadcast.emit('expense/' + socketData.expense + ':createParticipation', json);
					callback(null, json);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
		.fail(function (err) {
			console.log('Error in createParticipation');
			console.dir(err);
			callback(null);
		})
	;
};
ParticipationHandler.prototype.readParticipation = function (socketData, callback) {
	// TODO validate incoming id (is it <person>_<expense>?)
	Q.nmcall(this.pool, 'getConnection')
		.then(function (connection) {
			return Q.nmcall(
					connection, 'query', 'select * from participations where person = ? and expense = ?', 
					[socketData.person, socketData.expense]
				)
				.then(function (dbData) {
					var json = dbData[0][0];
					json.id = ParticipationHandler.generateId(json);
					callback(null, dbData[0][0]);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
		.fail(function (err) {
			console.log('Error in createParticipation');
			console.dir(err);
			callback(null);
		})
	;
};
ParticipationHandler.prototype.deleteParticipation = function (socketData, callback) {
	var socket = this.socket;

	// TODO validate incoming id (is it <person>_<expense>?)
	Q.nmcall(this.pool, 'getConnection')
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'delete from participations where person = ? and expense = ?', 
					[socketData.person, socketData.expense]
				)
				.then(function (data) {
					// TODO check whether something was actually deleted (mysql changed lines)
					socket.broadcast.emit('participation/' + socketData.id + ':delete', true);
					callback(null, true);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
		.fail(function (err) {
			console.log('Error in deleteParticipation');
			console.dir(err);
			callback(null);
		})
	;
};
Beispiel #4
0
NetService.prototype.stop = function(){
    switch(this.config.lib){
        case 'net':
        case 'http':
        case 'https':
        case 'tls':
            return Q.nmcall(this.server, 'close'); // keeps existing connections open!
        case 'udp4':
        case 'udp6':
            this.server.close(); // no callback argument
            return;
    }
};
 .then(function(){
     return Q.nmcall(db, 'collectionNames')
         .then(function(collections){
             return _.map(
                 _.pluck(collections, 'name'),
                 function(collectionName){
                     var c = collectionName.split('.');
                     if (c[0] == db.databaseName)
                         c = c.slice(1);
                     return c.join('.');
                 }
             );
         });
 })
		.then(function (connection) {
			return Q.nmcall(
					connection, 'query', 'select * from participations where person = ? and expense = ?', 
					[socketData.person, socketData.expense]
				)
				.then(function (dbData) {
					var json = dbData[0][0];
					json.id = ParticipationHandler.generateId(json);
					callback(null, dbData[0][0]);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'delete from participations where person = ? and expense = ?', 
					[socketData.person, socketData.expense]
				)
				.then(function (data) {
					// TODO check whether something was actually deleted (mysql changed lines)
					socket.broadcast.emit('participation/' + socketData.id + ':delete', true);
					callback(null, true);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
        it('should talk with the "net" service', function(){
            var sock = new net.Socket();

            var d = Q.defer();
            sock.on('data', function(data){
                if (data.toString('utf8') === 'hi')
                    d.resolve();
                else
                    d.reject(new Error('Wrong data received: ' + data));
            });

            return Q.all([
                Q.nmcall(sock, 'connect', addresses.net.port)
                    .then(function(){
                        sock.end();
                    }),
                d.promise.timeout(1000, 'Socket connect timeout')
            ]);
        });
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'insert into participations (person,expense,amount,participating) values (?, ?, ?, ?)',
					[socketData.person, socketData.expense, socketData.amount, socketData.participating]
				)
				.then(function (dbData) {
					var json = {
						person: socketData.person,
						expense: socketData.expense,
						amount: socketData.amount,
						participating: socketData.participating
					};
					json.id = ParticipationHandler.generateId(json);

					socket.broadcast.emit('expense/' + socketData.expense + ':createParticipation', json);
					callback(null, json);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
ParticipationHandler.prototype.updateParticipation = function (socketData, callback) {
	var socket = this.socket;

	// TODO validate incoming id (is it <person>_<expense>?)
	Q.nmcall(this.pool, 'getConnection')
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'update participations set amount = ?, participating = ? where person = ? and expense = ?',
					[socketData.amount, socketData.participating, socketData.person, socketData.expense]
				)
				.then(function (data) {
					// TODO check whether something was actually updated (mysql changed lines)
					var json = {
						person: socketData.person,
						expense: socketData.expense,
						amount: socketData.amount,
						participating: socketData.participating
					};
					json.id = ParticipationHandler.generateId(json);

					socket.broadcast.emit('participation/' + socketData.id + ':update', json);
					callback(null, json);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
		.fail(function (err) {
			console.log('Error in updateParticipation');
			console.dir(err);
			callback(null);
		})
	;
};
		.then(function (connection) {
			// TODO valid person id, expense id?
			return Q.nmcall(
					connection, 'query', 'update participations set amount = ?, participating = ? where person = ? and expense = ?',
					[socketData.amount, socketData.participating, socketData.person, socketData.expense]
				)
				.then(function (data) {
					// TODO check whether something was actually updated (mysql changed lines)
					var json = {
						person: socketData.person,
						expense: socketData.expense,
						amount: socketData.amount,
						participating: socketData.participating
					};
					json.id = ParticipationHandler.generateId(json);

					socket.broadcast.emit('participation/' + socketData.id + ':update', json);
					callback(null, json);
				})
				.fin(function () {
					connection.release();
				})
			;
		})
 return function(){
     return Q.nmcall(db.collection(  collection  ), 'drop');
 };
Beispiel #13
0
 .then(function() {
   return Q.nmcall(request, 'get', {
     url: req.param('url'),
     encoding: 'binary'
   });
 })