Skip to content

Commit

Permalink
ecommerce-web-app v2
Browse files Browse the repository at this point in the history
  • Loading branch information
imsankalp committed May 30, 2021
1 parent b6bab9c commit b813167
Show file tree
Hide file tree
Showing 10 changed files with 166 additions and 133 deletions.
27 changes: 25 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ const path = require('path');
// const seedDB = require('./seed');
const productRoutes = require('./routes/product');
const methodOverride = require('method-override');
const session = require('express-session');
const flash = require('connect-flash');


mongoose.connect('mongodb://localhost:27017/shopApp',
{
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify:false
useFindAndModify:false,
useCreateIndex:true,
})
.then(() =>{
console.log("DB connected");
Expand All @@ -25,6 +29,21 @@ app.use(express.static(path.join(__dirname, '/public')));
app.use(express.urlencoded({extended: true}));
app.use(methodOverride('_method'));

const sessionConfig = {
secret:"weneedsomebettersecret",
resave: false,
saveUninitialized: true
}
app.use(session(sessionConfig));
app.use(flash());
app.use((req, res, next) => {
res.locals.success = req.flash('success');
res.locals.error = req.flash('error');
next();
})



// seedDB();

app.get('/', (req, res) => {
Expand All @@ -39,4 +58,8 @@ app.use(productRoutes);
app.listen(3000, () => {

console.log("Server running at port no 3000");
})
})




2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
"author": "sankalp singh",
"license": "ISC",
"dependencies": {
"connect-flash": "^0.1.1",
"ejs": "^3.1.6",
"express": "^4.17.1",
"express-session": "^1.17.2",
"method-override": "^3.0.0",
"mongoose": "^5.12.11",
"nodemon": "^2.0.7"
Expand Down
99 changes: 78 additions & 21 deletions routes/product.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ const Product = require('../models/product')
const methodOverride = require('method-override');
const Review= require('../models/review');

//Show Products
//Show all Products
router.get('/products', async (req, res) => {

const products= await Product.find({});
res.render('products/index', {products});
})
try{
const products= await Product.find({});
res.render('products/index', {products});
}
catch (e){
console.log("Something went wrong");
req.flash('error', 'Cannot find products');
res.render('error');
}
});

//Create a new product
router.get('/products/new', (req, res) => {
Expand All @@ -18,50 +25,100 @@ router.get('/products/new', (req, res) => {
})
router.post('/products', async(req, res) => {

await Product.create(req.body.product);
res.redirect('/products');
})
try{
await Product.create(req.body.product);
req.flash('success','Product created successfully!');
res.redirect('/products');
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot create new product! Something went wrong');
res.render('error');
}
});

//Show a particular product
router.get('/products/:id', async (req, res) => {

const product = await Product.findById(req.params.id).populate('reviews');;
res.render('products/show', {product});
try{
const product = await Product.findById(req.params.id).populate('reviews');;
res.render('products/show', {product});
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot find this Product');
res.redirect('/error');
}
})

//Get edit form
router.get('/products/:id/edit', async(req, res) => {

const product = await Product.findById(req.params.id);
res.render('products/edit', {product});
try{
const product = await Product.findById(req.params.id);
res.render('products/edit', {product});
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot Edit this Product');
res.redirect('/error');
}
})


//Update the product
router.patch('/products/:id', async(req, res) => {

await Product.findByIdAndUpdate(req.params.id, req.body.product);
res.redirect('/products');
try{
await Product.findByIdAndUpdate(req.params.id, req.body.product);
req.flash('success', 'Updated Successfully!');
res.redirect(`/products/${req.params.id}`)
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot update this Product');
res.redirect('/error');
}
})

//Delete a product
router.delete('/products/:id', async(req, res) => {

await Product.findByIdAndDelete(req.params.id);
res.redirect('/products');
try{
await Product.findByIdAndDelete(req.params.id);
res.redirect('/products');
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot delete this Product');
res.redirect('/error');
}
})

//Creating a new comment
router.post('/products/:id/review', async (req, res) => {

const product = await (await Product.findById(req.params.id));
const review = new Review(req.body);
try{
const product = await (await Product.findById(req.params.id));
const review = new Review(req.body);

product.reviews.push(review);

product.reviews.push(review);
await review.save();
await product.save();

await review.save();
await product.save();
res.redirect(`/products/${req.params.id}`);
}
catch(e) {
console.log(e.message);
req.flash('error', 'Cannot add review to this Product');
res.redirect('/error');
}

res.redirect(`/products/${req.params.id}`);
})

router.get('/error', (req, res) => {
res.status(404).render('error');
})

module.exports = router;
9 changes: 9 additions & 0 deletions views/error.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<%- include("./partials/header")%>


<div class="container mt-3">
<%- include("./partials/flash")%>
<h1>This is Error Page</h1>
</div>

<%- include("./partials/footer")%>
13 changes: 13 additions & 0 deletions views/partials/flash.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<% if(success && success.length){ %>
<div class="alert alert-success alert-dismissible fade show mt-2" role="alert">
<%=success%>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<%}%>

<% if(error && error.length){ %>
<div class="alert alert-danger alert-dismissible fade show mt-2" role="alert">
<%=error%>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<%}%>
137 changes: 29 additions & 108 deletions views/partials/footer.ejs
Original file line number Diff line number Diff line change
@@ -1,132 +1,53 @@
<br><br>
<!-- Footer -->
<footer class="page-footer font-small stylish-color-dark pt-4" style="background-color: #0077b6; color: aliceblue;">

<!-- Footer Links -->
<div class="container text-center text-md-left">

<!-- Grid row -->
<div class="row">

<!-- Grid column -->
<div class="col-md-4 mx-auto">

<!-- Content -->
<h5 class="font-weight-bold text-uppercase mt-3 mb-4">Shopping <i style="font-size:2rem;">K</i>art</h5>
<p>This is the best e-commerce website. This website has the best products available and we ship to all parts of India. Hope you are having nice experience!</p>

</div>
<!-- Grid column -->

<hr class="clearfix w-100 d-md-none">

<!-- Grid column -->
<div class="col-md-2 mx-auto">

<!-- Links -->
<h5 class="font-weight-bold text-uppercase mt-3 mb-4">Links</h5>

<ul class="list-unstyled">
<li>
<a style="color: snow;" href="#!">About Us</a>
</li>
</ul>

</div>
<!-- Grid column -->

<hr class="clearfix w-100 d-md-none">

<!-- Grid column -->
<div class="col-md-2 mx-auto">

<!-- Links -->
<h5 class="font-weight-bold text-uppercase mt-3 mb-4">Links</h5>

<ul class="list-unstyled">
<li>
<a style="color: snow;" href="#!">Reach Us</a>
</li>
</ul>

</div>
<!-- Grid column -->

<hr class="clearfix w-100 d-md-none">

<!-- Grid column -->
<div class="col-md-2 mx-auto">

<!-- Links -->
<h5 class="font-weight-bold text-uppercase mt-3 mb-4">Links</h5>

<ul class="list-unstyled">
<li>
<a style="color: snow;" href="#!">Feedback</a>
</li>
</ul>

</div>
<!-- Grid column -->

</div>
<!-- Grid row -->

</div>
<!-- Footer Links -->

<hr>

<!-- Call to action -->
<ul class="list-unstyled list-inline text-center py-2">
<li class="list-inline-item">
<h5 class="mb-1">Register for free</h5>
</li>
<li class="list-inline-item">
<a href="#!" class="btn btn-danger btn-rounded">Sign up!</a>
</li>
</ul>
<!-- Call to action -->

<hr>

<!-- Footer -->
<footer class="page-footer font-small special-color-bg-blue pt-4" style="background-color: #0077b6 ;">

<!-- Footer Elements -->
<div class="container">

<!-- Social buttons -->
<ul class="list-unstyled list-inline text-center">
<li class="list-inline-item">
<a style="color: snow;" class="btn-floating btn-fb mx-1">
<i class="fab fa-facebook-f"> </i>
<a class="btn-floating btn-fb mx-1">
<i class="fab fa-facebook-f" style="color: aliceblue;"> </i>
</a>
</li>
<li class="list-inline-item">
<a style="color: snow;" class="btn-floating btn-tw mx-1">
<i class="fab fa-twitter"> </i>
<a class="btn-floating btn-tw mx-1">
<i class="fab fa-twitter" style="color: aliceblue;"> </i>
</a>
</li>
<li class="list-inline-item">
<a style="color: snow;" class="btn-floating btn-gplus mx-1">
<i class="fab fa-google-plus-g"> </i>
<a class="btn-floating btn-gplus mx-1">
<i class="fab fa-google-plus-g" style="color: aliceblue;"> </i>
</a>
</li>
<li class="list-inline-item">
<a style="color: snow;" class="btn-floating btn-li mx-1">
<i class="fab fa-linkedin-in"> </i>
<a class="btn-floating btn-li mx-1">
<i class="fab fa-linkedin-in" style="color: aliceblue;"> </i>
</a>
</li>
<li class="list-inline-item">
<a style="color: snow;" class="btn-floating btn-dribbble mx-1">
<i class="fab fa-dribbble"> </i>
<a class="btn-floating btn-dribbble mx-1">
<i class="fab fa-dribbble" style="color: aliceblue;"> </i>
</a>
</li>
</ul>
<!-- Social buttons -->

<!-- Copyright -->
<div class="footer-copyright text-center py-3">© 2020 Copyright:
<a style="color: snow;" href="">Sankalp Singh</a>
</div>
<!-- Copyright -->

</footer>

</div>
<!-- Footer Elements -->

<!-- Copyright -->
<div class="footer-copyright text-center py-3" style="color: aliceblue;">© 2020 Copyright:
<a href="http://github.com/imsankalp" style="text-decoration: none; color: aliceblue;"> Sankalp Singh</a>
</div>
<!-- Copyright -->

</footer>
<!-- Footer -->


<script>
(function () {
Expand Down
Loading

0 comments on commit b813167

Please sign in to comment.