-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
110 lines (101 loc) · 3.01 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
const express = require("express");
const mongoose = require("mongoose");
const app = express();
const Articll = require("./models/articlemodel");
const trends = require("./models/trendingmodel");
app.use(express.json());
app.post("/api/article", async (req, res) => {
try {
const articll = await Articll.create(req.body);
res.status(200).json(articll);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
app.get("/api/article/all", async (req, res) => {
try {
const articles = await Articll.find({});
res.status(200).json(articles);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.get("/api/article/:id", async (req, res) => {
try {
const { id } = req.params;
const article = await Articll.findById(id);
res.status(200).json(article);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.post("/api/article/trending", async (req, res) => {
try {
const trend = await trends.create(req.body);
res.status(200).json(trend);
} catch (error) {
console.log(error.message);
res.status(500).json({ message: error.message });
}
});
app.get("/api/article/trending/all", async (req, res) => {
try {
const trendings = await trends.find({});
res.status(200).json(trendings);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.put("/api/article/:id", async (req, res) => {
try {
const { id } = req.params;
const article = await Articll.findByIdAndUpdate(id, req.body);
if (!article) {
return res.status(404).json({ message: "Article not found " });
}
const updatedArticle = await Articll.findById(id);
res.status(200).json(updatedArticle);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.delete("/api/article/:id", async (req, res) => {
try {
const { id } = req.params;
const article = await Articll.findByIdAndDelete(id);
if (!article) {
return res.status(404).json({ message: "Article not found " });
}
res.status(200).json(article);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
app.delete("/api/article/:id/like", async (req, res) => {});
app.post("/api/article/:id/like", async (req, res) => {
try {
const { id } = req.params;
const article = await Articll.findByIdAndUpdate(id, req.body);
if (!article) {
return res.status(404).json({ message: "Article not found " });
}
const updatedArticle = await Articll.findById(id);
res.status(200).json(updatedArticle);
} catch (error) {
res.status(500).json({ message: error.message });
}
});
mongoose
.connect(
"mongodb+srv://Peeyushdas:[email protected]/?retryWrites=true&w=majority"
)
.then(() => {
console.log("connected to MONGOserver");
app.listen(2000, () => {
console.log("connected to server at 2000");
});
})
.catch((error) => {
console.log(error);
});