-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
107 lines (98 loc) · 4.1 KB
/
index.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
import express from "express";
import bodyParser from "body-parser";
const app = express();
const port = 4000;
// In-memory data store
let posts = [
{
id: 1,
title: "The Rise of Decentralized Finance",
content:
"Decentralized Finance (DeFi) is an emerging and rapidly evolving field in the blockchain industry. It refers to the shift from traditional, centralized financial systems to peer-to-peer finance enabled by decentralized technologies built on Ethereum and other blockchains. With the promise of reduced dependency on the traditional banking sector, DeFi platforms offer a wide range of services, from lending and borrowing to insurance and trading.",
author: "Alex Thompson",
date: "2023-08-01, 06:30:31 pm",
},
{
id: 2,
title: "The Impact of Artificial Intelligence on Modern Businesses",
content:
"Artificial Intelligence (AI) is no longer a concept of the future. It's very much a part of our present, reshaping industries and enhancing the capabilities of existing systems. From automating routine tasks to offering intelligent insights, AI is proving to be a boon for businesses. With advancements in machine learning and deep learning, businesses can now address previously insurmountable problems and tap into new opportunities.",
author: "Mia Williams",
date: "2023-08-05, 12:14:43 am",
},
{
id: 3,
title: "Sustainable Living: Tips for an Eco-Friendly Lifestyle",
content:
"Sustainability is more than just a buzzword; it's a way of life. As the effects of climate change become more pronounced, there's a growing realization about the need to live sustainably. From reducing waste and conserving energy to supporting eco-friendly products, there are numerous ways we can make our daily lives more environmentally friendly. This post will explore practical tips and habits that can make a significant difference.",
author: "Samuel Green",
date: "2023-08-10, 01:06:38 pm",
},
];
let lastId = 3;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Write your code here//
//CHALLENGE 1: GET All posts
app.get("/posts",(req,res)=>{
res.json(posts);
});
//CHALLENGE 2: GET a specific post by id
app.get("/posts/:id",(req,res)=>{
const id=parseInt(req.params.id);
const specificPost=posts.find((p)=>p.id===id);
if(!specificPost) return res.status(404).json({message: "Post not found."});
res.json(specificPost);
});
//CHALLENGE 3: POST a new post
app.post("/posts",(req,res)=>{
const options = { timeZone: 'Asia/Kolkata', hour12: true };
const currentISTTime = new Date().toLocaleString('en-IN', options);
const newBlog={
id: lastId+1,
title: req.body.title,
content: req.body.content,
author: req.body.author,
date: currentISTTime,
};
lastId+=1;
posts.push(newBlog);
res.status(201).json(newBlog);
})
//CHALLENGE 4: PATCH a post when you just want to update one parameter
app.patch("/posts/:id",(req,res)=>{
const id = parseInt(req.params.id);
const existingPost=posts.find((post)=>post.id===id);
if (!existingPost) return res.status(404).json({ message: "Post not found" });
const options = { timeZone: 'Asia/Kolkata', hour12: true };
const currentISTTime = new Date().toLocaleString('en-IN', options);
const replacementPost={
id: id,
title: req.body.title || existingPost.title,
content: req.body.content || existingPost.content,
author: req.body.author || existingPost.author,
date: currentISTTime,
};
const postIndex=posts.findIndex((post)=>post.id===id);
posts[postIndex]=replacementPost;
// console.log(posts[postIndex]);
res.json(posts[postIndex]);
})
//CHALLENGE 5: DELETE a specific post by providing the post id.
app.delete("/posts/:id",(req,res)=>{
const id = parseInt(req.params.id);
const searchIndex=posts.findIndex((post)=>post.id===id);
if(searchIndex>-1){
posts.splice(searchIndex,1);
res.sendStatus(200);
}
else{
res.status(404);
res.json({error: `Joke with id : ${id} not found.
No jokes were deeted.`});
}
})
app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});