-
Notifications
You must be signed in to change notification settings - Fork 2
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
Vote for ideas #51
Vote for ideas #51
Changes from 4 commits
4753101
4535bdf
c8dcbd6
acf84fb
374ba1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 }; |
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 }; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there such a word? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I doubt that. Do you have some alternative suggestions? Perhaps There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For me primarys can stay it doesn't make a confusion (just lingual one) and it is quite clear. I was mostly curious. |
||
|
||
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) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are we not returning any comment for those errors? Is it because it is boring to write or somehow unnecessary with the frontend? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added error response body.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking more generally if there is any standard in ditup regarding this and what is the explanation. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
// duplicate vote | ||
case 409: { | ||
return res.status(409).end(); | ||
} | ||
// missing idea | ||
case 404: { | ||
return res.status(404).end(); | ||
} | ||
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); | ||
|
||
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) { | ||
// missing idea | ||
case 404: { | ||
return res.status(404).end(); | ||
} | ||
default: { | ||
return next(e); | ||
} | ||
} | ||
} | ||
} | ||
|
||
module.exports = { del, post }; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is very nice to have this factory function for this.