Skip to content

Commit

Permalink
Merge pull request #20 from nsonanh/feature-get-author-impl
Browse files Browse the repository at this point in the history
[feature-get-author-impl] Implement GET by author
  • Loading branch information
nsonanh committed Jun 26, 2017
2 parents 58ae2ce + 47e0f0d commit a893d62
Show file tree
Hide file tree
Showing 7 changed files with 266 additions and 12 deletions.
32 changes: 32 additions & 0 deletions app/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,36 @@ describe('Danhngon', () => {
});
});

/*
* Test the /GET/api/author/:author route and return all danhngon from :author
*/
describe('/GET/api/danhngon/author/:author danhngon', () => {
it('it should GET all danhngon from author', (done) => {
let newDanhngon = new Danhngon({ content: "one two three", author: "test author1", 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 author2", language: "vi"});
newDanhngon2.save(function(err, danhngon) {
chai.request(server)
.get('/api/danhngon/author/test%20author2')
.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();
});
});
});
});

});
18 changes: 17 additions & 1 deletion lib/request-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,21 @@ var handleGetWithLanguage = function(req, res) {
}
}

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

module.exports = {
handlePost: handlePost,
handlePut: handlePut,
Expand All @@ -168,5 +183,6 @@ module.exports = {
handleGetRandomWithLang: handleGetRandomWithLang,
handleGetWithID: handleGetWithID,
handleGetWithIDAndLang: handleGetWithIDAndLang,
handleGetWithLanguage: handleGetWithLanguage
handleGetWithLanguage: handleGetWithLanguage,
handleGetWithAuthor: handleGetWithAuthor
};
91 changes: 88 additions & 3 deletions public/apidoc/api_data.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,91 @@ define({ "api": [
"groupTitle": "Danhngon",
"name": "GetApiDanhngon"
},
{
"type": "get",
"url": "/api/danhngon/author/:author",
"title": "Find all danhngon with author",
"version": "1.0.0",
"group": "Danhngon",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "author",
"description": "<p>danhngon author</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 from author.\"\n}",
"type": "json"
},
{
"title": "Find error",
"content": "HTTP/1.1 500 Internal Server Error",
"type": "json"
}
]
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonAuthorAuthor"
},
{
"type": "get",
"url": "/api/danhngon/:danhngon_id/:language",
Expand Down Expand Up @@ -303,7 +388,7 @@ define({ "api": [
},
{
"type": "get",
"url": "/api/danhngon/:language",
"url": "/api/danhngon/language/:language",
"title": "Find all danhngon with original language",
"version": "1.0.0",
"group": "Danhngon",
Expand All @@ -315,7 +400,7 @@ define({ "api": [
"type": "String",
"optional": false,
"field": "language",
"description": "<p>danhngon language (in ISO code)</p>"
"description": "<p>danhngon language (in ISO code. If &quot;auto&quot;, browser's language will be used.)</p>"
}
]
}
Expand Down Expand Up @@ -384,7 +469,7 @@ define({ "api": [
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonLanguage"
"name": "GetApiDanhngonLanguageLanguage"
},
{
"type": "get",
Expand Down
91 changes: 88 additions & 3 deletions public/apidoc/api_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,91 @@
"groupTitle": "Danhngon",
"name": "GetApiDanhngon"
},
{
"type": "get",
"url": "/api/danhngon/author/:author",
"title": "Find all danhngon with author",
"version": "1.0.0",
"group": "Danhngon",
"parameter": {
"fields": {
"Parameter": [
{
"group": "Parameter",
"type": "String",
"optional": false,
"field": "author",
"description": "<p>danhngon author</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 from author.\"\n}",
"type": "json"
},
{
"title": "Find error",
"content": "HTTP/1.1 500 Internal Server Error",
"type": "json"
}
]
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonAuthorAuthor"
},
{
"type": "get",
"url": "/api/danhngon/:danhngon_id/:language",
Expand Down Expand Up @@ -303,7 +388,7 @@
},
{
"type": "get",
"url": "/api/danhngon/:language",
"url": "/api/danhngon/language/:language",
"title": "Find all danhngon with original language",
"version": "1.0.0",
"group": "Danhngon",
Expand All @@ -315,7 +400,7 @@
"type": "String",
"optional": false,
"field": "language",
"description": "<p>danhngon language (in ISO code)</p>"
"description": "<p>danhngon language (in ISO code. If &quot;auto&quot;, browser's language will be used.)</p>"
}
]
}
Expand Down Expand Up @@ -384,7 +469,7 @@
},
"filename": "./server.js",
"groupTitle": "Danhngon",
"name": "GetApiDanhngonLanguage"
"name": "GetApiDanhngonLanguageLanguage"
},
{
"type": "get",
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-26T15:37:00.253Z",
"time": "2017-06-26T16:10:32.112Z",
"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-26T15:37:00.253Z",
"time": "2017-06-26T16:10:32.112Z",
"url": "http://apidocjs.com",
"version": "0.17.6"
}
Expand Down
42 changes: 39 additions & 3 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ router.route('/danhngon/random/:language')
// ----------------------------------------------------
router.route('/danhngon/language/:language')

// get the danhngon with that language (accessed at GET http://localhost:8080/api/danhngon/:danhngon_id)
// get the danhngon with that language (accessed at GET http://localhost:8080/api/danhngon/language/:language)
/**
* @api {get} /api/danhngon/:language Find all danhngon with original language
* @api {get} /api/danhngon/language/:language Find all danhngon with original language
* @apiVersion 1.0.0
* @apiGroup Danhngon
* @apiParam {String} language danhngon language (in ISO code)
* @apiParam {String} language danhngon language (in ISO code. If "auto", browser's language will be used.)
* @apiSuccess {Number} danhngon._id danhngon id
* @apiSuccess {String} danhngon.content danhngon content
* @apiSuccess {String} danhngon.author danhngon author
Expand All @@ -222,6 +222,42 @@ router.route('/danhngon/language/:language')
requestHandler.handleGetWithLanguage(req, res);
})

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

// get the danhngon with that author (accessed at GET http://localhost:8080/api/danhngon/author/:author)
/**
* @api {get} /api/danhngon/author/:author Find all danhngon with author
* @apiVersion 1.0.0
* @apiGroup Danhngon
* @apiParam {String} author danhngon author
* @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 from author."
* }
* @apiErrorExample {json} Find error
* HTTP/1.1 500 Internal Server Error
*/
.get(function(req, res) {
requestHandler.handleGetWithAuthor(req, res);
})

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

0 comments on commit a893d62

Please sign in to comment.