-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
146 lines (123 loc) · 3.67 KB
/
index.js
File metadata and controls
146 lines (123 loc) · 3.67 KB
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
140
141
142
143
144
145
146
const express = require("express");
const app = express();
const port = 3000;
const mongoose = require("mongoose");
const path = require("path");
const Chat = require("./models/chat.js");
const methodOverride = require("method-override");
const ExpressError = require("./ExpressError")
app.set("view engine", "ejs");
app.set("views", path.join(__dirname, "views"));
app.use(express.static(path.join(__dirname, "public")));
app.use(express.urlencoded({ extended: true }));
app.use(methodOverride("_method"));
main().then(() => {
console.log("Connected to MongoDB");
}).catch((err) => {
console.log(err);
})
async function main() {
await mongoose.connect("mongodb://127.0.0.1:27017/fakewhatsapp")
}
// start page
app.get("/", (req, res) => {
res.send("API is working . go to /chats to see the chats");
})
// index route
app.get("/chats", async (req, res) => {
try {
let chats = await Chat.find();
//console.log(chats);
res.render("index.ejs", { chats });
// res.send("Chats show section");
} catch (err) {
next(err);
}
})
// Form for create new route
app.get("/chats/new", (req, res) => {
// throw new ExpressError(404,"form not found" );
res.render("new.ejs")
// res.send("New route");
})
// create route
app.post("/chats", async (req, res, next) => {
try {
let { from, to, message } = req.body;
let newChat = new Chat({
from: from,
to: to,
message: message,
created_at: new Date(),
})
await newChat.save();
res.redirect("/chats");
} catch (err) {
next(new ExpressError(401, "You have fill the required fields"));
}
})
// NEW Show route--------
app.get("/chats/:id", async (req, res, next) => {
try{
let { id } = req.params;
let chat = await Chat.findById(id);
if (!chat) {
next(new ExpressError(404, "chat not found"));
}
res.render("edit.ejs", { chat });
}catch(err){
next(new ExpressError(404, "you are trying to access different thing"));
}
})
// edit form route
app.get("/chats/:id/edit", async (req, res) => {
try{
let { id } = req.params;
let chat = await Chat.findById(id);
res.render("edit.ejs", { chat });
}catch(err){
next(new ExpressError(401, "the message is not edited"));
}
})
// Update route
app.put("/chats/:id", async (req, res) => {
let { id } = req.params;
let { message: newMessage } = req.body;
try {
// Update the message and set updated_at to the current date
let updatedChat = await Chat.findByIdAndUpdate(
id,
{
message: newMessage,
updated_at: new Date() // Correctly update the updated_at field
},
{ runValidators: true, new: true } // Ensure validation and return the updated document
);
if (!updatedChat) {
next(new ExpressError(404, "chat not found"));
}
console.log("Updated Chat:", updatedChat);
res.redirect("/chats");
} catch (err) {
console.error("Error updating chat:", err);
res.status(500).send("Error updating chat");
}
});
// destroy route
app.delete("/chats/:id", async (req, res, next) => {
try {
let { id } = req.params;
let deletedChat = await Chat.findByIdAndDelete(id);
//console.log(deletedChat);
res.redirect("/chats");
} catch (err) {
next(err);
}
})
app.use((err, req, res, next) => {
let { status = 500, message = "some error occured" } = err;
res.status(status).send(message);
})
app.listen(port, () => {
console.log("Server is running on port 3000");
})