Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

date range, numer/page pagination #35

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 42 additions & 65 deletions template/controller.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,14 @@ var <%= service %>sController = {};
ourProjection = <%= service %>sController.buildProjection(projection);
delete query.select;
}
var limit = query.limit * 1;
if(limit){
var limit = query.limit * 1 || 50;
if (limit) {
delete query.limit;
}
var skip = query.skip * 1 || 0;
if (skip) {
delete query.skip;
}

var from = query.from;
var to = query.to;
Expand All @@ -89,34 +93,10 @@ var <%= service %>sController = {};
if(to){
delete query.to;
}else{
to = new Date().toISOString();
}
query.createdAt.$lt = to;
}else{
query.createdAt = {};
query.createdAt.$gt = new Date('1989-03-15T00:00:00').toISOString();
if(to){
delete query.to;
}else{
to = new Date().toISOString();
to = new Date().toISOString();;
}
query.createdAt.$lt = to;
}
var lastId = query.lastId;
if(lastId){
if(query.desc){
query._id = {};
query._id.$lt = lastId;
delete query.desc;
}else{
query._id = {};
query._id.$gt = lastId;
}
delete query.lastId;
}
if(query.desc){
delete query.desc;
}
var sort = query.sort; // -fieldName: means descending while fieldName without the minus mean ascending bith by fieldName. eg, '-fieldName1 fieldName2'
if(sort){
delete query.sort;
Expand All @@ -125,18 +105,19 @@ var <%= service %>sController = {};
if(populate){
delete query.populate;
}
var totalResult = <%= service %>s.estimatedDocumentCount(query);
var totalResult = <%= service %>s.countDocuments(query);
var total = <%= service %>s.estimatedDocumentCount({});
var question = <%= service %>s.find(query);
if(skip !== 0){
question = question.skip(skip)
}

if(limit !== 0){
totalResult = totalResult.limit(limit);
question = question.limit(limit);
}


if(limit){
totalResult = totalResult.limit(limit);
question = question.limit(limit);
}else{
limit = 50;
totalResult = totalResult.limit(limit);
question = question.limit(limit);
}
if(sort){
question = question.sort(sort);
}
Expand All @@ -150,18 +131,17 @@ var <%= service %>sController = {};
return [question.select(resp),total,totalResult];
})
.spread(function(resp,total,totalResult){
var ourLastId;
if(resp.length === 0){
ourLastId = null;
}else{
ourLastId = resp[resp.length - 1]._id;
}
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.totalResult = totalResult;
extraData.lastId = ourLastId;
extraData.isLastPage = (totalResult < limit) ? true : false;
var pages = Math.ceil(total / limit)
var remainingPages = Math.ceil((total - (totalResult + skip)) / limit)
totalResult = totalResult + skip
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.totalResult = totalResult > total ? total : totalResult;
extraData.skip = skip;
extraData.currentPage = pages - remainingPages;
extraData.pages = pages;
extraData.isLastPage = (totalResult >= total) ? true : false;
res.ok(resp, false, extraData);
})
.catch(function(err){
Expand All @@ -170,18 +150,19 @@ var <%= service %>sController = {};
}else{
q.all([question,total,totalResult])
.spread(function(resp,total,totalResult){
var ourLastId;
if(resp.length === 0){
ourLastId = null;
}else{
ourLastId = resp[resp.length - 1]._id;
}
var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.lastId = ourLastId;
extraData.totalResult = totalResult;
extraData.isLastPage = (totalResult < limit) ? true : false;
var pages = Math.ceil(total / limit)
var remainingPages = Math.ceil((total - (totalResult + skip)) / limit)
totalResult = totalResult + skip

var extraData = {};
extraData.limit = limit * 1;
extraData.total = total;
extraData.totalResult = totalResult > total ? total : totalResult;
extraData.skip = skip;
extraData.pages = pages;
extraData.currentPage = pages - remainingPages;

extraData.isLastPage = (totalResult >= total) ? true : false;
res.ok(resp, false, extraData);
})
.catch(function(err){
Expand All @@ -207,11 +188,7 @@ var <%= service %>sController = {};

question
.then(function(resp){
if(!resp){
next();
}else{
res.ok(resp);
}
res.ok(resp);
})
.catch(function(err){
next(err);
Expand Down