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

HAL #34

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open

HAL #34

Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions service/config.js.example
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
'use strict';

var development = {
'db': {
'url': 'http://localhost:9200',
Expand Down
3 changes: 2 additions & 1 deletion service/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
"express": "~3.4.6",
"q": "~0.9.7",
"lodash": "~2.4.1",
"elasticsearch": "~1.0.3"
"elasticsearch": "~1.0.3",
"express-hal": "0.0.1"
},
"devDependencies": {
"should": "~2.1.1",
Expand Down
5 changes: 5 additions & 0 deletions service/src/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
'use strict';

var routes = require('./routes'),
express = require('express'),
hal = require('express-hal'),
api = express(),
config = require('../config').Config;

api.use(express.methodOverride());
api.use(express.json());

api.use(hal.middleware);

routes.setup(api);

api.use(express.logger('dev'));
Expand Down
71 changes: 71 additions & 0 deletions service/src/lib/paginator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict';
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we agree on this I'll start adding some unit-tests for it. we can probably have a sitdown and discuss it tomorrow.


var getRetroId = function (type, item) {
if (type==='ticket') {
return item.retroId;
} else {
return item.id;
}
};

exports.getParameters = function(req) {
var start = 0,
limit = 10,
page = 1;

if (req.query.page) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should probably add a check that req.query.page is within 1 and N where N is top most page for the result, but I think that belongs in another review.

The way it behaves now, if you have say 10 pages, and you send in ?page=30 it will create a previous link pointing to page=29 instead of redirecting you to page=10 and creating previous:9 which is probably a cleaner solution, but I consider this a edgecase for now.

page = parseInt(req.query.page, 10);
}

if (req.query.limit) {
limit = parseInt(req.query.limit, 10);
}

// page is a human readable iterator
// start is for the machines
start = (page -1) * limit;

return {
start: start,
page: page,
limit: limit
};
};

exports.getItemLink = function (type, item) {
var link = '/retrospectives',
retroId = 0;

if (!item.id) {
return false;
}

retroId = getRetroId(type, item);
link += '/' + retroId;

if (type === 'ticket') {
link += '/tickets/' + item.id;
}
return {
link : link
};
};

exports.getBulkLinks = function (baseUrl, parameters, results) {
var links = {
self: baseUrl + '?page=' + parameters.page + '&limit=' + parameters.limit,
find: {
href: baseUrl + '{?id}',
templated: true
}
};

if (parameters.page - 1) {
links.previous = baseUrl + '?page=' + (parameters.page - 1) + '&limit=' + parameters.limit;
}

if (results.length > parameters.limit) {
links.next = baseUrl + '?page=' + ( parameters.page + 1) + '&limit=' + parameters.limit;
}
return links;
};
56 changes: 40 additions & 16 deletions service/src/models/retrospectives.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use strict';

var config = require('../../config').Config,
db = require('../wrapper');
db = require('../wrapper'),
paginator = require('../lib/paginator');

exports.getRetrospective = function (req, res) {
db.query('_id:' + req.params.retroId).of('retrospective').from(config.db.index)
Expand All @@ -15,25 +16,48 @@ exports.getRetrospective = function (req, res) {
};

exports.getRetrospectives = function (req, res) {
var start = 0,
limit = 10,
page = 1;
var parameters = paginator.getParameters(req);

if (req.query.page) {
page = req.query.page;
}
db.getAll('retrospective').sortBy('createdAt:desc').start(parameters.start).size(parameters.limit + 1).from(config.db.index)
.then(function (result) {
var links = paginator.getBulkLinks('/retrospectives', parameters, result),
retrospectives = [];

if (req.query.limit) {
limit = req.query.limit;
}
if (links.next) {
result.pop();
}

// page is a human readable iterator
// start is for the machines
start = (page - 1) * limit;
result.forEach(function(retrospective, index) {
var links = {
self: paginator.getItemLink('retrospective', retrospective)
};

db.getAll('retrospective').sortBy('createdAt:desc').start(start).size(limit).from(config.db.index)
.then(function (result) {
res.json({'results': result, 'total': result.total});
if (result[index-1]) {
links.previous = paginator.getItemLink('retrospective', result[index - 1]);
}

if (result[index + 1]) {
links.next = paginator.getItemLink('retrospective', result[index + 1]);
}

retrospectives.push({
data: retrospective,
links: links
});
});

res.set('Content-Type', 'application/hal+json');
res.hal({
data: {
total: result.total,
page: parameters.page,
limit: parameters.limit
},
links: links,
embeds: {
'retrospectives': retrospectives
}
});
})
.fail(function (err) {
console.log(err);
Expand Down
52 changes: 39 additions & 13 deletions service/src/models/tickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
var _ = require('lodash'),
config = require('../../config').Config,
db = require('../wrapper'),
paginator = require('../lib/paginator'),

explodeMessages = function (results) {
var words = [];
Expand All @@ -25,23 +26,48 @@ exports.getTicketWords = function (req, res) {
};

exports.getTickets = function (req, res) {
var start = 0,
limit = 100,
page = 1;
var parameters = paginator.getParameters(req);

if (req.query.page) {
page = req.query.page;
}
db.query('retroId:' + req.params.retroId).start(parameters.start).sortBy('createdAt:desc').size(parameters.limit + 1).of('ticket').from(config.db.index)
.then(function (result) {
var links = paginator.getBulkLinks('/retrospectives' + req.params.retroId + '/tickets', parameters, result),
tickets = [];

if (req.query.limit) {
limit = req.query.limit;
}
if(links.next) {
result.pop();
}

start = (page - 1) * limit;
result.forEach(function(ticket, index) {
var links = {
self: paginator.getItemLink('ticket', ticket)
};

db.query('retroId:' + req.params.retroId).start(start).sortBy('createdAt:desc').size(limit).of('ticket').from(config.db.index)
.then(function (result) {
res.json({'results': result, 'total': result.total});
if (result[index-1]) {
links.previous = paginator.getItemLink('ticket', result[index - 1]);
}

if (result[index+1]) {
links.next = paginator.getItemLink('ticket', result[index + 1]);
}

tickets.push({
data: ticket,
links: links
});
});

res.set('Content-Type', 'application/hal+json');
res.hal({
data: {
total: result.total,
page: parameters.page,
limit: parameters.limit
},
links: links,
embeds: {
'tickets': tickets
}
});
})
.fail(function (err) {
console.log(err);
Expand Down
4 changes: 2 additions & 2 deletions static/app/scripts/controllers/board.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ angular.module('retrospectApp')

$scope.updateTickets = function () {
console.log('Updating tickets.');
tickets.get({'page': $scope.page, 'limit': $scope.limit, 'retroId': $scope.retroId}, function (tickets) {
tickets.get({'page': $scope.page, 'limit': $scope.limit, 'retroId': $scope.retroId}, function (response) {
console.log('Got tickets');
$scope.tickets = tickets.results;
$scope.tickets = response._embedded.tickets;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As a rule of thumb, when you access an underscore prefixed var there's probably a better way that something could be done. Is this object member created by express-hal or you?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the underscored _embed comes from express-hal.

http://stateless.co/hal_specification.html

I guess we need to deliver Content-Type: application/hal+json though.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed Content-Type to application/hal+json

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we might be able to use http://weluse.github.io/hyperagent/ for this, but I haven't quite figured it out yet .

});
};

Expand Down
2 changes: 1 addition & 1 deletion static/app/scripts/controllers/retrospectives.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ angular.module('retrospectApp').controller('RetroCtrl', [

// this throws an error with cross domain
retrospectives.get({'limit': $scope.limit, 'page': $scope.page},function (response) {
$scope.retrospectives = response.results;
$scope.retrospectives = response._embedded.retrospectives;
});
}
]);