Skip to content

Commit

Permalink
added support for popping elements from a collection
Browse files Browse the repository at this point in the history
  • Loading branch information
gilmaimon committed Dec 20, 2018
1 parent 243940c commit 6ee3efe
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 3 deletions.
73 changes: 70 additions & 3 deletions database-helpers/collections.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ class CollectionFetchRequest {
this.request.skip = getNumberOrDefault(requestJson, 'skip', 0);
this.request.from_last = requestJson.from_last? Boolean(requestJson.from_last) : false;
this.request.collection_key = requestJson['collection_key'];
console.log(this.request);
return true;
} else return false;
}
Expand Down Expand Up @@ -83,7 +82,6 @@ class CollectionFetchRequest {
}}
}

console.log(username);
this.db.collection('users').aggregate([
{ $match : { "username" : username }},
projectBlock,
Expand All @@ -94,7 +92,76 @@ class CollectionFetchRequest {
}
}

class CollectionPopRequest {
constructor(db, requestJson) {
this.db = db;
this.__isValid = this.__parse(requestJson);
}

// Returns boolean value indicating if the request
// is valid or not (result means isValid)
__parse(requestJson) {
if (requestJson.hasOwnProperty('collection_key') && requestJson.hasOwnProperty('position') &&
["first", "last"].includes(requestJson.position) ) {
this.request = {}
this.request.position = requestJson['position'];
this.request.collection_key = requestJson['collection_key'];
return true;
} else return false;
}

isValid() {
return this.__isValid;
}

__popItem(username, collection_key, popFirstElement, value, willBeEmpty, callback) {
this.db.collection('users').updateOne({username: username}, {$pop: { ["data." + collection_key] : popFirstElement? -1: 1}}, function(err) {
callback(Boolean(err), {value: value, empty: willBeEmpty});
});
}

__getTop(username, collection_key, popFirstElement, callback) {
this.db.collection('users').aggregate([
{ $match: { username: username } },
{ $project: {
topElements: { $slice: [ "$data." + collection_key, popFirstElement? 0 : -2, 2] },
}},
]).toArray(function(err, res) {
if(err) {
callback(true, null);
return;
}
var value = null;
var empty = false;
if(res[0].topElements && res[0].topElements.length > 1) {
if(popFirstElement) value = res[0].topElements[0];
else value = res[0].topElements[1];
} else {
if(res[0].topElements) {
value = res[0].topElements[0];
}
empty = true;
}

callback(value, empty);
})
}

execute(username, callback) {
if(!this.__isValid) throw Error("Requst is invalid (not parsed properly)");

var popFirstElement = this.request.position == "first";
var collection_key = this.request.collection_key
var that = this;

this.__getTop(username, collection_key, popFirstElement, function(value, willBeEmpty) {
that.__popItem(username, collection_key, popFirstElement, value, willBeEmpty, callback);
});
}
}

module.exports = {
CollectionAddRequest: CollectionAddRequest,
CollectionFetchRequest: CollectionFetchRequest
CollectionFetchRequest: CollectionFetchRequest,
CollectionPopRequest: CollectionPopRequest
}
11 changes: 11 additions & 0 deletions database-helpers/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ var getRequestFactory = new GetRequestFactory()

const CollectionAddRequest = require('./collections').CollectionAddRequest;
const CollectionFetchRequest = require('./collections').CollectionFetchRequest;
const CollectionPopRequest = require('./collections').CollectionPopRequest;

class User {
constructor(db, username) {
Expand Down Expand Up @@ -48,6 +49,16 @@ class User {

request.execute(this.username, callback);
}

pop(requestJson, callback) {
var request = new CollectionPopRequest(this.db, requestJson);
if(request.isValid() == false) {
callback(false, null);
return;
}

request.execute(this.username, callback);
}
}


Expand Down
14 changes: 14 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ app.post('/data/collection', function (req, res) {
res.send({"error": err});
});
});

app.get('/data/collection', function(req, res) {
req.userObj.filter(req.body, function(err, result) {
if(err) {
Expand All @@ -107,6 +108,19 @@ app.get('/data/collection', function(req, res) {
});
});

app.get('/data/collection/pop', function(req, res) {
req.userObj.pop(req.body, function(err, result) {
if(err) {
res.status(400).send({"error": true});
} else {
var parsedResult = {};
parsedResult.error = false;
parsedResult['result'] = result;
res.send(parsedResult);
}
});
});

// Route for Registering new users
app.post('/user/register', function (req, res) {
if(config.allow_registering) {
Expand Down

0 comments on commit 6ee3efe

Please sign in to comment.