mirror of
https://github.com/DerTyp7/shop-ejs-expressjs.git
synced 2025-10-29 20:42:10 +01:00
Filters & Merge
This commit is contained in:
36
index.js
36
index.js
@@ -210,15 +210,45 @@ app.post("/review/create/:productId", authenticatedHandler,(req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Search Page
|
// Search Page
|
||||||
app.get("/search/:query",authNoRedirectHandler,(req, res) => {
|
app.get("/search/:query/",authNoRedirectHandler,(req, res) => {
|
||||||
let query = req.params.query;
|
let query = req.params.query;
|
||||||
let dict = {
|
let dict = {
|
||||||
title: "Suche",
|
title: "Suche",
|
||||||
search: query,
|
search: query,
|
||||||
user: req.user,
|
user: req.user,
|
||||||
|
sort: req.query.sort ? req.query.sort : 0,
|
||||||
|
Cat: req.query.cat ? req.query.cat : 0
|
||||||
}
|
}
|
||||||
|
|
||||||
mysql_handler.con.query("SELECT *, (SELECT url FROM product_images i WHERE i.product_id = p.id LIMIT 1) as img, (SELECT AVG(rating) FROM reviews r WHERE r.productId = p.id) as rating FROM products p WHERE name LIKE ?;",["%"+query+"%"],function(err, result){
|
mysql_handler.con.query("SELECT * FROM categories;",function(err, result) {
|
||||||
|
if(err) throw err;
|
||||||
|
|
||||||
|
dict.categories = JSON.parse(JSON.stringify(result));
|
||||||
|
});
|
||||||
|
|
||||||
|
var catQuery = "";
|
||||||
|
var cat = req.query.cat;
|
||||||
|
if (typeof cat !== 'undefined' && cat != 0) {
|
||||||
|
catQuery = " AND categoryId = "+cat;
|
||||||
|
}
|
||||||
|
|
||||||
|
var sortQuery = "";
|
||||||
|
var sort = req.query.sort;
|
||||||
|
if (typeof sort !== 'undefined') {
|
||||||
|
if (sort == 1) {
|
||||||
|
sortQuery = " ORDER BY price ASC";
|
||||||
|
} else if (sort == 2) {
|
||||||
|
sortQuery = " ORDER BY price DESC";
|
||||||
|
} else if (sort == 3) {
|
||||||
|
sortQuery = " ORDER BY (SELECT SUM(quantity) FROM order_products o WHERE o.productId = p.id) DESC";
|
||||||
|
} else if (sort == 4) {
|
||||||
|
sortQuery = " ORDER BY name ASC";
|
||||||
|
} else if (sort == 5) {
|
||||||
|
sortQuery = " ORDER BY name DESC";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mysql_handler.con.query("SELECT *, (SELECT url FROM product_images i WHERE i.product_id = p.id LIMIT 1) as img, (SELECT AVG(rating) FROM reviews r WHERE r.productId = p.id) as rating FROM products p WHERE name LIKE ? "+catQuery+" "+sortQuery+";",["%"+query+"%"],function(err, result){
|
||||||
if(err) throw err;
|
if(err) throw err;
|
||||||
|
|
||||||
dict.products = JSON.parse(JSON.stringify(result));
|
dict.products = JSON.parse(JSON.stringify(result));
|
||||||
|
|||||||
@@ -12,8 +12,8 @@ con.query("SELECT * FROM users", function(err, result){
|
|||||||
|
|
||||||
let con = mysql.createConnection({ // TODO: change to config file
|
let con = mysql.createConnection({ // TODO: change to config file
|
||||||
host: "localhost",
|
host: "localhost",
|
||||||
user: "onlineshop",
|
user: "root",
|
||||||
password: "TestUser321", // TODO: DO NOT STORE PASSWORDS IN THE CODE
|
password: "", // TODO: DO NOT STORE PASSWORDS IN THE CODE
|
||||||
database: "onlineshop"
|
database: "onlineshop"
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@@ -7,9 +7,23 @@
|
|||||||
<header>
|
<header>
|
||||||
<%- include('partials/header'); %>
|
<%- include('partials/header'); %>
|
||||||
</header>
|
</header>
|
||||||
<div class="filtersDIV" style="display:none">
|
|
||||||
<br/>
|
<div class="filtersDIV">
|
||||||
<h3 style="text-align: center; "> FILTER COMMING SOON </h3>
|
<select id="cat-select" class="left" onchange="updateFilters();">
|
||||||
|
<option value="0">Kategorie: Alle</option>
|
||||||
|
<% for(var i=0; i < categories.length; i++) { var cat = categories[i]; %>
|
||||||
|
<option <%=Cat == cat.id ? "selected" : ""%> value="<%=cat.id%>">Kategorie: <%=cat.name %></option>
|
||||||
|
<% } console.log(sort) %>
|
||||||
|
</select>
|
||||||
|
|
||||||
|
<select id="sort-select" class="right" onchange="updateFilters();">
|
||||||
|
<option <%=sort == 0 ? "selected" : ""%> value="0">Sortierung: Standart</option>
|
||||||
|
<option <%=sort == 1 ? "selected" : ""%> value="1">Sortierung: Preis aufsteigend</option>
|
||||||
|
<option <%=sort == 2 ? "selected" : ""%> value="2">Sortierung: Preis absteigend</option>
|
||||||
|
<option <%=sort == 3 ? "selected" : ""%> value="3">Sortierung: Meistverkauft</option>
|
||||||
|
<option <%=sort == 4 ? "selected" : ""%> value="4">Sortierung: A-Z</option>
|
||||||
|
<option <%=sort == 5 ? "selected" : ""%> value="5">Sortierung: Z-A</option>
|
||||||
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<% if (products.length == 0) { %>
|
<% if (products.length == 0) { %>
|
||||||
@@ -69,22 +83,23 @@
|
|||||||
</html>
|
</html>
|
||||||
|
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
var cat = document.getElementById("cat-select");
|
||||||
|
var sort = document.getElementById("sort-select");
|
||||||
|
function updateFilters() {
|
||||||
|
window.open('/search/<%=search %>/?cat='+cat.value+'&sort='+sort.value,'_self')
|
||||||
|
}
|
||||||
|
|
||||||
function openProduct(id, newWindow) {
|
function openProduct(id, newWindow) {
|
||||||
window.open('/product/'+id, newWindow ? '_blank' : '_self')
|
window.open('/product/'+id, newWindow ? '_blank' : '_self')
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style type="text/css">
|
<style type="text/css">
|
||||||
html,body{
|
html, body{
|
||||||
height: 100% !important;
|
height: 100% !important;
|
||||||
display: block !important;
|
display: block !important;
|
||||||
min-height: 100vh;
|
min-height: 100vh;
|
||||||
flex-direction:unset !important;
|
flex-direction:unset !important;
|
||||||
}
|
|
||||||
|
|
||||||
a, a:hover, a:focus, a:active > .allProductsDIV {
|
|
||||||
text-decoration: none;
|
|
||||||
color: inherit;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* SEARCH INFO */
|
/* SEARCH INFO */
|
||||||
@@ -129,7 +144,7 @@
|
|||||||
right: 0;
|
right: 0;
|
||||||
bottom: 0;
|
bottom: 0;
|
||||||
}
|
}
|
||||||
.productDIV > .productImage {
|
.productImage {
|
||||||
width: calc(50% - 15px);
|
width: calc(50% - 15px);
|
||||||
height: calc(100% - 120px);
|
height: calc(100% - 120px);
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -142,7 +157,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* DESCRIPTION */
|
/* DESCRIPTION */
|
||||||
.productDIV > .productInfo {
|
.productInfo {
|
||||||
width: calc(50% - 15px);
|
width: calc(50% - 15px);
|
||||||
height: calc(100% - 60px);
|
height: calc(100% - 60px);
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -153,7 +168,7 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* PRICE ETC */
|
/* PRICE ETC */
|
||||||
.productDIV > .productData {
|
.productData {
|
||||||
width: calc(100% - 20px);
|
width: calc(100% - 20px);
|
||||||
height: 90px;
|
height: 90px;
|
||||||
position: absolute;
|
position: absolute;
|
||||||
@@ -164,16 +179,25 @@
|
|||||||
margin: 0px;
|
margin: 0px;
|
||||||
color: LightSlateGray;
|
color: LightSlateGray;
|
||||||
}
|
}
|
||||||
|
a, a:hover, a:focus, a:active > .allProductsDIV {
|
||||||
|
text-decoration: none;
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
|
||||||
/* FILTERS */
|
/* FILTERS */
|
||||||
.filtersDIV {
|
.filtersDIV {
|
||||||
|
margin-top: 20px;
|
||||||
position: relative;
|
position: relative;
|
||||||
left: calc(10% + 10px);
|
left: calc(10% + 10px);
|
||||||
width: calc(80% - 20px);
|
width: calc(80% - 20px);
|
||||||
height: 100px;
|
height: 30px;
|
||||||
background-color: grey;
|
|
||||||
}
|
}
|
||||||
|
.filtersDIV > .right {
|
||||||
|
float: right;
|
||||||
|
}
|
||||||
|
.filtersDIV > .left{
|
||||||
|
float: left;
|
||||||
|
}
|
||||||
|
|
||||||
/* PRODUCTS GRID */
|
/* PRODUCTS GRID */
|
||||||
.allProductsDIV {
|
.allProductsDIV {
|
||||||
|
|||||||
Reference in New Issue
Block a user