<repo_name>CodeLegacy/RestuarantReviewer<file_sep>/seeds.js
var mongoose = require("mongoose")
var Blog = require("./models/blogs"),
Comment = require("./models/comment"),
data = [
    {
        title:"France",
        image: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSIe6Xu8hGwEmaP4fb3HKwCSPFZhUF-9uTHwx6_pu6YDmjMboON",
        body:"Not of France"
    },{
        title: "India",
            image: "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTbWts0RTdJiP2p_Vv59V6x7LcvtG5wFC-LfXY8HKA0BgyrtVzx",
            body: "Not of India"
    },{
        title: "Italy",
            image: "https://images.livemint.com/img/2019/09/23/600x338/hotels-kZnG--621x414@LiveMint_1569216860852.jpg",
            body: "Not of Italy"
    }
]

function seedDB(){

    Blog.remove({}, function (err) {
        if (err) {
            console.log(err)
        }
        console.log("removed hotels")
        data.forEach(function (seed) {
            Blog.create(seed, function (err, hotel) {
                if (err) {
                    console.log(err)
                } else {
                    console.log("added a hotel")

                }
            })
        })
    })

    
}

module.exports = seedDB<file_sep>/app.js
var express = require("express")
app =express()
bodyParser=require("body-parser")
expressSanitizer=require("express-sanitizer")
mongoose=require("mongoose")
methodOverride=require("method-override")
passport = require("passport")
LocalStrategy = require("passport-local")
Blog = require("./models/blogs")
User = require("./models/user")
Comment = require("./models/comment")
seedDB = require("./seeds")

seedDB()
//mongoose.connect("mongodb://localhost/restaurant2", { useNewUrlParser: true,useUnifiedTopology:true })
//mongoose.connect("mongodb + srv://Samay:@cluster<EMAIL>/test?retryWrites=true&w=majority", { useNewUrlParser: true, useUnifiedTopology: true })

//const MongoClient = require('mongodb').MongoClient;
//const uri = "mongodb+srv://Samay:<EMAIL>/test?retryWrites=true&w=majority";
const uri = process.env.DATABASEURL

mongoose.connect(uri,{
    useNewUrlParser: true, useUnifiedTopology: true 
})
// const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// client.connect(err => {
//     const collection = client.db("test").collection("devices");
//     // perform actions on the collection object
//     client.close();
// })
mongoose.connection.on("connected",() => {
    console.log("Mongoose is connected!!!!!")
})

app.set("view engine","ejs")   
app.use(express.static("public"))
app.use(bodyParser.urlencoded({extended: true}))
app.use(expressSanitizer())
app.use(methodOverride("_method"))


app.use(require("express-session")({
    secret: "Just like that",
    resave: false,
    saveUninitialized: false
}))
app.use(passport.initialize())
app.use(passport.session())
passport.use(new LocalStrategy(User.authenticate()))
passport.serializeUser(User.serializeUser())
passport.deserializeUser(User.deserializeUser())
app.use(function (req, res, next) {
    res.locals.currentUser = req.user
    next()
})

app.get("/",function(req,res){
    Blog.find({},function(err,blogs){
        if(err){
            console.log("ERROR!")
        }
        else{
            res.render("home",{blogs:blogs,currentUser:req.user})
        }
    })
    
})


app.get("/blogs",function(req,res){
     if(req.query.search){
         const regex= new RegExp(escapeRegex(req.query.search),'gi')
         Blog.find({title: regex}, function (err, blogs) {
             if (err) {
                 console.log("ERROR!!")
             }
             else {
                 res.render("index", { blogs: blogs,currentUser:req.user })
             }
         })
     }else{
    Blog.find({},function(err,blogs){
        if(err){
            console.log("ERROR!!")
        }
        else{
            res.render("index",{blogs:blogs})
        }
    })
}}
)

app.get("/blogs/register", function (req, res) {
    Blog.find({}, function (err, blogs) {
        if (err) {
            console.log("ERROR!!")
        } else {
            res.render("register")
        }
    })
})

app.post("/blogs/register",function(req,res){
    var newUser = new User({username:req.body.username})
    User.register(newUser,req.body.password, function(err,user){
        if(err){
            console.log(err)
            return res.render("register")
        }
        passport.authenticate("local")(req,res,function(){
            res.redirect("/")
        })
    })
})

app.get("/blogs/login", function (req, res) {
    Blog.find({}, function (err, blogs) {
        if (err) {
            console.log("ERROR!!")
        }
        else {
            res.render("login", { blogs: blogs })
        }
    })
})


app.post("/blogs/login", passport.authenticate("local",
{
    successRedirect:"/",
    failureRedirect:"/blogs/login"
}),function(req,res){

})



app.get("/blogs/new",function(req,res){
    res.render("new")
})

app.post("/blogs",function(req,res){
     req.body.blog.body = req.sanitize(req.body.blog.body)
    Blog.create(req.body.blog,function(err,newBlog){
        if(err)
        {
            res.render("new")
        }
        else{
            res.redirect("/blogs")
        }
    })
})

app.get("/blogs/:id",function(req,res){
    Blog.findById(req.params.id).populate("comments").exec(function(err,foundBlog){
        if(err)
        {
            res.redirect("/blogs")
        }
        else
        {
           // console.log(foundBlog )
            res.render("show",{blog:foundBlog})
        }
    })
})

app.get("/blogs/:id/edit",function(req,res){
    Blog.findById(req.params.id,function(err,foundBlog){
        if(err)
        {
            res.redirect("/blogs")
        }
        else
        {
            res.render("edit",{blog:foundBlog})
        }
    })
})

app.put("/blogs/:id",function(req,res){
    req.body.blog.body = req.sanitize(req.body.blog.body)
    Blog.findByIdAndUpdate(req.params.id,req.body.blog,function(err,updatedBlog){
        if(err)
        {
            res.redirect("/blogs")
        }
        else{
            res.redirect("/blogs/"+req.params.id)
        }
    })
})

app.delete("/blogs/:id",function(req,res)
{
    Blog.findByIdAndRemove(req.params.id,function(err){
        if(err){
            res.redirect("/blogs")
        }
        else
        {
            res.redirect("/blogs")
        }
    })
})

app.get("/logout",function(req,res){
    req.logout()
    res.redirect("/")
})


//COMMENT ROUTES
app.get("/blogs/:id/comments/new",function(req,res){
    Blog.findById(req.params.id,function(err,blog){
        if(err)
        console.log(err)
        else{

            res.render("comments/new",{blog:blog})
        }
    })
})

app.post("/blogs/:id/comments",function(req,res){
    Blog.findById(req.params.id, function (err, blog) {
        if (err)
            {console.log(err)
            res.redirect("/blogs")
            }
        else {
            Comment.create(req.body.comment,function(err,comment){
                if(err){
                    console.log(err)
                }
                else{
                    blog.comments.push(comment)
                    blog.save();
                    res.redirect('/blogs/'+blog._id)
                }
            })
        }
    })
})

function escapeRegex(text){
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g,"\\s&")
}

app.listen(process.env.PORT,process.env.IP,function(){
    console.log("Server is Running")
})<file_sep>/README.md
# RestaurantReviewer


<p>This project focuses on the development of restaurant reviewer , a website where one can search for hotels and can give or read the reviews given also about the menu , timing of the restuarant and location of the restaurant.</p>
<h2>WebPage Link: <a href="https://agile-everglades-67068.herokuapp.com">Go to Site</a></h2>

<h1>Prerequirements:</h1>
<ul>
  <li>nodejs</li>
  <li>npm</li>
  <li>mongo</li>
</ul>
<h2>Packages to be installed </h2>
<ul>
  <li>passport</li>
  <li>express</li>
  <li>mongoose</li>
  <li>body-parser</li>
  <li>ejs</li>
</ul>
<h3>To login as admin, type your username as "Admin"</h3>
