-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* vote for ideas: POST /ideas/:id/votes * Remove vote from idea DELETE /ideas/:id/votes/vote * show amount of up and down votes and my vote when GET /ideas/:id * generalize votes for other objects use it to vote for comments * add error messages, remove TODO comment
- Loading branch information
Showing
22 changed files
with
812 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const { id, voteValue: value } = require('./paths'); | ||
|
||
const postVotes = { | ||
properties: { | ||
params: { | ||
properties: { id }, | ||
required: ['id'], | ||
additionalProperties: false | ||
}, | ||
body: { | ||
properties: { value }, | ||
required: ['value'], | ||
additionalProperties: false | ||
}, | ||
required: ['body', 'params'] | ||
} | ||
}; | ||
|
||
const deleteVote = { | ||
properties: { | ||
params: { | ||
properties: { id }, | ||
required: ['id'], | ||
additionalProperties: false | ||
}, | ||
required: ['params'] | ||
} | ||
}; | ||
|
||
module.exports = { deleteVote, postVotes }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
'use strict'; | ||
|
||
const validate = require('./validate-by-schema'); | ||
|
||
const del = validate('deleteVote'); | ||
const post = validate('postVotes'); | ||
|
||
module.exports = { del, post }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
const path = require('path'), | ||
models = require(path.resolve('./models')), | ||
serializers = require(path.resolve('./serializers')); | ||
|
||
/** | ||
* Middleware to POST a vote to idea (and other objects in the future) | ||
*/ | ||
async function post(req, res, next) { | ||
|
||
// read data from request | ||
const { id } = req.params; | ||
const { value } = req.body; | ||
const { username } = req.auth; | ||
|
||
// what is the type of the object we vote for (i.e. ideas, comments, ...) | ||
const primarys = req.baseUrl.substring(1); | ||
const primary = primarys.slice(0, -1); | ||
|
||
try { | ||
// save the vote to database | ||
const vote = await models.vote.create({ from: username, to: { type: primarys, id }, value }); | ||
// respond | ||
const serializedVote = serializers.serialize.vote(vote); | ||
return res.status(201).json(serializedVote); | ||
} catch (e) { | ||
// handle errors | ||
switch (e.code) { | ||
// duplicate vote | ||
case 409: { | ||
return res.status(409).json({ | ||
errors: [{ | ||
status: 409, | ||
detail: 'duplicate vote' | ||
}] | ||
}); | ||
} | ||
// missing idea | ||
case 404: { | ||
return res.status(404).json({ | ||
errors: [{ | ||
status: 404, | ||
detail: `${primary} doesn't exist` | ||
}] | ||
}); | ||
|
||
} | ||
default: { | ||
return next(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Middleware to DELETE a vote from an idea (and other objects in the future). | ||
*/ | ||
async function del(req, res, next) { | ||
|
||
// read data from request | ||
const { id } = req.params; | ||
const { username } = req.auth; | ||
|
||
// what is the type of the object we vote for (i.e. ideas, comments, ...) | ||
const primarys = req.baseUrl.substring(1); | ||
const primary = primarys.slice(0, -1); | ||
|
||
try { | ||
// remove the vote from database | ||
await models.vote.remove({ from: username, to: { type: primarys, id } }); | ||
// respond | ||
return res.status(204).end(); | ||
} catch (e) { | ||
// handle errors | ||
switch (e.code) { | ||
// primary object or vote doesn't exist | ||
case 404: { | ||
return res.status(404).json({ | ||
errors: [{ | ||
status: 404, | ||
detail: `vote or ${primary} doesn't exist` | ||
}] | ||
}); | ||
} | ||
default: { | ||
return next(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { del, post }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.