diff --git a/index.js b/index.js index 8660968..f4a4b84 100644 --- a/index.js +++ b/index.js @@ -23,6 +23,32 @@ app.use(bodyParser.json()); app.use(express.static(__dirname + "/static")); // Authentication Handlers + +// Check if user is authenticated and NO redirect +function authNoRedirectHandler(req, res, next){ + const authcookie = req.cookies.authcookie; // Get authcookie from cookie + + jwt.verify(authcookie, SECRET_KEY, (err, data) =>{ // Verify authcookie + if(err){ // If authcookie is invalid + console.log(err); + next(); + } else if(data.user){ // If authcookie is valid + req.user = data.user; // Set user to data.user + mysql_handler.con.query(`SELECT * FROM users WHERE id = "${req.user}"`, (err, result) => { // Get user from database + if(err) console.log(err); + let user = JSON.parse(JSON.stringify(result))[0]; // Parse user from database + // Set user to req.user + req.isAdmin = user.isAdmin; + req.username = user.username; + req.firstname = user.firstname; + req.lastname = user.lastname; + next(); // Continue to next handler + }); + } + }); +} + + // Check if user is authenticated and redirect to login if not function authenticatedHandler(req, res, next){ const authcookie = req.cookies.authcookie; // Get authcookie from cookie @@ -62,32 +88,70 @@ function notAuthenticatedHandler(req, res, next){ } // Homepage -app.get("/", authenticatedHandler, (req, res) => { - let dict = { - title: "Hallo", - isAdmin: req.isAdmin - } +app.get("/", authNoRedirectHandler, (req, res) => { + mysql_handler.con.query("SELECT * FROM products", function(err, result){ + if(err) throw err; - res.render('index', dict) + let dict = { + title: "Startseite", + user: req.user, + products: JSON.parse(JSON.stringify(result)) + } + res.render('index', dict) + }); }); // Product Page app.get("/product/:productId", (req, res) => { let productId = req.params.productId; - console.log(productId); - mysql_handler.con.query(`SELECT * FROM products WHERE id=${productId}` , function(err, result){ + mysql_handler.con.query(`SELECT s.name AS sellerName, p.name AS productName, p.description AS productDescription, p.id AS id, price,quantity, delivery_time, p.categoryId + FROM products AS p LEFT JOIN sellers AS s ON p.sellerId= s.id WHERE p.id=${productId}` , function(err, result){ if(err) throw err; let product = JSON.parse(JSON.stringify(result))[0]; - let dict = { - title: "product", - product: product - } - res.render('product', dict) + + mysql_handler.con.query(`SELECT title, content ,rating, u.username AS name FROM reviews AS r LEFT JOIN users AS u ON r.userId = u.id WHERE productId=${productId}`,function(err,result){ + if(err) throw err; + let reviews = JSON.parse(JSON.stringify(result)); + console.log(product) + mysql_handler.con.query(`SELECT * FROM categories WHERE id='${product.categoryId}'`,function(err,result){ + if(err) throw err; + let category = JSON.parse(JSON.stringify(result))[0]; + + let dict = { + title: product.productName, + product: product, + shippingDays: 3, + stockAmount: 50, + productDescription: "ez", + loggedIn: true, + reviews: reviews, + category: category, + } + res.render('product', dict) + }); + }); + }); }); +// Reviews +app.post("/review/create/:productId", authenticatedHandler,(req, res) => { + let productId = req.params.productId; + let rating = req.body.rating; + let title = req.body.title; + let content = req.body.content; + + + mysql_handler.con.query(`INSERT INTO reviews(title, content, rating, userId, productId) + VALUES('${title}', '${content}', '${rating}', (SELECT id FROM users WHERE id = ${req.user}), (SELECT id FROM products WHERE id = ${productId}))`, (err, result) => { + if(err) throw err; + res.redirect("/product/" + productId); + }); + +}); + // Search Page app.get("/search", (req, res) => { var products = [ @@ -304,4 +368,4 @@ app.post("/auth/login", notAuthenticatedHandler, (req, res) =>{ app.listen(port, () =>{ // Start server console.log("Listining to " + port) -}) \ No newline at end of file +}); \ No newline at end of file diff --git a/static/auth.css b/static/css/auth.css similarity index 100% rename from static/auth.css rename to static/css/auth.css diff --git a/static/css/header.css b/static/css/header.css new file mode 100644 index 0000000..8b1376f --- /dev/null +++ b/static/css/header.css @@ -0,0 +1,92 @@ +/* UTIL */ + .vertical-center { + float: left; + top: 50%; + position: relative; + transform: translateY(-50%); +} + +body { + margin: 0; +} + +/* BACKGROUND */ +.headerDIV > div { + height: 100%; + text-align: center; + display: inline-block; + vertical-align: top; +} + +/* LOGO PART */ +.headerDIVLogo { + width: 20%; +} +.headerDIVLogo > img { + float: left; + height: 80%; + position: relative; + top: 10%; +} + +/* SEARCH PART */ +.headerDIVSearch { + width: 60%; +} +.headerDIVSearch > input { + left: 10%; + width: calc(80% - 60px); + border-radius: 5px; + border-top-right-radius: 0px; + border-bottom-right-radius: 0px; + height: 20px; + padding: 3px 10px; +} +.headerDIVSearch > button { + width: 40px; + left: 10%; + border-radius: 5px; + border-top-left-radius: 0px; + border-bottom-left-radius: 0px; + height: 30px; +} + +/* BUTTON PART */ +.headerDIVButton { + width: 20%; +} +.headerDIVButton > button { + height: 30px; + float: right; + margin-right: 10px; + height: 35px; + border-radius: 5px; + border: 3px solid rgb(6, 70, 107); + background-color: rgb(0, 99, 156); + transition: 0.1s; + transition-timing-function: linear; + cursor: pointer; + color:white; + font-weight: bold; +} + +#headerBtnLogout{ + background-color: rgb(172, 38, 38); + border-color: rgb(145, 35, 35); +} + +#headerBtnLogout:hover{ + background-color: rgb(206, 50, 50); +} + + +.headerDIVButton > button:hover { + background-color: rgb(23, 114, 167); +} + +.headerDIV { + width: 100%; + height: 65px; + background-color: #414854; + margin: 0; +} \ No newline at end of file diff --git a/static/order.css b/static/css/order.css similarity index 70% rename from static/order.css rename to static/css/order.css index cd22924..cb0959e 100644 --- a/static/order.css +++ b/static/css/order.css @@ -3,8 +3,15 @@ margin-left: auto; margin-right: auto; width: 500px; + margin-top: 60px; + margin-bottom: 60px; } +#order-info p{ + height: 30px; +} + + form input[type="submit"]{ color: rgb(255, 255, 255); font-weight: bold; @@ -27,4 +34,13 @@ form input[type="submit"]{ h3{ text-align: center; +} + +hr{ + width: 100%; + border: 0px; + background-color:rgba(51, 51, 51, 0.337); + height: 2px; + margin-top: 20px; + margin-bottom: 20px; } \ No newline at end of file diff --git a/static/css/product.css b/static/css/product.css new file mode 100644 index 0000000..2a697f7 --- /dev/null +++ b/static/css/product.css @@ -0,0 +1,238 @@ +*{ + padding:0; +} + +h1, h2{ + margin-left: 0px; + text-align: left; + padding-bottom: 0; + margin-bottom: 0px; +} + +.seller{ + display: block; + height: 30px; + margin-top:10px; + padding: 0; + font-weight: bold; + font-size: 10pt; +} + +.badge{ + background-color: rgb(224, 224, 224); + color: black; + height: 15px; + padding-left: 3px; + padding-right: 3px; + font-size: 9pt; + display: block; + margin-top: -5; + border-radius: 5px; + padding-top: 0; + padding-bottom: 0; + margin-top: -10px; + border: 2px solid rgb(143, 143, 143); + float:left; +} +#content +{ + width: 1000px; + height:3000px; + background-color: rgb(244, 244, 244); + margin:auto; + padding-top: 10px; +} +#productPicture +{ + width: 40%; + height: 400px; + /*background-color: rgb(85, 85, 85);*/ + display: block; + float: left; + border-bottom: 2px solid rgb(104, 117, 151); +} + +#productPicture img +{ + width: 90%; + display: block; +} +#info +{ + width: 60%; + height: 400px; + display: block; + float: right; + /*background-color: rgb(121, 170, 182);*/ + border-bottom: 2px solid rgb(104, 117, 151);; +} + +.order-button{ + color: rgb(255, 255, 255); + font-weight: bold; + letter-spacing: 1.5px; + padding-left: 20px; + padding-right: 20px; + display:block; + float:left; + height: 35px; + border-radius: 5px; + border: 2px solid rgb(0, 99, 156); + background-color: rgb(0, 99, 156); + transition: 0.1s; + transition-timing-function: linear; + outline: none !important; + cursor:pointer; + margin-right: 30px; + margin-top: 10px; +} + +#description +{ + width: 100%; + display: block; + float: left; + background-color: rgba(103, 187, 183, 0); + border-bottom: 2px solid rgb(104, 117, 151); +} + +#description p,h2 { + padding-left: 30px; + padding-right: 30px; +} + +#newReview{ + width: 100%; + display: block; + float: left; + background-color: rgba(45, 66, 94, 0); + padding-bottom: 20px; + border-bottom: 2px solid rgb(104, 117, 151);; +} +#reviews{ + width: 100%; + height: 1600px; + display: block; + float: left; + background-color: rgba(39, 39, 39, 0); + +} +#reviewTemplate +{ + width: 100%; + float: left; + margin-bottom: 20px; +} +#reviewTemplatePicture +{ + width: 200px; + height: 200px; + display: block; + float: left; + background-color: rgb(85, 158, 255); +} + + +#data +{ + width: 100%; + height: 50px; + display: block; + float: left; + background-color: rgb(124, 142, 212); + +} +#text +{ + width: 100%; + height: 150px; + display: block; + float: left; + background-color: rgb(211, 196, 247); +} +#reviewTemplateText +{ + background-color: rgb(235, 196, 228); +} + +form{ + display:block; + margin-left: auto; + margin-right: auto; + width: 500px; +} + +form label{ + display:block; + margin-left: auto; + margin-right: auto; + width: 80%; + margin-top: 20px; +} + +form label p{ + width: 100%; + display:block; + margin-left: auto; + margin-right: auto; + margin-bottom: 0px; + font-weight: bold; + letter-spacing: 1.5px; +} +form label input{ + width: 100%; + display:block; + margin-left: auto; + margin-right: auto; + height: 35px; + border-radius: 5px; + border: 2px solid rgb(0, 99, 156); + background-color: rgb(255, 255, 255); + transition: 0.1s; + transition-timing-function: linear; + outline: none !important; +} +/* Submit button with blue background horizontal center*/ +form input[type="submit"]{ + color: rgb(255, 255, 255); + font-weight: bold; + letter-spacing: 1.5px; + width: 100%; + display:block; + margin-top: 20px; + margin-left: auto; + margin-right: auto; + height: 35px; + border-radius: 5px; + border: 2px solid rgb(0, 99, 156); + background-color: rgb(0, 99, 156); + transition: 0.1s; + transition-timing-function: linear; + outline: none !important; + cursor:pointer; +} + + +/* Hover */ + +form label input:hover{ + border: 2px solid rgb(1, 197, 246); +} + +form label input:focus{ + border: 2px solid rgb(1, 197, 246); + box-shadow: 0 0 5px #719ece62; +} + +form input[type="submit"]:hover{ + border: 2px solid rgb(7, 130, 200); + background-color: rgb(7, 130, 200); +} + +textarea{ + resize: vertical; + width: 100%; + display:block; + margin-left: auto; + margin-right: auto; +} \ No newline at end of file diff --git a/static/style.css b/static/css/style.css similarity index 88% rename from static/style.css rename to static/css/style.css index 03267ed..004bdb2 100644 --- a/static/style.css +++ b/static/css/style.css @@ -2,10 +2,17 @@ font-family: Arial, Helvetica, sans-serif; } +html,body{ + min-height: 100% !important; + height: 100%; +} + h1{ text-align: center; } + + #error-text{ color: red; font-weight: bold; diff --git a/static/images/examples.jpg b/static/images/examples.jpg new file mode 100644 index 0000000..0c47144 Binary files /dev/null and b/static/images/examples.jpg differ diff --git a/views/index.ejs b/views/index.ejs index b78130d..db077e7 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -1,10 +1,103 @@ - - -
- <%- include('partials/head'); %> + + + <%- include('partials/head'); %> - -<%= products[i].name %>
+ +
+
+
','_self')">
+
+
','_self')">
+
+
','_self')">
+
+
+
+ <%- error %>
@@ -19,10 +20,12 @@ + + \ No newline at end of file diff --git a/views/order_success.ejs b/views/order_success.ejs index f90d4b8..c4328d9 100644 --- a/views/order_success.ejs +++ b/views/order_success.ejs @@ -2,7 +2,7 @@ <%- include('partials/head'); %> - +
Nr.:<%= product.id %>
Verkäufer: <%= product.sellerName%>
Preis: <%= product.price %> € / Stück +
Kategorie: <%= category.name %>
<%= productDescription %>
+<%= product.productDescription %>
junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text junge geiler text
- -Bewertung: <%= reviews[i].rating %>
+<%= reviews[i].content %>
+ +