-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
139 lines (118 loc) · 3.72 KB
/
server.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
139
const express = require("express");
const app = express();
const bodyParser = require("body-parser");
var ObjectId = require('mongodb').ObjectID;
//database connection
const MongoClient = require("mongodb").MongoClient;
app.use(bodyParser.json());
MongoClient.connect(
"mongodb+srv://myexpressapp:[email protected]/test?retryWrites=true&w=majority",
{ useNewUrlParser: true, useUnifiedTopology: true }
)
.then(client => {
console.log("connected to database");
const mydb = client.db("mydb");
const col = mydb.collection("thoughts");
//this is to handle the post request sent when submit button is clicked
app.post("/thoughts", (req, res) => {
req.body.likes = { count: 0, person: [] };
req.body.comments = [{ by: "", comment: "" }];
req.body.name = req.body.name.trim();
req.body.thought = req.body.thought.trim();
col
.insertOne(req.body)
.then(result => {
res.redirect("/");
})
.catch(err => {
console.log(err);
});
});
app.get("/", (req, res) => {
col
.find()
.sort({ likes: -1 })
.toArray()
.then(result => {
res.render("index.ejs", { thoughts: result });
});
});
//to handle the get-comments request from fetch
app.get("/get-comments/:id", (req, res) => {
col.findOne({ _id : ObjectId(req.params.id) }).then(result => {
res.render("commentModal.ejs", { data: result, id: req.params.id });
});
});
//code to handle post request to add-comment
app.put("/get-comments/add-comment", async (req, res) => {
let commentsDoc = await col.findOne({ _id: ObjectId(req.body.id) });
let comments = commentsDoc.comments;
comments.push({ by: req.body.commentBy, comment: req.body.comment });
col
.updateOne({ _id: ObjectId(req.body.id) }, { $set: { comments: comments } })
.then(result => {
res.json("ok");
})
.catch(err => {
console.log(err);
});
});
// to handle the delete request from fetch
app.delete("/del-thoughts", (req, res) => {
console.log(req.body.id);
col
.deleteOne({
_id: ObjectId(req.body.id)
})
.then(result => {
res.json("Post is deleted");
})
.catch(err => {
console.log(err);
});
});
//code for handling the put request for the like button
app.put("/addLike", async (req, res) => {
let likesDoc = await col.findOne({ _id : ObjectId(req.body.id)});
let likesCount = likesDoc.likes.count;
let person = likesDoc.likes.person;
if (!person.includes(req.body.person)) {
person.push(req.body.person);
col
.updateOne(
{ _id : ObjectId(req.body.id) },
{ $set: { likes: { count: likesCount + 1, person: person } } }
)
.then(result => {
res.json("Like Added");
})
.catch(err => {
console.log(err);
});
} else {
person = person.filter(p => {
return p !== req.body.person;
});
col
.updateOne(
{ _id : ObjectId(req.body.id) },
{ $set: { likes: { count: likesCount - 1, person: person } } }
)
.then(result => {
res.json("Like Removed");
})
.catch(err => {
console.log(err);
});
}
});
})
.catch(err => {
console.log(err);
});
app.set("view engine", "ejs");
app.use(express.static("public"));
app.use(bodyParser.urlencoded({ extended: true }));
app.listen(4000, () => {
console.log("running at port 4000...");
});