Skip to content

Commit

Permalink
Merge pull request #18 from nsonanh/feature-get-language-impl
Browse files Browse the repository at this point in the history
Feature get language impl
  • Loading branch information
nsonanh committed Jun 26, 2017
2 parents 1b70ac5 + 81ca45e commit f26f3f4
Show file tree
Hide file tree
Showing 8 changed files with 274 additions and 12 deletions.
41 changes: 40 additions & 1 deletion app/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ describe('Danhngon', () => {

beforeEach((done) => { //Before each test we empty the database
Danhngon.remove({}, (err) => {
done();
done();
});
});

after((done) => { //After all tests we empty the database
Danhngon.remove({}, (err) => {
done();
});
});

Expand Down Expand Up @@ -228,4 +234,37 @@ describe('Danhngon', () => {
});
});
});

/*
* Test the /GET/api/language/:language route and return all danhngon in :language
*/
describe('/GET/api/danhngon/language/:lang danhngon', () => {
it('it should GET all danhngon by language', (done) => {
let newDanhngon = new Danhngon({ content: "one two three", author: "test author", language: "en"});
newDanhngon.save(function(err, danhngon) {
if (err) {
assert.fail(0, 1, 'Could not save danhngon');
}
});

let newDanhngon2 = new Danhngon({ content: "một hai ba", author: "test author", language: "vi"});
newDanhngon2.save(function(err, danhngon) {
chai.request(server)
.get('/api/danhngon/language/vi')
.send(danhngon)
.end((err, res) => {
res.should.have.status(200);
res.body.should.be.a('array');
res.body.length.should.be.eql(1);
res.body[0].should.be.a('object');
res.body[0].should.have.property('content').eql("một hai ba");
res.body[0].should.have.property('author');
res.body[0].should.have.property('language');
res.body[0].should.have.property('_id').eql(danhngon.id);
done();
});
});
});
});

});
3 changes: 2 additions & 1 deletion lib/input-validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ var validateParams = function(req, res) {
}

var validateLangParam = function(req, res) {
var lang = req.params.translatedlanguage;
var lang = req.params.language;
if (lang !== 'auto') {
var checkForHexRegExp = new RegExp("^[a-zA-Z]{2}$");
if (!checkForHexRegExp.test(lang)) {
console.log("lang: " + lang);
res.json({ message: "error: invalid input, please enter valid language ISO code." });
return false;
}
Expand Down
22 changes: 19 additions & 3 deletions lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ var handleGetRandom = function(req, res) {
}

var handleGetRandomWithLang = function(req, res) {
var lang = req.params.translatedlanguage;
var lang = req.params.language;
Danhngon.count().exec(function(err, count) {
var random = Math.floor(Math.random() * count);
Danhngon.findOne().skip(random).exec(function (err, danhngon) {
Expand Down Expand Up @@ -129,7 +129,7 @@ var handleGetWithID = function(req, res) {

var handleGetWithIDAndLang = function(req, res) {
if (inputValidator.validateID(req, res)) {
var lang = req.params.translatedlanguage;
var lang = req.params.language;
Danhngon.findById(req.params.danhngon_id).exec(function (err, danhngon) {
if (err) {
res.json({ message: 'error: ', err });
Expand All @@ -144,6 +144,21 @@ var handleGetWithIDAndLang = function(req, res) {
}
}

var handleGetWithLanguage = function(req, res) {
if (inputValidator.validateLangParam(req, res)) {
var lang = req.params.language;
Danhngon.find({ language: lang }, function (err, danhngon) {
if (err) {
res.json({ message: 'error: ', err });
}else if (!danhngon) {
res.json({ message: "error: can't find danhngon by language." });
} else {
res.json(danhngon);
}
});
}
}

module.exports = {
handlePost: handlePost,
handlePut: handlePut,
Expand All @@ -152,5 +167,6 @@ module.exports = {
handleGetRandom: handleGetRandom,
handleGetRandomWithLang: handleGetRandomWithLang,
handleGetWithID: handleGetWithID,
handleGetWithIDAndLang: handleGetWithIDAndLang
handleGetWithIDAndLang: handleGetWithIDAndLang,
handleGetWithLanguage: handleGetWithLanguage
};
85 changes: 85 additions & 0 deletions public/apidoc/api_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,91 @@ define({ "api": [
"groupTitle": "Danhngon",
"name": "GetApiDanhngonId"
},
{
"type": "get",
"url": "/api/danhngon/:language",
"title": "Find a danhngon with language",
"version": "1.0.0",
"group": "Danhngon",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "language",
"description": "<p>danhngon language (in ISO code)</p>"
}
]
}
},
"success": {
"fields": {
"Success 200": [
{
"group": "Success 200",
"type": "Number",
"optional": false,
"field": "danhngon._id",
"description": "<p>danhngon id</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.content",
"description": "<p>danhngon content</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.author",
"description": "<p>danhngon author</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.language",
"description": "<p>danhngon language</p>"
},
{
"group": "Success 200",
"type": "Date",
"optional": false,
"field": "danhngon.created_at",
"description": "<p>Register's date</p>"
}
]
},
"examples": [
{
"title": "Success",
"content": "HTTP/1.1 200 OK\n{\n \"_id\": 594634907c371c3e209e3446,\n \"content\": \"A smile is the universal welcome.\",\n \"author\": \"Max Eastman\",\n \"language\": \"en\",\n \"created_at\": \"2017-06-18T08:06:40.926Z\"\n}",
"type": "json"
}
]
},
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by language.\"\n}",
"type": "json"
},
{
"title": "Find error",
"content": "HTTP/1.1 500 Internal Server Error",
"type": "json"
}
]
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonLanguage"
},
{
"type": "get",
"url": "/api/danhngon/random",
Expand Down
85 changes: 85 additions & 0 deletions public/apidoc/api_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,91 @@
"groupTitle": "Danhngon",
"name": "GetApiDanhngonId"
},
{
"type": "get",
"url": "/api/danhngon/:language",
"title": "Find a danhngon with language",
"version": "1.0.0",
"group": "Danhngon",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "language",
"description": "<p>danhngon language (in ISO code)</p>"
}
]
}
},
"success": {
"fields": {
"Success 200": [
{
"group": "Success 200",
"type": "Number",
"optional": false,
"field": "danhngon._id",
"description": "<p>danhngon id</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.content",
"description": "<p>danhngon content</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.author",
"description": "<p>danhngon author</p>"
},
{
"group": "Success 200",
"type": "String",
"optional": false,
"field": "danhngon.language",
"description": "<p>danhngon language</p>"
},
{
"group": "Success 200",
"type": "Date",
"optional": false,
"field": "danhngon.created_at",
"description": "<p>Register's date</p>"
}
]
},
"examples": [
{
"title": "Success",
"content": "HTTP/1.1 200 OK\n{\n \"_id\": 594634907c371c3e209e3446,\n \"content\": \"A smile is the universal welcome.\",\n \"author\": \"Max Eastman\",\n \"language\": \"en\",\n \"created_at\": \"2017-06-18T08:06:40.926Z\"\n}",
"type": "json"
}
]
},
"error": {
"examples": [
{
"title": "danhngon not found",
"content": "HTTP/1.1 200 OK\n{\n \"message\": \"error: can't find danhngon by language.\"\n}",
"type": "json"
},
{
"title": "Find error",
"content": "HTTP/1.1 500 Internal Server Error",
"type": "json"
}
]
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonLanguage"
},
{
"type": "get",
"url": "/api/danhngon/random",
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-21T16:46:01.771Z",
"time": "2017-06-26T15:25:11.992Z",
"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-21T16:46:01.771Z",
"time": "2017-06-26T15:25:11.992Z",
"url": "http://apidocjs.com",
"version": "0.17.6"
}
Expand Down
46 changes: 41 additions & 5 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ router.route('/danhngon/random')

// on routes that end in /danhngon/random/:tramslatedLanguage
// ----------------------------------------------------
router.route('/danhngon/random/:translatedlanguage')
router.route('/danhngon/random/:language')

// get the random translated danhngon (accessed at GET http://localhost:8080/api/danhngon/random)
/**
Expand Down Expand Up @@ -186,6 +186,42 @@ router.route('/danhngon/random/:translatedlanguage')
requestHandler.handleGetRandomWithLang(req, res);
});

// on routes that end in /danhngon/:language
// ----------------------------------------------------
router.route('/danhngon/language/:language')

// get the danhngon with that language (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id)
/**
* @api {get} /api/danhngon/:language Find a danhngon with language
* @apiVersion 1.0.0
* @apiGroup Danhngon
* @apiParam {String} language danhngon language (in ISO code)
* @apiSuccess {Number} danhngon._id danhngon id
* @apiSuccess {String} danhngon.content danhngon content
* @apiSuccess {String} danhngon.author danhngon author
* @apiSuccess {String} danhngon.language danhngon language
* @apiSuccess {Date} danhngon.created_at Register's date
* @apiSuccessExample {json} Success
* HTTP/1.1 200 OK
* {
* "_id": 594634907c371c3e209e3446,
* "content": "A smile is the universal welcome.",
* "author": "Max Eastman",
* "language": "en",
* "created_at": "2017-06-18T08:06:40.926Z"
* }
* @apiErrorExample {json} danhngon not found
* HTTP/1.1 200 OK
* {
* "message": "error: can't find danhngon by language."
* }
* @apiErrorExample {json} Find error
* HTTP/1.1 500 Internal Server Error
*/
.get(function(req, res) {
requestHandler.handleGetWithLanguage(req, res);
})

// on routes that end in /danhngon/:danhngon_id
// ----------------------------------------------------
router.route('/danhngon/:danhngon_id')
Expand Down Expand Up @@ -287,13 +323,13 @@ router.route('/danhngon/:danhngon_id')
requestHandler.handleDelete(req, res);
});

// on routes that end in /danhngon/:danhngon_id/:translatedlanguage
// on routes that end in /danhngon/:danhngon_id/:language
// ----------------------------------------------------
router.route('/danhngon/:danhngon_id/:translatedlanguage')
router.route('/danhngon/:danhngon_id/:language')

// get the danhngon with that id
// (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id/:translatedlanguage)
// get the translated danhngon (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id/:translatedlanguage)
// (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id/:language)
// get the translated danhngon (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id/:language)
/**
* @api {get} /api/danhngon/:danhngon_id/:language Find a translated danhngon with id
* @apiVersion 1.0.0
Expand Down

0 comments on commit f26f3f4

Please sign in to comment.