Skip to content

Commit

Permalink
Refactored web app for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
sourishkrout committed Sep 15, 2018
1 parent 0b3feb3 commit 50fde37
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 12 deletions.
2 changes: 1 addition & 1 deletion lib/shortcode.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,5 @@
"VoteRelaxed": ":relaxed:",
"VoteSunglasses": ":sunglasses:",
"VoteJoy": ":joy:",
"VotePoop": "ERROR"
"VotePoop": ":poop:"
}
27 changes: 16 additions & 11 deletions services/nodevoto-web/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,20 @@ class App {
routes.get('/api/leaderboard', this.handleLeaderboard.bind(this));
}

async handleLeaderboard(req, res) {
const findByShortcode = wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient));
const getResults = wrapOp(this.votingClient.Results.bind(this.votingClient));
_FindByShortcode(arg) {
return wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient))(arg);
}

_Results() {
return wrapOp(this.votingClient.Results.bind(this.votingClient))();
}

async handleLeaderboard(req, res) {
try {
let response = await getResults();
let response = await this._Results();

let list = response.results.map(async (item) => {
return findByShortcode({ Shortcode: item.Shortcode }).then(r => {
return this._FindByShortcode({ Shortcode: item.Shortcode }).then(r => {
return { 'shortcode': r.Emoji.shortcode,
'unicode': r.Emoji.unicode,
'votes': item.Votes };
Expand All @@ -60,17 +65,17 @@ class App {

async handleVoteEmoji(req, res) {
let emojiShortcode = req.query['choice'];

let response;
let vote;

if (emojiShortcode === undefined || emojiShortcode === '') {
logger.error(`Emoji choice [${emojiShortcode}] is mandatory`);
return res.status(400).end();
}

let vote;
const findByShortcode = wrapOp(this.emojiClient.FindByShortcode.bind(this.emojiClient));

try {
response = await findByShortcode({ Shortcode: emojiShortcode });
response = await this._FindByShortcode({ Shortcode: emojiShortcode });
} catch (err) {
logger.error(err);
return res.status(500).json(err.message);
Expand All @@ -89,8 +94,8 @@ class App {

if (op !== null && this.votingClient[op] !== undefined) {
vote = wrapOp(this.votingClient[op].bind(this.votingClient));
} else if (emojiShortcode === ':poop:') {
vote = wrapOp(this.votingClient.VotePoop.bind(this.votingClient));
} else {
logger.error(`Emoji lacks implementation of rpc operation [${op}]`);
}

try {
Expand Down
17 changes: 17 additions & 0 deletions test/nodevoto-web/app.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ const testMap = [
},{
unicode: '🐷',
shortcode: ':pig:'
},{
unicode: '💩',
shortcode: ':poop:'
}
];

Expand Down Expand Up @@ -70,6 +73,10 @@ class VotingMock {
this.inc(':ghost:');
return callback(null);
}

VotePoop (args, callback) {
return callback('Unkown error', null);
}
}

describe('app', () => {
Expand Down Expand Up @@ -105,6 +112,16 @@ describe('app', () => {
expect(voting.get(':ghost:')).equals(1);
});

it('should reject vote for :poop: emoji', async() => {
try {
let response = await superget(`http://127.0.0.1:${WEB_PORT}/api/vote?choice=:poop:`);
expect(response).to.equal(null);
} catch(err) {
expect(err.status).equals(500);
expect(err.message).equals('Internal Server Error');
}
});

it('should reject vote without choice parameter', async() => {
try {
let response = await superget(`http://127.0.0.1:${WEB_PORT}/api/vote`);
Expand Down

0 comments on commit 50fde37

Please sign in to comment.