From 2fabfafe5c2a840d44e35ea55436045dd39ae5cb Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Sun, 13 Oct 2019 20:58:21 +0530 Subject: [PATCH 1/6] Single Route --- library/src/routes/bookRoutes.js | 16 +- library/src/views/bookInfo.ejs | 187 ++++++++++++++++++ library/src/views/{books.ejs => bookList.ejs} | 0 3 files changed, 200 insertions(+), 3 deletions(-) create mode 100644 library/src/views/bookInfo.ejs rename library/src/views/{books.ejs => bookList.ejs} (100%) diff --git a/library/src/routes/bookRoutes.js b/library/src/routes/bookRoutes.js index ce69c36..f6ef9c3 100644 --- a/library/src/routes/bookRoutes.js +++ b/library/src/routes/bookRoutes.js @@ -52,7 +52,7 @@ const books = [{ bookRouter.route('/') .get((req, res) => { - res.render('books', + res.render('bookList', { title: 'Books', nav: [ @@ -64,9 +64,19 @@ bookRouter.route('/') }) }); -bookRouter.route('/single') +bookRouter.route('/:id') .get((req, res) => { - res.send('Hello single books'); + const { id } = req.params; + res.render('bookInfo', + { + title: books[id].genre, + nav: [ + { link: '/books', title: 'Books' }, + { link: '/authors', title: 'Authors' }, + { link: '/genre', title: 'Genre' } + ], + book: books[id] + }) }); module.exports = bookRouter; \ No newline at end of file diff --git a/library/src/views/bookInfo.ejs b/library/src/views/bookInfo.ejs new file mode 100644 index 0000000..febebf9 --- /dev/null +++ b/library/src/views/bookInfo.ejs @@ -0,0 +1,187 @@ + + + + + + + <%=title%> + + + + + + + + + + + +
+
+
+
+

<%=title%> +

+

+
+
+
+
+
+ + +
+
+
+
+
+
+ +
+
+
+
+
+
+
+ +
+ + +
+
+ +
+ +
+
+ + +
+
+
+ +
+
+

<%=book.title%>

+
+
+

<%=book.author%>

+

+ 2 + hours ago • Read More +

+
+
+
+

+
+
+
+ + + + + + + + + More + + +
+
+ + + +
+ +
+
+ + +
+ + + +
+ +
+ + + + + + + + \ No newline at end of file diff --git a/library/src/views/books.ejs b/library/src/views/bookList.ejs similarity index 100% rename from library/src/views/books.ejs rename to library/src/views/bookList.ejs From 9a909cef6e7d3ac6df1ae921156bce2b82122056 Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Sun, 13 Oct 2019 21:14:47 +0530 Subject: [PATCH 2/6] Router Function --- library/index.js | 14 +-- library/src/routes/bookRoutes.js | 149 +++++++++++++++---------------- 2 files changed, 81 insertions(+), 82 deletions(-) diff --git a/library/index.js b/library/index.js index 4f1133d..e6af22c 100644 --- a/library/index.js +++ b/library/index.js @@ -20,17 +20,19 @@ app.set('views', path.join(__dirname, 'src', 'views')); //app.set('views', './src/views'); app.set('view engine', 'ejs'); -const bookRouter = require('./src/routes/bookRoutes'); +const nav = [ + { link: '/books', title: 'Book' }, + { link: '/authors', title: 'Author' }, + { link: '/genre', title: 'Genre' } +]; + +const bookRouter = require('./src/routes/bookRoutes')(nav); app.use('/books', bookRouter); app.get('/', (req, res) => res.render('index', { title: 'Safwan-Sana Library', - nav: [ - { link: '/books', title: 'Books' }, - { link: '/authors', title: 'Authors' }, - { link: '/genre', title: 'Genre' } - ] + nav } )); diff --git a/library/src/routes/bookRoutes.js b/library/src/routes/bookRoutes.js index f6ef9c3..fe1a498 100644 --- a/library/src/routes/bookRoutes.js +++ b/library/src/routes/bookRoutes.js @@ -1,82 +1,79 @@ const express = require('express'); const bookRouter = express.Router(); -const books = [{ - id: 1, - title: "Ansible 3.0", - genre: "Automation", - author: "Mohamed Safwan" -}, -{ - id: 2, - title: "Concourse", - genre: "Dev Ops", - author: "Aisha Sana" -}, -{ - id: 3, - title: "Mastering Node Applications", - genre: "Server Scripting", - author: "Mohamed Safwan" -}, -{ - id: 4, - title: "Learning Me", - genre: "Biography", - author: "Abdul Waheed" -}, -{ - id: 5, - title: "Worst Human Behaviours", - genre: "Fiction", - author: "Abdul Waheed" -}, -{ - id: 6, - title: "Space Shuttle", - genre: "Astro Science", - author: "Mohamed Safwan" -}, -{ - id: 7, - title: "Drawing Basics", - genre: "Kids Learning", - author: "Aisha Sana" -}, -{ - id: 8, - title: "Miserable", - genre: "Fiction", - author: "Abdul Waheed" -}] +function router(nav) { -bookRouter.route('/') - .get((req, res) => { - res.render('bookList', - { - title: 'Books', - nav: [ - { link: '/books', title: 'Books' }, - { link: '/authors', title: 'Authors' }, - { link: '/genre', title: 'Genre' } - ], - books - }) - }); + const books = [{ + id: 1, + title: "Ansible 3.0", + genre: "Automation", + author: "Mohamed Safwan" + }, + { + id: 2, + title: "Concourse", + genre: "Dev Ops", + author: "Aisha Sana" + }, + { + id: 3, + title: "Mastering Node Applications", + genre: "Server Scripting", + author: "Mohamed Safwan" + }, + { + id: 4, + title: "Learning Me", + genre: "Biography", + author: "Abdul Waheed" + }, + { + id: 5, + title: "Worst Human Behaviours", + genre: "Fiction", + author: "Abdul Waheed" + }, + { + id: 6, + title: "Space Shuttle", + genre: "Astro Science", + author: "Mohamed Safwan" + }, + { + id: 7, + title: "Drawing Basics", + genre: "Kids Learning", + author: "Aisha Sana" + }, + { + id: 8, + title: "Miserable", + genre: "Fiction", + author: "Abdul Waheed" + }] -bookRouter.route('/:id') - .get((req, res) => { - const { id } = req.params; - res.render('bookInfo', - { - title: books[id].genre, - nav: [ - { link: '/books', title: 'Books' }, - { link: '/authors', title: 'Authors' }, - { link: '/genre', title: 'Genre' } - ], - book: books[id] - }) - }); + bookRouter.route('/') + .get((req, res) => { + res.render('bookList', + { + title: 'Books', + nav, + books + }) + }); -module.exports = bookRouter; \ No newline at end of file + bookRouter.route('/:id') + .get((req, res) => { + const { id } = req.params; + res.render('bookInfo', + { + title: books[id].genre, + nav, + book: books[id] + }) + }); + return bookRouter; +} + + +module.exports = router; \ No newline at end of file From 6247a990d4ffad7d54c7eb1c7a5da14125b784a0 Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Tue, 15 Oct 2019 09:56:21 +0530 Subject: [PATCH 3/6] SQl Server Connectivity --- library/index.js | 14 ++- library/package-lock.json | 179 +++++++++++++++++++++++++++ library/package.json | 3 +- library/src/routes/bookRoutes-obj.js | 81 ++++++++++++ library/src/routes/bookRoutes.js | 33 ++++- 5 files changed, 302 insertions(+), 8 deletions(-) create mode 100644 library/src/routes/bookRoutes-obj.js diff --git a/library/index.js b/library/index.js index e6af22c..6b182eb 100644 --- a/library/index.js +++ b/library/index.js @@ -4,11 +4,23 @@ const chalk = require('chalk'); const debug = require('debug')('app'); const morgan = require('morgan'); const path = require('path'); - +const sql = require('mssql'); const app = express(); const port = process.env.PORT || 3000 +const config = { + user: 'smuser', + password: 'smuser', + server: 'localhost', // You can use 'localhost\\instance' to connect to named instance + database: 'sslib', + + options: { + encrypt: false // Use this if you're on Windows Azure + } +} + +sql.connect(config).catch(err=>debug(err)); app.use(morgan('tiny')); app.use(express.static(path.join(__dirname, '/public/'))); diff --git a/library/package-lock.json b/library/package-lock.json index 6a6f112..6c5d321 100644 --- a/library/package-lock.json +++ b/library/package-lock.json @@ -191,6 +191,22 @@ } } }, + "adal-node": { + "version": "0.1.28", + "resolved": "https://registry.npmjs.org/adal-node/-/adal-node-0.1.28.tgz", + "integrity": "sha1-RoxLs+u9lrEnBmn0ucuk4AZepIU=", + "requires": { + "@types/node": "8.10.54", + "async": "2.6.3", + "date-utils": "1.2.21", + "jws": "3.2.2", + "request": "2.88.0", + "underscore": "1.9.1", + "uuid": "3.3.3", + "xmldom": "0.1.27", + "xpath.js": "1.1.0" + } + }, "ajv": { "version": "6.10.2", "resolved": "https://registry.npmjs.org/ajv/-/ajv-6.10.2.tgz", @@ -584,6 +600,11 @@ "tweetnacl": "0.14.5" } }, + "big-number": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/big-number/-/big-number-1.0.0.tgz", + "integrity": "sha512-cHUzdT+mMXd1ozht8n5ZwBlNiPO/4zCqqkyp3lF1TMPsRJLXUbQ7cKnfXRkrW475H5SOtSOP0HFeihNbpa53MQ==" + }, "binary": { "version": "0.3.0", "resolved": "https://registry.npmjs.org/binary/-/binary-0.3.0.tgz", @@ -952,6 +973,11 @@ "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-0.2.13.tgz", "integrity": "sha1-DTM+PwDqxQqhRUq9MO+MKl2ackI=" }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "buffer-from": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/buffer-from/-/buffer-from-1.1.1.tgz", @@ -1611,6 +1637,11 @@ "resolved": "https://registry.npmjs.org/date-now/-/date-now-0.1.4.tgz", "integrity": "sha1-6vQ5/U1ISK105cx9vvIAZyueNFs=" }, + "date-utils": { + "version": "1.2.21", + "resolved": "https://registry.npmjs.org/date-utils/-/date-utils-1.2.21.tgz", + "integrity": "sha1-YfsWzcEnSzyayq/+n8ad+HIKK2Q=" + }, "debug": { "version": "4.1.1", "resolved": "https://registry.npmjs.org/debug/-/debug-4.1.1.tgz", @@ -1857,6 +1888,14 @@ "safer-buffer": "2.1.2" } }, + "ecdsa-sig-formatter": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", + "integrity": "sha512-nagl3RYrbNv6kQkeJIpt6NJZy8twLB/2vtz6yN9Z4vRKHN4/QZJIEbqohALSgwKdnksuY3k5Addp5lg8sVoVcQ==", + "requires": { + "safe-buffer": "5.1.2" + } + }, "ee-first": { "version": "1.1.1", "resolved": "https://registry.npmjs.org/ee-first/-/ee-first-1.1.1.tgz", @@ -2513,6 +2552,11 @@ "integrity": "sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=", "dev": true }, + "generic-pool": { + "version": "3.7.1", + "resolved": "https://registry.npmjs.org/generic-pool/-/generic-pool-3.7.1.tgz", + "integrity": "sha512-ug6DAZoNgWm6q5KhPFA+hzXfBLFQu5sTXxPpv44DmE0A2g+CiHoq9LTVdkXpZMkYVMoGw83F6W+WT0h0MFMK/w==" + }, "get-assigned-identifiers": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/get-assigned-identifiers/-/get-assigned-identifiers-1.2.0.tgz", @@ -3336,6 +3380,25 @@ "promise": "7.3.1" } }, + "jwa": { + "version": "1.4.1", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.4.1.tgz", + "integrity": "sha512-qiLX/xhEEFKUAJ6FiBMbes3w9ATzyk5W7Hvzpa/SLYdxNtng+gcurvrI7TbACjIXlsJyr05/S1oUhZrc63evQA==", + "requires": { + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.11", + "safe-buffer": "5.1.2" + } + }, + "jws": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.2.2.tgz", + "integrity": "sha512-YHlZCB6lMTllWDtSPHz/ZXTsi8S00usEV6v1tjq8tOUZzw7DpSDWVXjXDre6ed1w/pd495ODpHZYSdkRTsa0HA==", + "requires": { + "jwa": "1.4.1", + "safe-buffer": "5.1.2" + } + }, "kind-of": { "version": "3.2.2", "resolved": "https://registry.npmjs.org/kind-of/-/kind-of-3.2.2.tgz", @@ -3788,6 +3851,26 @@ "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz", "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==" }, + "mssql": { + "version": "5.1.0", + "resolved": "https://registry.npmjs.org/mssql/-/mssql-5.1.0.tgz", + "integrity": "sha512-eHrqRWCEBaXo48y2ZBaDleFvrWm2vYm6dNm1ci0XLYxm6kUb4KRsvjl74iKFhfYyuF9z6qzmTe/QmoQk+YVcVw==", + "requires": { + "debug": "3.2.6", + "generic-pool": "3.7.1", + "tedious": "4.2.0" + }, + "dependencies": { + "debug": { + "version": "3.2.6", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.2.6.tgz", + "integrity": "sha512-mel+jf7nrtEl5Pn1Qx46zARXKDpBbvzezse7p7LqINmdoIk8PYP5SySaxEmYv6TZ0JyEKA1hsCId6DIhgITtWQ==", + "requires": { + "ms": "2.1.2" + } + } + } + }, "mute-stream": { "version": "0.0.7", "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-0.0.7.tgz", @@ -3840,6 +3923,11 @@ "resolved": "https://registry.npmjs.org/nanosocket/-/nanosocket-1.1.0.tgz", "integrity": "sha512-v2LsjYMRu3/JT/z8Qpkj1X5dgwCptC3D0NzeYlb7tb2qGJhlx0PSXZJraf1tRPKipj2iwB15GRzqUaOlN+LieQ==" }, + "native-duplexpair": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/native-duplexpair/-/native-duplexpair-1.0.0.tgz", + "integrity": "sha1-eJkHjmS/PIo9cyYBs9QP8F21j6A=" + }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -7407,6 +7495,82 @@ } } }, + "tedious": { + "version": "4.2.0", + "resolved": "https://registry.npmjs.org/tedious/-/tedious-4.2.0.tgz", + "integrity": "sha512-Py59XmvMcYWdjc1qyXDsbBwQE3yM8CJzuDnagjRpwjgndaBQXBULDI3D6OxKClbTNxA3qaLBFd9DjfV+is3AYA==", + "requires": { + "adal-node": "0.1.28", + "big-number": "1.0.0", + "bl": "2.2.0", + "depd": "1.1.2", + "iconv-lite": "0.4.24", + "native-duplexpair": "1.0.0", + "punycode": "2.1.1", + "readable-stream": "3.4.0", + "sprintf-js": "1.1.2" + }, + "dependencies": { + "bl": { + "version": "2.2.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-2.2.0.tgz", + "integrity": "sha512-wbgvOpqopSr7uq6fJrLH8EsvYMJf9gzfo2jCsL2eTy75qXPukA4pCgHamOQkZtY5vmfVtjB+P3LNlMHW5CEZXA==", + "requires": { + "readable-stream": "2.3.6", + "safe-buffer": "5.1.2" + }, + "dependencies": { + "readable-stream": { + "version": "2.3.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.3.6.tgz", + "integrity": "sha512-tQtKA9WIAhBF3+VLAseyMqZeBjW0AHJoxOtYqSUZNJxauErmLbVm2FW1y+J/YA9dUrAC39ITejlZWhVIwawkKw==", + "requires": { + "core-util-is": "1.0.2", + "inherits": "2.0.3", + "isarray": "1.0.0", + "process-nextick-args": "2.0.1", + "safe-buffer": "5.1.2", + "string_decoder": "1.1.1", + "util-deprecate": "1.0.2" + } + } + } + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz", + "integrity": "sha1-u5NdSFgsuhaMBoNJV6VKPgcSTxE=" + }, + "punycode": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/punycode/-/punycode-2.1.1.tgz", + "integrity": "sha512-XRsRjdf+j5ml+y/6GKHPZbrF/8p2Yga0JPtdqTIY2Xe5ohJPD9saDJJLPvp9+NSBprVvevdXZybnj2cv8OEd0A==" + }, + "readable-stream": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.4.0.tgz", + "integrity": "sha512-jItXPLmrSR8jmTRmRWJXCnGJsfy85mB3Wd/uINMXA65yrnFo0cPClFIUWzo2najVNSl+mx7/4W8ttlLWJe99pQ==", + "requires": { + "inherits": "2.0.3", + "string_decoder": "1.1.1", + "util-deprecate": "1.0.2" + } + }, + "sprintf-js": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.1.2.tgz", + "integrity": "sha512-VE0SOVEHCk7Qc8ulkWw3ntAzXuqf7S2lvwQaDLRnUeIEaKNQJzV6BwmLKhOqT61aGhfUMrXeaBk+oDGCzvhcug==" + }, + "string_decoder": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.1.1.tgz", + "integrity": "sha512-n/ShnvDi6FHbbVfviro+WojiFzv+s8MPMHBczVePfUpDJLwoLT0ht1l4YwBCbi8pJAveEEdnkHyPyTP/mzRfwg==", + "requires": { + "safe-buffer": "5.1.2" + } + } + } + }, "term-size": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/term-size/-/term-size-1.2.0.tgz", @@ -7688,6 +7852,11 @@ } } }, + "underscore": { + "version": "1.9.1", + "resolved": "https://registry.npmjs.org/underscore/-/underscore-1.9.1.tgz", + "integrity": "sha512-5/4etnCkd9c8gwgowi5/om/mYO5ajCaOgdzj/oW+0eQV9WxKBDZw5+ycmKmeaTXjInS/W0BzpGLo2xR2aBwZdg==" + }, "union-value": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/union-value/-/union-value-1.0.1.tgz", @@ -8049,6 +8218,16 @@ "resolved": "https://registry.npmjs.org/xdg-basedir/-/xdg-basedir-3.0.0.tgz", "integrity": "sha1-SWsswQnsqNus/i3HK2A8F8WHCtQ=" }, + "xmldom": { + "version": "0.1.27", + "resolved": "https://registry.npmjs.org/xmldom/-/xmldom-0.1.27.tgz", + "integrity": "sha1-1QH5ezvbQDr4757MIFcxh6rawOk=" + }, + "xpath.js": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/xpath.js/-/xpath.js-1.1.0.tgz", + "integrity": "sha512-jg+qkfS4K8E7965sqaUl8mRngXiKb3WZGfONgE18pr03FUQiuSV6G+Ej4tS55B+rIQSFEIw3phdVAQ4pPqNWfQ==" + }, "xrs": { "version": "1.2.2", "resolved": "https://registry.npmjs.org/xrs/-/xrs-1.2.2.tgz", diff --git a/library/package.json b/library/package.json index e37b6c8..b32ad0a 100644 --- a/library/package.json +++ b/library/package.json @@ -5,7 +5,7 @@ "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "start": "set DEBUG=app & nodemon index.js", + "start": "set DEBUG=app:* & nodemon index.js", "lint": "eslint index.js" }, "author": "", @@ -18,6 +18,7 @@ "express": "^4.17.1", "jquery": "^3.4.1", "morgan": "^1.9.1", + "mssql": "^5.1.0", "nodemon": "^1.19.3", "popper": "^1.0.1", "pug": "^2.0.4" diff --git a/library/src/routes/bookRoutes-obj.js b/library/src/routes/bookRoutes-obj.js new file mode 100644 index 0000000..865e7e8 --- /dev/null +++ b/library/src/routes/bookRoutes-obj.js @@ -0,0 +1,81 @@ +const express = require('express'); +const bookRouter = express.Router(); +const sql = require('mssql'); +const debug = require('debug')('app:bookRoute'); + +function router(nav) { + + const books = [{ + id: 1, + title: "Ansible 3.0", + genre: "Automation", + author: "Mohamed Safwan" + }, + { + id: 2, + title: "Concourse", + genre: "Dev Ops", + author: "Aisha Sana" + }, + { + id: 3, + title: "Mastering Node Applications", + genre: "Server Scripting", + author: "Mohamed Safwan" + }, + { + id: 4, + title: "Learning Me", + genre: "Biography", + author: "Abdul Waheed" + }, + { + id: 5, + title: "Worst Human Behaviours", + genre: "Fiction", + author: "Abdul Waheed" + }, + { + id: 6, + title: "Space Shuttle", + genre: "Astro Science", + author: "Mohamed Safwan" + }, + { + id: 7, + title: "Drawing Basics", + genre: "Kids Learning", + author: "Aisha Sana" + }, + { + id: 8, + title: "Miserable", + genre: "Fiction", + author: "Abdul Waheed" + }] + + bookRouter.route('/') + .get((req, res) => { + res.render('bookList', + { + title: 'Books', + nav, + books + }) + }); + + bookRouter.route('/:id') + .get((req, res) => { + const { id } = req.params; + res.render('bookInfo', + { + title: books[id].genre, + nav, + book: books[id] + }) + }); + return bookRouter; +} + + +module.exports = router; \ No newline at end of file diff --git a/library/src/routes/bookRoutes.js b/library/src/routes/bookRoutes.js index fe1a498..c6cea25 100644 --- a/library/src/routes/bookRoutes.js +++ b/library/src/routes/bookRoutes.js @@ -1,5 +1,7 @@ const express = require('express'); const bookRouter = express.Router(); +const sql = require('mssql'); +const debug = require('debug')('app'); function router(nav) { @@ -51,15 +53,34 @@ function router(nav) { genre: "Fiction", author: "Abdul Waheed" }] + const request = new sql.Request(); + + //Promise example + /*bookRouter.route('/') + .get((req, res) => { + request.query('select * from books') + .then(result => { + debug(result); + res.render('bookList', + { + title: 'Books', + nav, + books: result.recordset + }) + }); + });*/ bookRouter.route('/') .get((req, res) => { - res.render('bookList', - { - title: 'Books', - nav, - books - }) + (async function () { + const result = await request.query('select * from books'); + res.render('bookList', + { + title: 'Books', + nav, + books: result.recordset + }) + }()); }); bookRouter.route('/:id') From 59194e7b127b5d22d96212595b0050f0b4e942b4 Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Tue, 15 Oct 2019 10:00:42 +0530 Subject: [PATCH 4/6] DB Scripts --- DB/MSSQL/DbScript.sql | Bin 0 -> 10454 bytes 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 DB/MSSQL/DbScript.sql diff --git a/DB/MSSQL/DbScript.sql b/DB/MSSQL/DbScript.sql new file mode 100644 index 0000000000000000000000000000000000000000..9dad32466f3b82462bc1cd9d8be646f86ab4eb2c GIT binary patch literal 10454 zcmdU#Yfsx&7{|}sP1<+xJLptVE~RTXRa!^_jUY)ILn&>M6)t6TkW_?q>zCj5|DDGt zw$l~@acC+lj(vQ5p8NIqzkhdJ?CLIZZ`?q~nLBj{Zr}anesmkI?f)ORpL0i~(F6D1 z9lJw!>3-Flk=u4%jh$(Xb&ho1*BT4D=Bn@h)XMvQCbMI`c_BC-^nT=OdiPRqveJ@U zbPsf1)p=RhOYVtApXk}DKd)#sauwIqIgir=k7vzwgxhmH+t!>lx0TZMHGFPH5hTjZ+J>z3z1L;p>^&t2r6>euqm>fQpd&6bzSb3tinW<~#d8bLqi z{cNlU$oB4B&O+zheT}c{JuIiCcZ)NtvctEsuM1i9iOyYj?&qI+%OB`jQ&x>__oXp? z_s-jQ>|SU+tRCz6=+DfIb?{`#J(Q=&?(s$)_tKy9^WF}5^)`p|f^awzq&YzV`&ih& z9@6*Ndyr+fDQ{rU;mE6zSv#V2%X6zCuGR%7&xZ|R({{Uh@=R;MwMX(st8PXAw(h-@ z_gk8`q30%paX6lcbMW$e!v)uqvO ztvwpUiNE>SH&Db)WGe=Tm%Jq@VM!896F& z<8~6L$Of_9ME}OUMDnYPD;0l_w`=PTPUmA?S}d72Y^tWUFJ`?!uCxv z#OKL1xGJmYXhkHF=Ut+amLI{RHNEW0q_zlm)IuZaI8s~_<@n1o=!BlC=VabD?1{3v zVARBeY`jcL@k8W4=uA~G@Y-YwNO&5S@ItG%r77eeiD1H8vOJ;-v=Y(E^MK@_L6!|4 z-x9%s)(ss%E7y+@jT>4s)Mbfy2uC+l$|lzo;)HpU>+LWR92xO{Y7{2OL@A> zq>hN%(GT0SOmCEvbb(^2GAg zGNGDpXs0z8)nmwL%kb!tWJ89B6(n9y&9_Z1Co}`!m86*CIl;av&BWVnN-y#0bS)-_ zR+B`~2r?tGit;nOPy=Dfri0D2hBsfrsO8NS;SuzaYw8T@YpYm--QO#PZnBz==QFlG zsw+ehtS@vJ9;sHcI?H+(#OrdnZ=Q8ekg&{}Bnvn4wVP32j61av=vnPFaGz9fE$By; zMBS9AThUd|;z-rbp(-J|X=fUxqt^HRj}twiqT-IH^fOzkjIutNRrZV_Gpm_G-FUm& zg**X^p*BS`@Bx9_Ch5FxXT87-r($>aYAj;u5-|%dhdvZ~xsmc4X+v37OXo8etDwo= z%m#x#w%l(wu)v$+fal-vP~j5rJ3jv)Qe~ArHe%YEMd*MbRoZlLT=x-9eS`W?CH?pCcxgTJ;5SKQw)TE4VnMOI1d zisX^$n`0ec+FRSz6YISfyA4>DMHOuRX|C6qkAK&5>hAU~ui{$prl0pg(Q~{-o~kf= zyq-bv-J*QP+bNy*(0>q*Pn8YPALgp3e244Nh)*Z{zJBDJR4?>yLs$3ya3y-nm&&J( z)AcOZI&^>doNHg1?y}!!u~n=moW0#u7yD9k$-E4oo4rrw*3-K;fsO>g#`2vvX$^+< z5xt99RglmqZ>WyxbmP(BOR+;=+$}daK;<|7d-Fp%v5)mee9Aa&BSr4nSe_$L`O@b~ zv!&NoqM@i6)(Ylnm5}% z13MhEKGgfJgLnU+gRemjH81P~K}Vo|JSpdQec@8}<%hwX6}~(i(zPYIQT6pi zyK$vCBJ~6Dh&{AO{jaX%f2@Puxxkqx+@PHGUc<@7=GrF(#XPcUTr1|7)MGKh~dLIwzhO~xb$dP<;cf6t{5J|xyd8bzWGvLVMDVPJ!Lx)oQ_PXB?mQL0N5sbL YTa8GW6W>qK_1o1o)dBr4poY%c|9S1enE(I) literal 0 HcmV?d00001 From 2fdddd084bda2a7379981261e51450ee37cebb1a Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Tue, 15 Oct 2019 22:47:55 +0530 Subject: [PATCH 5/6] Sql version 2 --- library/src/routes/bookRoutes.js | 70 +++++++------------------------- 1 file changed, 14 insertions(+), 56 deletions(-) diff --git a/library/src/routes/bookRoutes.js b/library/src/routes/bookRoutes.js index c6cea25..0b4b71f 100644 --- a/library/src/routes/bookRoutes.js +++ b/library/src/routes/bookRoutes.js @@ -5,54 +5,6 @@ const debug = require('debug')('app'); function router(nav) { - const books = [{ - id: 1, - title: "Ansible 3.0", - genre: "Automation", - author: "Mohamed Safwan" - }, - { - id: 2, - title: "Concourse", - genre: "Dev Ops", - author: "Aisha Sana" - }, - { - id: 3, - title: "Mastering Node Applications", - genre: "Server Scripting", - author: "Mohamed Safwan" - }, - { - id: 4, - title: "Learning Me", - genre: "Biography", - author: "Abdul Waheed" - }, - { - id: 5, - title: "Worst Human Behaviours", - genre: "Fiction", - author: "Abdul Waheed" - }, - { - id: 6, - title: "Space Shuttle", - genre: "Astro Science", - author: "Mohamed Safwan" - }, - { - id: 7, - title: "Drawing Basics", - genre: "Kids Learning", - author: "Aisha Sana" - }, - { - id: 8, - title: "Miserable", - genre: "Fiction", - author: "Abdul Waheed" - }] const request = new sql.Request(); //Promise example @@ -73,12 +25,12 @@ function router(nav) { bookRouter.route('/') .get((req, res) => { (async function () { - const result = await request.query('select * from books'); + const {recordset} = await request.query('select * from books'); res.render('bookList', { title: 'Books', nav, - books: result.recordset + books: recordset }) }()); }); @@ -86,12 +38,18 @@ function router(nav) { bookRouter.route('/:id') .get((req, res) => { const { id } = req.params; - res.render('bookInfo', - { - title: books[id].genre, - nav, - book: books[id] - }) + (async function () { + + const {recordset} = await request.input('id',sql.Int,Number(id)+1) + .query('select * from books where id = @id'); + + res.render('bookInfo', + { + title: recordset[0].genre, + nav, + book: recordset[0] + }) + }()); }); return bookRouter; } From dd2f5ddb56314ef4aecf1e68495fdb6c0b90c5ef Mon Sep 17 00:00:00 2001 From: Abdul Waheed Date: Tue, 15 Oct 2019 23:10:25 +0530 Subject: [PATCH 6/6] Middle ware changes --- library/src/routes/bookRoutes.js | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/library/src/routes/bookRoutes.js b/library/src/routes/bookRoutes.js index 0b4b71f..46c7e9a 100644 --- a/library/src/routes/bookRoutes.js +++ b/library/src/routes/bookRoutes.js @@ -25,7 +25,7 @@ function router(nav) { bookRouter.route('/') .get((req, res) => { (async function () { - const {recordset} = await request.query('select * from books'); + const { recordset } = await request.query('select * from books'); res.render('bookList', { title: 'Books', @@ -36,20 +36,24 @@ function router(nav) { }); bookRouter.route('/:id') - .get((req, res) => { + .all((req, res, next) => { const { id } = req.params; (async function () { - - const {recordset} = await request.input('id',sql.Int,Number(id)+1) - .query('select * from books where id = @id'); - - res.render('bookInfo', - { - title: recordset[0].genre, - nav, - book: recordset[0] - }) + const { recordset } = await request.input('id', sql.Int, Number(id) + 1) + .query('select * from books where id = @id'); + //req.book = recordset[0]; + //array destructuring + [req.book] = recordset; + next(); }()); + }) + .get((req, res) => { + res.render('bookInfo', + { + title: req.book.genre, + nav, + book: req.book + }) }); return bookRouter; }