Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 1 addition & 24 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1 @@
<span><img src="https://s3.ap-south-1.amazonaws.com/venuefy.images/venuefy-logo.png"></span>

# ProblemStatement-IX

The IX-th Problem Statement.
------
<!-- edit this line below--!>

my work : &lt;your working link&gt;

<!--edit the line above--!>

<p>
</p>
<p>
Read the instruction.pdf.
<ul>
<li>fork this git repo and upload your files.</li>
<li>you can have multiple pull request, close one before opening another</li>
<li>edit Readme.md and replace &lt;your working link&gt; with url of your hosted site</li>
<li>start a pull request</li>
</ul>
</p>
### if you have face problem with images, you can use any image of your own choice
Mangement Crud operation using mongo db .Designed with node js.
31 changes: 31 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const express=require('express')
const path = require('path');
const helmet=require('helmet')
const mongoose=require('mongoose')
const bodyParser = require('body-parser');
const app=express()

const URI='mongodb+srv://owner:downer@cluster0-icfip.mongodb.net/products?retryWrites=true&w=majority'

app.set('view engine', 'ejs');
app.set('views', 'views');

const adminRoutes = require('./routes/admin');
const shopRoutes = require('./routes/shop');

app.use(helmet())
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));

app.use(shopRoutes)
app.use('/admin',adminRoutes)


mongoose
.connect(URI)
.then(result => {
app.listen(process.env.PORT || 5000);
})
.catch(err => {
console.log(err);
});
88 changes: 88 additions & 0 deletions controllers/admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
const Product =require('../modal/products')

exports.getCreateProduct=(req,res,next)=>{
res.render('create-product',{
editing:false
})
}
exports.postCreateProduct=(req,res,next)=>{
const title = req.body.title;
const image = req.body.image;
const price = req.body.price;
const description = req.body.description;

const product=new Product({
title: title,
price: price,
description: description,
imageUrl: image,
})
product.save()
.then(result=>{
res.redirect('/')
})
.catch(err=>{
console.log(err)
})
}

exports.getInput=(req,res)=>{
res.render('deleteProduct')
}
exports.postDeleteProduct = (req, res, next) => {
const prodId = req.body.productId;
Product.findById(prodId)
.then(product => {
if (!product) {
return next(new Error('Product not found.'));
}
return Product.deleteOne({ _id: prodId });
})
.then(() => {
console.log('DESTROYED PRODUCT');
res.redirect('/products');
})
.catch(err => {
const error = new Error(err);
error.httpStatusCode = 500;
return next(error);
});
};

exports.getEditInput=(req,res)=>{
res.render('editProduct')
}

exports.getEditProduct = (req, res, next) => {
res.render('editProduct')
};


exports.postEditProduct = (req, res, next) => {
const prodId = req.body.productId;
const updatedTitle = req.body.title;
const updatedPrice = req.body.price;
const image = req.body.image;
const updatedDesc = req.body.description;
console.log(prodId)
console.log(updatedPrice)
Product.findById(prodId)
.then(product => {
product.title = updatedTitle;
product.price = updatedPrice;
product.description = updatedDesc;
product.image=image
return product.save()
.then(result => {
console.log('UPDATED PRODUCT!');
res.redirect('/products');
});
})
.catch(err => {
const error = new Error(err);
error.httpStatusCode = 500;
return next(error);
});
};


21 changes: 21 additions & 0 deletions controllers/shop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const Product =require('../modal/products')
const mongoose = require('mongoose');

exports.getPage=(req,res,next)=>{
res.render('index')
}

exports.getProducts = (req, res, next) => {
Product.find()
.then(products => {
// console.log(products);
res.render('allProducts', {
prods: products
});
})
.catch(err => {
const error = new Error(err);
error.httpStatusCode = 500;
return next(error);
});
};
24 changes: 24 additions & 0 deletions modal/products.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const productSchema = new Schema({
title: {
type: String,
required:true
},
price: {
type: Number,
required:true
},
description: {
type: String,
required:true
},
imageUrl: {
type: String,
required:true
}
});

module.exports = mongoose.model('Product', productSchema);
Loading