-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
93 lines (84 loc) · 2.31 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
import express from "express";
import bodyParser from "body-parser";
import pg from "pg";
const app = express();
const port = 4000;
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: "blog",
password: "123456",
port: 5432,
});
db.connect();
// In-memory data store
let posts = [];
const result = await db.query("SELECT * FROM posts");
posts = result.rows
let lastId = 3;
// Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
//Write your code here//
//GET All posts
app.get("/posts",(req,res) => {
res.json(posts);
})
//GET a specific post by id
app.get("/posts/:id",(req,res) => {
const id=parseInt(req.params.id);
res.json(posts.find((post) => post.id===id));
})
//POST a new post
app.post("/posts",async(req,res) => {
const id=posts[posts.length-1].id+1;
let d;
d=new Date();
d=d.toISOString();
const data={
id: id,
title: req.body.title,
content: req.body.content,
author: req.body.author,
date: d
}
const r = await db.query("INSERT INTO posts values($1,$2,$3,$4,$5);",[data.id,data.title,data.content,data.author,data.date])
posts.push(data);
res.json(posts.find((joke) => joke.id===id));
})
//PATCH a post when you just want to update one parameter
app.patch("/posts/:id",async(req,res) => {
const id=parseInt(req.params.id);
const index=posts.findIndex((post) => post.id===id);
let d;
d=new Date();
d=d.toISOString();
const data={
id: id,
title: req.body.title || posts[index].title,
content: req.body.content || posts[index].content,
author: req.body.author || posts[index].author,
date: d
}
const r = await db.query("UPDATE posts set title=$1, content=$2, author=$3 where id=$4;",[data.title,data.content,data.author,data.id])
posts[index]=data;
res.json(data);
})
//DELETE a specific post by providing the post id.
app.delete("/posts/:id",async(req,res) => {
const id=parseInt(req.params.id);
const index=posts.findIndex((post) => post.id===id);
if (index>-1){
const r = await db.query("DELETE FROM posts where id=$1;",[id]);
posts.splice(index,1);
res.sendStatus(200);
}
else{
res
.status(404)
.json({error:`Joke with id ${id} not found. No joke was deleted.`})
}
})
app.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});