-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
99 lines (83 loc) · 2.87 KB
/
solution.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
import express from "express";
import bodyParser from "body-parser";
const app = express();
const port = 4000;
// In-memory data store
let posts = [
{
id: 1,
title: "how to make my plant look happy ?",
asked:"rakesh",
content:
"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Curabitur finibus erat tellus, quis interdum enim laoreet in. Aliquam erat volutpat. Integer elit justo, venenatis in neque ac, imperdiet egestas nibh. Mauris a tristique enim. Ut eget placerat sem, ut lacinia ipsum. Cras sed cursus ex. Quisque a dolor in arcu euismod lacinia.",
author: "Alex Thompson",
},
{
id: 2,
title: "are there any dating apps for plants ?",
asked:"ravi",
content:
"Donec id condimentum orci, sed aliquet sapien. Nullam bibendum purus vel elit elementum, at pellentesque urna malesuada. Mauris vel pulvinar mi. ",
author: "Mia Williams",
},
{
id: 3,
title: "is watering cycle different for different plants ?",
asked:"shukla",
content:
"Sed in lorem congue, scelerisque eros quis, pulvinar dui. Donec ut mauris at sem tincidunt auctor eget a felis. In orci erat, tempus eu fringilla vitae, posuere vel quam. ",
author: "Samuel Green",
},
];
let lastId = 3;
// Middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
// GET all posts
app.get("/posts", (req, res) => {
console.log(posts);
res.json(posts);
});
// GET a specific post by id
app.get("/posts/:id", (req, res) => {
const post = posts.find((p) => p.id === parseInt(req.params.id));
if (!post) return res.status(404).json({ message: "Post not found" });
res.json(post);
});
// POST a new post
app.post("/posts", (req, res) => {
const newId = lastId += 1;
const post = {
id: newId,
title: req.body.title,
content: req.body.content,
author: req.body.author,
asked: req.body.asked,
};
lastId = newId;
posts.push(post);
res.status(201).json(post);
});
// PATCH a post when you just want to update one parameter
app.patch("/posts/:id", (req, res) => {
const post = posts.find((p) => p.id === parseInt(req.params.id));
if (!post) return res.status(404).json({ message: "Post not found" });
if (req.body.title) post.title = req.body.title;
if (req.body.content) post.content = req.body.content;
if (req.body.author) post.author = req.body.author;
res.json(post);
});
// DELETE a specific post by providing the post id
app.delete("/posts/:id", (req, res) => {
const index = posts.findIndex((p) => p.id === parseInt(req.params.id));
if (index === -1) return res.status(404).json({ message: "Post not found" });
posts.splice(index, 1);
res.json({ message: "Post deleted" });
});
/*app.get("/addans/:id",(req,res)=>
{
res.render("modify.ejs",{posts[req.params.id-1]})
})*/
app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});