Skip to content

Commit

Permalink
Merge pull request #16 from nsonanh/feature-json-response-update
Browse files Browse the repository at this point in the history
[feature-json-response-update] Update all response types to be of cod…
  • Loading branch information
nsonanh committed Jun 21, 2017
2 parents dc81bfa + 3b4f54a commit af48e2d
Show file tree
Hide file tree
Showing 9 changed files with 158 additions and 61 deletions.
28 changes: 23 additions & 5 deletions app/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ describe('Danhngon', () => {
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('errors');
res.body.errors.should.have.property('content');
res.body.errors.content.should.have.property('kind').eql('required');
res.body.should.have.property('message').eql("error: invalid input, please enter params: content, author and original language.");
done();
});
});
Expand All @@ -54,7 +52,7 @@ describe('Danhngon', () => {
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message').eql('Danhngon created!');
res.body.should.have.property('message').eql('danhngon created!');
res.body.danhngon.should.have.property('content');
res.body.danhngon.should.have.property('author');
res.body.danhngon.should.have.property('language');
Expand Down Expand Up @@ -84,6 +82,26 @@ describe('Danhngon', () => {
});
});

/*
* Test the /PUT/:id route
*/
describe('/PUT/:id danhngon', () => {
it('it should UPDATE a danhngon given the id', (done) => {
let danhngon = new Danhngon({ content: "Lies have many variations, truth has none", author: "African proverb", language: "en"})
danhngon.save(function(err, danhngon) {
chai.request(server)
.put('/api/danhngon/' + danhngon.id)
.send({ content: "Lies have many variations, truth has none", author: "African proverb" })
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message').eql("error: invalid input, please enter params: content, author and original language.");
done();
});
});
});
});

/*
* Test the /DELETE/:id route
*/
Expand All @@ -96,7 +114,7 @@ describe('Danhngon', () => {
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('object');
res.body.should.have.property('message').eql('Danhngon successfully deleted!');
res.body.should.have.property('message').eql('danhngon successfully deleted!');
done();
});
});
Expand Down
9 changes: 3 additions & 6 deletions lib/input-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
var validateID = function(req, res) {
var checkForHexRegExp = new RegExp("^[0-9a-fA-F]{24}$");
if (!checkForHexRegExp.test(req.params.danhngon_id)) {
res.status(404) // HTTP status 404: NotFound
.send('Invalid input, please enter valid _id.');
res.json({ message: "error: invalid input, please enter valid _id." });
return false;
}
return true;
}

var validateParams = function(req, res) {
if (!req.body.content || !req.body.author || !req.body.language) {
res.status(404) // HTTP status 404: NotFound
.send('Invalid input, please enter params: content, author and original language.');
res.json({ message: "error: invalid input, please enter params: content, author and original language." });
return false;
}
return true;
Expand All @@ -25,8 +23,7 @@ var validateLangParam = function(req, res) {
if (lang !== 'auto') {
var checkForHexRegExp = new RegExp("^[a-zA-Z]{2}$");
if (!checkForHexRegExp.test(lang)) {
res.status(404) // HTTP status 404: NotFound
.send('Invalid input, please enter valid language ISO code.');
res.json({ message: "error: invalid input, please enter valid language ISO code." });
return false;
}
}
Expand Down
67 changes: 37 additions & 30 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,43 +23,50 @@ db.on('error', console.error.bind(console, 'connection error:'));
var handlePost = function(req, res) {
var newDanhngon = new Danhngon(req.body);
// save the danhngon and check for errors
newDanhngon.save(function(err, danhngon) {
if (err) {
res.send(err);
} else {
res.json({ message: 'Danhngon created!', danhngon });
}
});
}

var handlePut = function(req, res) {
if (inputValidator.validateID(req, res)) {
// use our danhngon model to find the danhngon we want
Danhngon.findById(req.params.danhngon_id, function(err, danhngon) {
if (inputValidator.validateParams(req, res)) {
newDanhngon.save(function(err, danhngon) {
if (err) {
res.send(err);
} else if (!danhngon) {
res.status(404).send("Can't find danhngon by id."); // HTTP status 404: NotFound
res.json({ message: 'error: ', err });
} else {
// save the danhngon
Object.assign(danhngon, req.body).save((err, danhngon) => {
if(err) res.send(err);
res.json({ message: 'danhngon updated!', danhngon });
});
res.json({ message: 'danhngon created!', danhngon });
}
});
}
}

var handlePut = function(req, res) {
if (inputValidator.validateParams(req, res)) {
if (inputValidator.validateID(req, res)) {
// use our danhngon model to find the danhngon we want
Danhngon.findById(req.params.danhngon_id, function(err, danhngon) {
if (err) {
res.json({ message: 'error: ', err });
} else if (!danhngon) {
res.json({ message: "error: can't find danhngon by id." });
} else {
// save the danhngon
Object.assign(danhngon, req.body).save((err, danhngon) => {
if(err) {
res.json({ message: 'error: ', err });
} else {
res.json({ message: 'danhngon updated!', danhngon });
}
});
}
});
}
}
}

var handleDelete = function(req, res) {
if (inputValidator.validateID(req, res)) {
Danhngon.remove({
_id: req.params.danhngon_id
}, function(err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else {
res.json({ message: 'Danhngon successfully deleted!' });
res.json({ message: 'danhngon successfully deleted!' });
}
});
}
Expand All @@ -68,7 +75,7 @@ var handleDelete = function(req, res) {
var handleGet = function(req, res) {
Danhngon.find(function(err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else {
res.json(danhngon);
}
Expand All @@ -82,7 +89,7 @@ var handleGetRandom = function(req, res) {
Danhngon.findOne().skip(random).exec(
function (err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else {
res.json(danhngon);
}
Expand All @@ -96,7 +103,7 @@ var handleGetRandomWithLang = function(req, res) {
var random = Math.floor(Math.random() * count);
Danhngon.findOne().skip(random).exec(function (err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else if (!lang) {
res.json(danhngon);
} else if (inputValidator.validateLangParam(req, res)) {
Expand All @@ -110,9 +117,9 @@ var handleGetWithID = function(req, res) {
if (inputValidator.validateID(req, res)) {
Danhngon.findById(req.params.danhngon_id, function(err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else if (!danhngon) {
res.status(404).send("Can't find danhngon by id."); // HTTP status 404: NotFound
res.json({ message: "error: can't find danhngon by id." });
} else {
res.json(danhngon);
}
Expand All @@ -125,9 +132,9 @@ var handleGetWithIDAndLang = function(req, res) {
var lang = req.params.translatedlanguage;
Danhngon.findById(req.params.danhngon_id).exec(function (err, danhngon) {
if (err) {
res.send(err);
res.json({ message: 'error: ', err });
} else if (!danhngon) {
res.status(404).send("Can't find danhngon by id."); // HTTP status 404: NotFound
res.json({ message: "error: can't find danhngon by id." });
} else if (!lang) {
res.json(danhngon);
} else if (inputValidator.validateLangParam(req, res)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/translate.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var sendTranslation = function (danhngon, lang, req, res) {
// get translation
googleTranslate.translate(danhngon.content, danhngon.language, lang, function(err, translation) {
if (err) {
res.status(err.response.statusCode).send(err.body);
res.json({ message: "error: cannot translate with language code: " + lang });
} else {
danhngon.content = translation.translatedText;
res.json(danhngon);
Expand Down
32 changes: 26 additions & 6 deletions public/apidoc/api_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@ define({ "api": [
"examples": [
{
"title": "Success",
"content": "HTTP/1.1 200 OK",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"danhngon successfully deleted!\"\n}",
"type": "json"
}
]
},
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "Delete error",
"content": "HTTP/1.1 500 Internal Server Error",
Expand Down Expand Up @@ -192,7 +197,12 @@ define({ "api": [
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "cannot translate danhngon",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: cannot translate with language code: :language.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -277,7 +287,7 @@ define({ "api": [
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -349,7 +359,7 @@ define({ "api": [
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find random danhngon.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -433,8 +443,8 @@ define({ "api": [
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"title": "cannot translate danhngon",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: cannot translate with language code: :language.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -607,6 +617,16 @@ define({ "api": [
},
"error": {
"examples": [
{
"title": "lack param",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"Invalid input, please enter params: content, author and original language.\"\n}",
"type": "json"
},
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "Register error",
"content": "HTTP/1.1 500 Internal Server Error",
Expand Down
32 changes: 26 additions & 6 deletions public/apidoc/api_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,18 @@
"examples": [
{
"title": "Success",
"content": "HTTP/1.1 200 OK",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"danhngon successfully deleted!\"\n}",
"type": "json"
}
]
},
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "Delete error",
"content": "HTTP/1.1 500 Internal Server Error",
Expand Down Expand Up @@ -192,7 +197,12 @@
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "cannot translate danhngon",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: cannot translate with language code: :language.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -277,7 +287,7 @@
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -349,7 +359,7 @@
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find random danhngon.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -433,8 +443,8 @@
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 404 Not Found",
"title": "cannot translate danhngon",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: cannot translate with language code: :language.\"\n}",
"type": "json"
},
{
Expand Down Expand Up @@ -607,6 +617,16 @@
},
"error": {
"examples": [
{
"title": "lack param",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"Invalid input, please enter params: content, author and original language.\"\n}",
"type": "json"
},
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by id.\"\n}",
"type": "json"
},
{
"title": "Register error",
"content": "HTTP/1.1 500 Internal Server Error",
Expand Down
2 changes: 1 addition & 1 deletion public/apidoc/api_project.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ define({
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2017-06-18T10:43:33.559Z",
"time": "2017-06-21T16:46:01.771Z",
"url": "http://apidocjs.com",
"version": "0.17.6"
}
Expand Down
2 changes: 1 addition & 1 deletion public/apidoc/api_project.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"apidoc": "0.3.0",
"generator": {
"name": "apidoc",
"time": "2017-06-18T10:43:33.559Z",
"time": "2017-06-21T16:46:01.771Z",
"url": "http://apidocjs.com",
"version": "0.17.6"
}
Expand Down

0 comments on commit af48e2d

Please sign in to comment.