-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
124 lines (104 loc) · 3.29 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
const express = require("express")
const app =express()
const mongoose = require('mongoose');
const MONGO_URL='mongodb://127.0.0.1:27017/ghumo'
const Listing = require ('/Users/rehbarkhan/Documents/codebox/major project/models/listing.js')
const path = require("path");
const methodOverride = require ("method-override")
const ejsMate = require("ejs-mate")
const wrapAsync = require("./utlis/wrapAsync.js")
const ExpressError = require("./utlis/ExpressError.js")
const Review = require ('/Users/rehbarkhan/Documents/codebox/major project/models/review.js')
main().then(()=>{
console.log("Main is working!")
})
.catch(err=>{
console.log(err)
})
async function main(){
await mongoose.connect(MONGO_URL);
}
app.set("view engine",'ejs')
app.set("views",path.join(__dirname,"views"))
app.use(express.urlencoded({extended:true}))
app.use(methodOverride("_method"))
app.engine("ejs",ejsMate)
app.use(express.static(path.join(__dirname, "public")));
//home
app.get("/",wrapAsync(async(req,res)=>{
res.render("listings/home.ejs")
}))
//main page
app.get("/main",wrapAsync(async(req,res)=>{
res.render("listings/main.ejs")
}))
//index Route!
app.get("/listings",wrapAsync(async(req,res)=>{
const allListings =await Listing .find({});
res.render("listings/index.ejs",{allListings})
}))
//New route
app.get("/listings/new",(req,res)=>{
res.render("listings/new.ejs");
})
//review route
//post
app.post("/listings/:id/reviews",wrapAsync(async(req,res)=>{
let listing = await Listing.findById(req.params.id);
let newReview = new Review (req.body.review)
listing.reviews.push(newReview)
await newReview.save()
await listing.save()
console.log("new review saved")
res.send("New review saved")
}))
//Show route
app.get("/listings/:id",wrapAsync(async(req,res)=>{
let {id} = req.params;
const listing = await Listing.findById(id).populate("reviews");
res.render("listings/show.ejs",{listing});
})
)
//create route
app.post("/listings",wrapAsync(async (req,res,next)=>{
if(!req.body.listing){
throw new ExpressError(404,"Send valid Data for listing")
}
const newListing=new Listing(req.body.listing);
await newListing.save();
res.redirect("/listings")
}))
//edit Route
app.get("/listings/:id/edit",wrapAsync(async(req,res)=>{
let {id} = req.params;
const listing = await Listing.findById(id);
res.render("listings/edit.ejs",{listing});
}))
// update route
app.put("/listings/:id",wrapAsync(async (req,res)=>{
let {id} = req.params;
await Listing.findByIdAndUpdate(id,{...req.body.listing}) //deconstruction ...req.body.listing
res.redirect(`/listings/${id}`)
}))
//delete route
app.delete("/listings/:id",wrapAsync(async(req,res)=>{
let {id} = req.params;
let deletedListing = await Listing.findByIdAndDelete(id);
console.log(deletedListing)
res.redirect("/listings")
}))
app.listen(8080,()=>{
console.log("port listening is working")
});
app.all("*",(req,res,next)=>{
next(new ExpressError(404,"page not found!"))
});
app.use((err,req,res,next)=>{
let {statusCode,message} = err;
// res.status(statusCode).send(message)
res.render("./listings/error.ejs",{err})
console.log(err)
});
app.get("/",(req,res)=>{
res.send("http://localhost:8080/listings")
})