forked from kshitijanand36/Election-Corruption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
138 lines (90 loc) · 2.52 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
const _ = require('lodash');
const mongoose = require("mongoose");
const credentials = require(__dirname + '/Credentials.js');
//! for local testing and development .
// mongoose.connect('mongodb://localhost:27017/ElectionCorruption',{
// useNewUrlParser : true,
// useUnifiedTopology:true
// });
//! for deployment
mongoose.connect(process.env.DB_LINK,{
useNewUrlParser: true,
useUnifiedTopology: true
});
const postSchema = new mongoose.Schema({
post_title : {
type : String,
required : true
},
post_body : {
type : String ,
required : true
}
});
const Post = mongoose.model('Post',postSchema);
const homeStartingContent = "All the reports posted have been listed down here. You can read any of the reports by clicking on read more.";
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.get("/" , (req , res)=>{
res.sendFile(__dirname + "/index.html");
});
app.get("/latest", (req , res)=>{
Post.find({},(err,docs)=>{
res.render('content',{
starting_content : homeStartingContent,
posts : docs
});
})
});
app.get("/reports/:id" , (req , res)=>{
const reqd = req.params.id;
Post.findById(reqd , (err,doc)=>{
res.render('post',{thispost : doc});
});
})
app.get("/home" , (req , res)=>{
res.redirect("/");
})
app.get("/report" , (req , res)=>{
res.render("compose");
});
app.post("/compose" , (req , res)=>{
const newPost = new Post({
post_title : req.body.post_title,
post_body : req.body.post_body
});
newPost.save(()=>{
res.redirect('/latest');
})
});
//* update
app.get('/update/:id' , (req,res)=>{
const reqd = req.params.id;
Post.findById(reqd, (err,doc)=>{
res.render('update' , {thispost : doc});
})
});
app.post('/update/:id' , (req,res)=>{
const reqd = req.params.id;
const updatedTitle = req.body.post_title;
const updatedBody = req.body.post_body;
Post.findByIdAndUpdate(reqd,{post_title : updatedTitle , post_body : updatedBody} , (err,doc)=>{
res.redirect('/reports/' + reqd);
});
});
//* delete
app.post('/delete/:id',(req,res)=>{
const reqd = req.params.id;
Post.findByIdAndRemove(reqd, (err)=>{
res.redirect('/latest');
});
});
app.listen(process.env.PORT || 3000 , function() {
console.log("Server started on port 3000");
});