/
AuthorService.js
40 lines (36 loc) · 1.17 KB
/
AuthorService.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
var BaseService = require('./BaseService'),
NotFound = require("../../lib/error/NotFound"),
Dao = require('../dao/Dao');
AuthorService = function(app) {
this.app = app;
this.dao = new Dao(app.pool, "author");
};
AuthorService.prototype = new BaseService();
/**
* Retrieves an Author and a list of their Books.
*
* @param {String} authorKey The key of the Author to return.
* @param {Function} callback The function to call when retrieval is complete.
*/
AuthorService.prototype.getAuthorAndTheirBooks = function(authorKey, callback) {
var self = this;
this.dao.getItemByKey(authorKey, function(error, author) {
if (error) {
callback(error);
} else if (!author) {
callback(new NotFound("Author not found"));
} else {
self.app.services.bookService.getBooksByAuthor(authorKey, function(error, books) {
if (error) {
callback(error);
} else {
author.bookList = books;
callback(null, author);
}
});
}
});
};
module.exports = function(app) {
return new AuthorService(app);
};