From 1b3b676d621422762f938b97d69b73fd4907ccea Mon Sep 17 00:00:00 2001 From: Mohammed Yehia Date: Wed, 2 May 2018 19:38:15 +0300 Subject: [PATCH] feat: adding insert and select queries, reading the user login input and check in the data base. chore:adding body-parser module. fix:fix some bugs in hbs and app file --- package-lock.json | 152 ++++++++++++++++++------------------ package.json | 1 + src/app.js | 8 +- src/controllers/home.js | 15 +++- src/controllers/index.js | 2 + src/controllers/login.js | 21 +++++ src/controllers/signup.js | 1 + src/model/queries/insert.js | 32 ++++++++ src/model/queries/select.js | 31 ++++++++ src/views/home.hbs | 7 +- src/views/login.hbs | 6 +- src/views/partials/head.hbs | 2 +- src/views/signup.hbs | 9 ++- 13 files changed, 197 insertions(+), 90 deletions(-) create mode 100644 src/model/queries/insert.js create mode 100644 src/model/queries/select.js diff --git a/package-lock.json b/package-lock.json index 694f2fa..46d81cf 100644 --- a/package-lock.json +++ b/package-lock.json @@ -974,8 +974,8 @@ "dev": true, "optional": true, "requires": { - "delegates": "1.0.0", - "readable-stream": "2.3.6" + "delegates": "^1.0.0", + "readable-stream": "^2.0.6" } }, "balanced-match": { @@ -988,7 +988,7 @@ "bundled": true, "dev": true, "requires": { - "balanced-match": "1.0.0", + "balanced-match": "^1.0.0", "concat-map": "0.0.1" } }, @@ -1052,7 +1052,7 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "fs.realpath": { @@ -1067,14 +1067,14 @@ "dev": true, "optional": true, "requires": { - "aproba": "1.2.0", - "console-control-strings": "1.1.0", - "has-unicode": "2.0.1", - "object-assign": "4.1.1", - "signal-exit": "3.0.2", - "string-width": "1.0.2", - "strip-ansi": "3.0.1", - "wide-align": "1.1.2" + "aproba": "^1.0.3", + "console-control-strings": "^1.0.0", + "has-unicode": "^2.0.0", + "object-assign": "^4.1.0", + "signal-exit": "^3.0.0", + "string-width": "^1.0.1", + "strip-ansi": "^3.0.1", + "wide-align": "^1.1.0" } }, "glob": { @@ -1083,12 +1083,12 @@ "dev": true, "optional": true, "requires": { - "fs.realpath": "1.0.0", - "inflight": "1.0.6", - "inherits": "2.0.3", - "minimatch": "3.0.4", - "once": "1.4.0", - "path-is-absolute": "1.0.1" + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" } }, "has-unicode": { @@ -1103,7 +1103,7 @@ "dev": true, "optional": true, "requires": { - "safer-buffer": "2.1.2" + "safer-buffer": "^2.1.0" } }, "ignore-walk": { @@ -1112,7 +1112,7 @@ "dev": true, "optional": true, "requires": { - "minimatch": "3.0.4" + "minimatch": "^3.0.4" } }, "inflight": { @@ -1121,8 +1121,8 @@ "dev": true, "optional": true, "requires": { - "once": "1.4.0", - "wrappy": "1.0.2" + "once": "^1.3.0", + "wrappy": "1" } }, "inherits": { @@ -1141,7 +1141,7 @@ "bundled": true, "dev": true, "requires": { - "number-is-nan": "1.0.1" + "number-is-nan": "^1.0.0" } }, "isarray": { @@ -1155,7 +1155,7 @@ "bundled": true, "dev": true, "requires": { - "brace-expansion": "1.1.11" + "brace-expansion": "^1.1.7" } }, "minimist": { @@ -1168,8 +1168,8 @@ "bundled": true, "dev": true, "requires": { - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "safe-buffer": "^5.1.1", + "yallist": "^3.0.0" } }, "minizlib": { @@ -1178,7 +1178,7 @@ "dev": true, "optional": true, "requires": { - "minipass": "2.2.4" + "minipass": "^2.2.1" } }, "mkdirp": { @@ -1201,9 +1201,9 @@ "dev": true, "optional": true, "requires": { - "debug": "2.6.9", - "iconv-lite": "0.4.21", - "sax": "1.2.4" + "debug": "^2.1.2", + "iconv-lite": "^0.4.4", + "sax": "^1.2.4" } }, "node-pre-gyp": { @@ -1212,16 +1212,16 @@ "dev": true, "optional": true, "requires": { - "detect-libc": "1.0.3", - "mkdirp": "0.5.1", - "needle": "2.2.0", - "nopt": "4.0.1", - "npm-packlist": "1.1.10", - "npmlog": "4.1.2", - "rc": "1.2.6", - "rimraf": "2.6.2", - "semver": "5.5.0", - "tar": "4.4.1" + "detect-libc": "^1.0.2", + "mkdirp": "^0.5.1", + "needle": "^2.2.0", + "nopt": "^4.0.1", + "npm-packlist": "^1.1.6", + "npmlog": "^4.0.2", + "rc": "^1.1.7", + "rimraf": "^2.6.1", + "semver": "^5.3.0", + "tar": "^4" } }, "nopt": { @@ -1230,8 +1230,8 @@ "dev": true, "optional": true, "requires": { - "abbrev": "1.1.1", - "osenv": "0.1.5" + "abbrev": "1", + "osenv": "^0.1.4" } }, "npm-bundled": { @@ -1246,8 +1246,8 @@ "dev": true, "optional": true, "requires": { - "ignore-walk": "3.0.1", - "npm-bundled": "1.0.3" + "ignore-walk": "^3.0.1", + "npm-bundled": "^1.0.1" } }, "npmlog": { @@ -1256,10 +1256,10 @@ "dev": true, "optional": true, "requires": { - "are-we-there-yet": "1.1.4", - "console-control-strings": "1.1.0", - "gauge": "2.7.4", - "set-blocking": "2.0.0" + "are-we-there-yet": "~1.1.2", + "console-control-strings": "~1.1.0", + "gauge": "~2.7.3", + "set-blocking": "~2.0.0" } }, "number-is-nan": { @@ -1278,7 +1278,7 @@ "bundled": true, "dev": true, "requires": { - "wrappy": "1.0.2" + "wrappy": "1" } }, "os-homedir": { @@ -1299,8 +1299,8 @@ "dev": true, "optional": true, "requires": { - "os-homedir": "1.0.2", - "os-tmpdir": "1.0.2" + "os-homedir": "^1.0.0", + "os-tmpdir": "^1.0.0" } }, "path-is-absolute": { @@ -1321,10 +1321,10 @@ "dev": true, "optional": true, "requires": { - "deep-extend": "0.4.2", - "ini": "1.3.5", - "minimist": "1.2.0", - "strip-json-comments": "2.0.1" + "deep-extend": "~0.4.0", + "ini": "~1.3.0", + "minimist": "^1.2.0", + "strip-json-comments": "~2.0.1" }, "dependencies": { "minimist": { @@ -1341,13 +1341,13 @@ "dev": true, "optional": true, "requires": { - "core-util-is": "1.0.2", - "inherits": "2.0.3", - "isarray": "1.0.0", - "process-nextick-args": "2.0.0", - "safe-buffer": "5.1.1", - "string_decoder": "1.1.1", - "util-deprecate": "1.0.2" + "core-util-is": "~1.0.0", + "inherits": "~2.0.3", + "isarray": "~1.0.0", + "process-nextick-args": "~2.0.0", + "safe-buffer": "~5.1.1", + "string_decoder": "~1.1.1", + "util-deprecate": "~1.0.1" } }, "rimraf": { @@ -1356,7 +1356,7 @@ "dev": true, "optional": true, "requires": { - "glob": "7.1.2" + "glob": "^7.0.5" } }, "safe-buffer": { @@ -1399,9 +1399,9 @@ "bundled": true, "dev": true, "requires": { - "code-point-at": "1.1.0", - "is-fullwidth-code-point": "1.0.0", - "strip-ansi": "3.0.1" + "code-point-at": "^1.0.0", + "is-fullwidth-code-point": "^1.0.0", + "strip-ansi": "^3.0.0" } }, "string_decoder": { @@ -1410,7 +1410,7 @@ "dev": true, "optional": true, "requires": { - "safe-buffer": "5.1.1" + "safe-buffer": "~5.1.0" } }, "strip-ansi": { @@ -1418,7 +1418,7 @@ "bundled": true, "dev": true, "requires": { - "ansi-regex": "2.1.1" + "ansi-regex": "^2.0.0" } }, "strip-json-comments": { @@ -1433,13 +1433,13 @@ "dev": true, "optional": true, "requires": { - "chownr": "1.0.1", - "fs-minipass": "1.2.5", - "minipass": "2.2.4", - "minizlib": "1.1.0", - "mkdirp": "0.5.1", - "safe-buffer": "5.1.1", - "yallist": "3.0.2" + "chownr": "^1.0.1", + "fs-minipass": "^1.2.5", + "minipass": "^2.2.4", + "minizlib": "^1.1.0", + "mkdirp": "^0.5.0", + "safe-buffer": "^5.1.1", + "yallist": "^3.0.2" } }, "util-deprecate": { @@ -1454,7 +1454,7 @@ "dev": true, "optional": true, "requires": { - "string-width": "1.0.2" + "string-width": "^1.0.2" } }, "wrappy": { diff --git a/package.json b/package.json index 5046daf..d52cbe4 100644 --- a/package.json +++ b/package.json @@ -19,6 +19,7 @@ }, "homepage": "https://github.com/FACG4/chat#readme", "dependencies": { + "body-parser": "^1.18.2", "env2": "^2.2.0", "express": "^4.16.3", "express-handlebars": "^3.0.0", diff --git a/src/app.js b/src/app.js index 3cffb34..ab5e587 100644 --- a/src/app.js +++ b/src/app.js @@ -1,16 +1,22 @@ const express = require("express"); const favicon = require("serve-favicon"); const exphbs = require("express-handlebars"); +const bodyParser = require('body-parser'); const app = express(); const controllers = require("./controllers/index"); const helpers = require("./views/helpers/index"); const path = require("path"); + +app.use(bodyParser.json()); +app.use(bodyParser.urlencoded({ extended: false })); + app.set("views", path.join(__dirname, "views")); app.set("view engine", "hbs"); app.set("port", process.env.PORT || 3000); app.use(favicon(path.join(__dirname, "..", "public", "favicon.ico"))); -app.use(express.static(path.join(__dirname, "..", "public"))); +app.use(express.static("public")); +// app.use('/user/Israa',express.static(path.join(__dirname,"..", "public"))); app.use(controllers); app.engine( diff --git a/src/controllers/home.js b/src/controllers/home.js index a5ae02d..1a46b2a 100644 --- a/src/controllers/home.js +++ b/src/controllers/home.js @@ -1,5 +1,14 @@ +const select = require('../model/queries/select'); + + + + exports.get = (req, res) => { - res.render("home", { - title: "Chat" - }); + select.selectUserData({username:'Israa', password:123}, (err,result)=>{ + if(err) console.log(err); + res.render("home", { + title: "Chat", + data: result.rows[0] + }); + }) }; diff --git a/src/controllers/index.js b/src/controllers/index.js index 7d7d22f..4631144 100644 --- a/src/controllers/index.js +++ b/src/controllers/index.js @@ -5,6 +5,8 @@ const signup = require("./signup"); const login = require("./login"); router.get("/", home.get); router.get("/login", login.get); +router.post("/login", login.post); router.get("/signup", signup.get); +router.get("/user/:name", home.get); module.exports = router; diff --git a/src/controllers/login.js b/src/controllers/login.js index 74ae1d1..a46fe74 100644 --- a/src/controllers/login.js +++ b/src/controllers/login.js @@ -1,5 +1,26 @@ +const select = require('../model/queries/select'); + exports.get = (req, res) => { res.render("login", { title: "Login Page" }); }; + + +exports.post = (req, res) => { + if(req.body.name !== '' && req.body.password !== ''){ + select.selectUserData(req.body, (err,result)=>{ + if(err){ + console.log(err); + }else{ + if(result.rowCount>0) + res.redirect('/'); + else + res.send('

Wrong name or password

') + } + }) + + }else{ + res.send('

Name and Password are required

') + } +}; diff --git a/src/controllers/signup.js b/src/controllers/signup.js index b31af3a..c5e3006 100644 --- a/src/controllers/signup.js +++ b/src/controllers/signup.js @@ -1,3 +1,4 @@ + exports.get = (req, res) => { res.render("signup", { title: "Sign Up" diff --git a/src/model/queries/insert.js b/src/model/queries/insert.js new file mode 100644 index 0000000..88c35bb --- /dev/null +++ b/src/model/queries/insert.js @@ -0,0 +1,32 @@ +const db = require('./../db_connect'); + +const insertUser = (data,cb)=>{ + const {username,email,password} = data; + let sql ={ + text:'INSERT INTO users (username,email,password) VALUES ($1,$2,$3)', + values:[username, email, password] + }; + + db.query(sql, (err,result)=>{ + if(err) return cb(err); + return cb(null, result); + }) +}; + +const insertMsg = (data,cb)=>{ + const {user_id,content} = data; + let sql ={ + text:'INSERT INTO messages (user_id, content, date) VALUES ($1,$2, $3)', + values:[user_id, content, 'DEFAULT'] + }; + + db.query(sql, (err,result)=>{ + if(err) return cb(err); + return cb(null, result); + }) +}; + +module.exports = { + insertUser, + insertMsg +} diff --git a/src/model/queries/select.js b/src/model/queries/select.js new file mode 100644 index 0000000..f74b5da --- /dev/null +++ b/src/model/queries/select.js @@ -0,0 +1,31 @@ +const db = require('./../db_connect'); + +const selectUserData = (data,cb)=>{ + const {username,password} = data; + let sql ={ + text:'SELECT id, username FROM users WHERE username = $1 AND password = $2', + values:[username,password] + }; + + db.query(sql, (err,result)=>{ + if(err) return cb(err); + return cb(null, result); + }) +}; + + +const selectMsg = (cb)=>{ + let sql ={ + text:'SELECT user_id, content, date FROM messages' + }; + + db.query(sql, (err,result)=>{ + if(err) return cb(err); + return cb(null, result); + }) +}; + +module.exports = { + selectUserData, + selectMsg +} diff --git a/src/views/home.hbs b/src/views/home.hbs index 6f2d75d..f17f88e 100644 --- a/src/views/home.hbs +++ b/src/views/home.hbs @@ -11,8 +11,11 @@ Ben Stiller
- Hey, nobody's here today. I'm at office alone. - Hey, nobody's here today. I'm at office alone. + +
diff --git a/src/views/login.hbs b/src/views/login.hbs index d8e5ae1..c2f4269 100644 --- a/src/views/login.hbs +++ b/src/views/login.hbs @@ -1,7 +1,7 @@
-
- - + + +
diff --git a/src/views/partials/head.hbs b/src/views/partials/head.hbs index c8f3932..26eadf4 100644 --- a/src/views/partials/head.hbs +++ b/src/views/partials/head.hbs @@ -2,4 +2,4 @@ {{title}} - + diff --git a/src/views/signup.hbs b/src/views/signup.hbs index 80b5fd0..a7c2023 100644 --- a/src/views/signup.hbs +++ b/src/views/signup.hbs @@ -1,8 +1,9 @@
-
- - - + + + + +