-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbooksManager.js
More file actions
102 lines (94 loc) · 3.83 KB
/
booksManager.js
File metadata and controls
102 lines (94 loc) · 3.83 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
(function () {
var db = require("./dbconn.js");
exports.getAllBooks = function () {
return new Promise(function (resolve, reject) {
db.query().then(function (data) {
resolve(data);
}).catch(function (err) {
reject(err);
})
});
}
exports.searchBooks = function (searchKey) {
console.log("Searching for books that contain the string: [", searchKey, "]");
return new Promise(function (resolve, reject) {
db.query(searchKey).then(function (books) {
resolve(books);
}).catch(function (err) {
reject(err);
});
});
}
exports.getBookByIsbn = function (isbn) {
return new Promise(function (resolve, reject) {
db.getItem(isbn).then(function (book) {
resolve(book);
}).catch(function (err) {
reject(err);
});
});
}
exports.buyBook = function (isbn, quantity) {
console.log("Buying book with ISBN ", isbn, ", quantity ", quantity);
return new Promise(function (resolve, reject) {
db.getItem(isbn).then(function (book) {
if (book.quantity < quantity) {
reject("There are not enough books left in stock. Current Stock " + book.quantity);
return;
}
book.quantity -= quantity;
db.update(isbn, book).then(function (data) {
resolve();
}).catch(function (err) {
reject("Failed to buy the book: " + err);
});
}).catch(function (err) {
console.log("Book " + isbn + " was not found");
reject("The Book bearing ID " + isbn + " was not found: " + err);
});
});
}
isNullOrEmpty = function (value) {
if (value && value !== "") {
return true;
}
return false;
}
exports.addBook = function (book) {
console.log("Adding or updating book:", book);
return new Promise(function (resolve, reject) {
db.getItem(book.isbn).then(function (findIsbn) {
if (book.price && typeof book.price === 'number') {
findIsbn.price = book.price;
}
if (typeof book.quantity === 'number' && typeof findIsbn.quantity === 'number') {
findIsbn.quantity = book.quantity + findIsbn.quantity;
} else if (!typeof book.quantity === 'number') {
reject("Invalid quantity: " + book.quantity);
return;
} else {
findIsbn.quantity = Number(book.quantity) + Number(findIsbn.quantity);
}
db.update(book.isbn, findIsbn).then(function (data) {
resolve();
}).catch(function (err) {
reject("Failed to add the book: " + err);
});
}).catch(function (err) {
console.log(err);
if (isNullOrEmpty(book.title) ||
isNullOrEmpty(book.subtitle) ||
isNullOrEmpty(book.author) ||
(isNullOrEmpty(book.price) || typeof book.price !== 'number') ||
(isNullOrEmpty(book.quantity) || typeof book.price !== 'number')) {
reject("The book details are not sufficient, enter all/correct details");
}
db.createObj(book).then(function (data) {
resolve();
}).catch(function (err) {
reject(err);
});
});
});
}
}());