From cdf2a3461f65dceae6a529a28b06a7a461bd781d Mon Sep 17 00:00:00 2001 From: Sandesh <sguru003@campus.goldsmiths.ac.uk> Date: Mon, 10 Apr 2023 18:48:22 +0100 Subject: [PATCH] listing products working --- create_db.sql | 2 +- routes/main.js | 42 +++++++++++++++++++-------------------- views/about.ejs | 24 +++++++++++++++++----- views/itemlisted.ejs | 47 +++++++++++++++++++++++++++++++++++++++----- views/login.ejs | 33 ++++++++++++++++++++++--------- views/stock.ejs | 25 +++++++++++++++++++++++ 6 files changed, 131 insertions(+), 42 deletions(-) create mode 100644 views/stock.ejs diff --git a/create_db.sql b/create_db.sql index 2755e79..4433923 100644 --- a/create_db.sql +++ b/create_db.sql @@ -3,7 +3,7 @@ CREATE DATABASE store; USE store; CREATE TABLE items (product_id INT AUTO_INCREMENT primary key,title VARCHAR(50),name VARCHAR(50), - price DECIMAL(5, 2), info TEXT , condi VARCHAR(50),img LONGTEXT); + price DECIMAL(5, 2), info TEXT , condi VARCHAR(50),img LONGTEXT, username VARCHAR(50) ); CREATE TABLE userdata (id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(50), firstname VARCHAR(50), lastname VARCHAR(50),email VARCHAR(255) NOT NULL UNIQUE, diff --git a/routes/main.js b/routes/main.js index c280564..2dc3a31 100644 --- a/routes/main.js +++ b/routes/main.js @@ -9,6 +9,7 @@ const upload = multer({storage: multer.memoryStorage()}); //Module needed for the validator const { check, validationResult } = require('express-validator'); + module.exports = function(app, shopData) { //redirects users back to login page if they are not logged in const redirectLogin = (req, res, next) => { @@ -34,7 +35,7 @@ module.exports = function(app, shopData) { }); - app.get('/itemlist',function(req,res){ + app.get('/itemlist',redirectLogin,function(req,res){ res.render('itemlist.ejs', shopData) }); @@ -47,7 +48,7 @@ module.exports = function(app, shopData) { if (!errors.isEmpty()) { res.redirect('./search'); } else { - let sqlquery = "SELECT * FROM item WHERE name LIKE '%" + req.query.keyword + "%'"; // query database to get all the books + let sqlquery = "SELECT * FROM items WHERE name LIKE '%" + req.query.keyword + "%'"; // query database to get all the books // execute sql query db.query(sqlquery, (err, result) => { if (err) { @@ -55,11 +56,10 @@ module.exports = function(app, shopData) { } let newData = Object.assign({}, shopData, {availableItem:result}); console.log(newData) - res.render("list.ejs", newData) + res.render("itemlisted.ejs", newData) }); } - }); app.post('/photoDB',upload.single("ProductImage"), (req,res) => { @@ -69,20 +69,21 @@ module.exports = function(app, shopData) { price = req.body.price; info = req.body.info; condition = req.body.condition; + username = req.session.userId; image =req.file.buffer.toString("base64"); - let sqlquery ="INSERT INTO items (title,name,price,info,condi,img) VALUES(?,?,?,?,?,?)"; + let sqlquery ="INSERT INTO items (title,name,price,info,condi,img,username) VALUES(?,?,?,?,?,?,?)"; let newListing = [req.sanitize(title),req.sanitize(product), req.sanitize(price),req.sanitize(info), - req.sanitize(condition),req.sanitize(image)] + req.sanitize(condition),req.sanitize(image),username] db.query(sqlquery, newListing,(err, result) => { if (err){ return console.error(err.message); } else{ - let newData = Object.assign({}, shopData, {listingAdded: newListing}); + let newData = Object.assign({}, shopData, {listingAdded: result}); res.render("itemlisted.ejs", newData) } }); @@ -91,21 +92,19 @@ module.exports = function(app, shopData) { }); + app.get('/itemlisted',function(req,res){ + let sqlquery = "SELECT * FROM items"; // query database to get all the books + // execute sql query + db.query(sqlquery, (err, result) => { + if (err) { + res.redirect('./'); + } + let newData = Object.assign({}, shopData, {listingAdded: result}); + console.log(newData) + res.render("itemlisted.ejs",newData) + }); - - - - - - - - - - - - - - + }); @@ -226,5 +225,4 @@ module.exports = function(app, shopData) { }) - } diff --git a/views/about.ejs b/views/about.ejs index d585737..abd895c 100644 --- a/views/about.ejs +++ b/views/about.ejs @@ -1,13 +1,27 @@ <!doctype html> <html> <head> - <title>About <%=shopName%></title> + <title><%=shopName%></title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <body> - <h1>About <%=shopName%></h1> - <p>We sell all sorts of and nothing else!</p> - <!-- Redirects to home page --> - <p><a href="/">home page</a></p> + + <section id = "header"> + <a href="/."> <img src = "/public/logo.png" class ="logo" alt = ""> </a> + <div> + <ul id="navbar"> + <li><a href="about">About </a></li> + <li><a href="search">Search </a></li> + <li><a href="itemlist">List </a></li> + <li><a href="register">Register </a></li> + <li><a href="login">Login</a></li> + <li><a href="logout">Logout</a></li> + </ul> + </div> + </section> + + </body> + + </html> \ No newline at end of file diff --git a/views/itemlisted.ejs b/views/itemlisted.ejs index d585737..8e7e3a2 100644 --- a/views/itemlisted.ejs +++ b/views/itemlisted.ejs @@ -1,13 +1,50 @@ <!doctype html> <html> <head> - <title>About <%=shopName%></title> + <title><%=shopName%></title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <body> - <h1>About <%=shopName%></h1> - <p>We sell all sorts of and nothing else!</p> - <!-- Redirects to home page --> - <p><a href="/">home page</a></p> + + <section id = "header"> + <a href="/."> <img src = "/public/logo.png" class ="logo" alt = ""> </a> + <div> + <ul id="navbar"> + <li><a href="about">About </a></li> + <li><a href="search">Search </a></li> + <li><a href="itemlist">List </a></li> + <li><a href="logout">Logout</a></li> + </ul> + </div> + </section> + + <section id = "items"> + <%listingAdded.forEach(function(list){ %> + <ul> + <li><%=list.title%></li> + <li><%=list.name%></li> + <li><%=list.price%></li> + <li><%=list.condi%></li> + <li><%=list.username%></li> + <li> <img alt=" Embedded Image" src = "data:image/png;base64,<%=list.img%>"> </li> + </ul> + <% }) %> + + + </section> + + + + + + + + + + + + </body> + + </html> \ No newline at end of file diff --git a/views/login.ejs b/views/login.ejs index 2c0bb83..d4f43f4 100644 --- a/views/login.ejs +++ b/views/login.ejs @@ -1,17 +1,32 @@ <!doctype html> <html> <head> - <title>Register for <%=shopName%></title> + <title><%=shopName%></title> <link rel="stylesheet" type="text/css" href="main.css" /> </head> <body> - <h2>Login for <%=shopName%></h2> - <form method="POST" action="/loggedin"> - <p>Username: <input id="username" type="username" name="username" value="" /></p> - <p>Password: <input id="password" type="password" name="password" value="" /></p> - <input type="submit" value="login" /> - </form> - <!-- Redirects to home page --> - <p><a href="/">home page</a></p> + + <section id = "header"> + <a href="/."> <img src = "/public/logo.png" class ="logo" alt = ""> </a> + <div> + <ul id="navbar"> + <li><a href="about">About </a></li> + <li><a href="search">Search </a></li> + <li><a href="itemlist">List </a></li> + <li><a href="register">Register </a></li> + </ul> + </div> + </section> + + <h2>Login for <%=shopName%></h2> + <form method="POST" action="/loggedin"> + <p>Username: <input id="username" type="username" name="username" value="" /></p> + <p>Password: <input id="password" type="password" name="password" value="" /></p> + <input type="submit" value="login" /> + </form> + + </body> + + </html> \ No newline at end of file diff --git a/views/stock.ejs b/views/stock.ejs new file mode 100644 index 0000000..52c0e6b --- /dev/null +++ b/views/stock.ejs @@ -0,0 +1,25 @@ +<!doctype html> +<html> + <head> + <title><%=shopName%></title> + <link rel="stylesheet" type="text/css" href="main.css" /> + </head> + <body> + + <section id = "header"> + <a href="/."> <img src = "/public/logo.png" class ="logo" alt = ""> </a> + <div> + <ul id="navbar"> + <li><a href="about">About </a></li> + <li><a href="search">Search </a></li> + <li><a href="itemlist">List </a></li> + <li><a href="logout">Logout</a></li> + </ul> + </div> + </section> + + + </body> + + +</html> \ No newline at end of file -- GitLab