forked from divyansharma001/BloodLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsolution.js
130 lines (105 loc) · 3.16 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
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
import express from "express";
import bodyParser from "body-parser";
import pg from 'pg'
import dotenv from 'dotenv'
const app = express();
const port = 4000;
const db = new pg.Client({
user: "postgres",
host: "localhost",
database: "bloodBank",
password: "process.env.DB_PASSWORD",
port: 5432,
});
db.connect();
var idToedit;
// In-memory data store
let posts = [
{
id: 1,
bloodbankname: "Janakpuri Blood Bank",
a_pos:"1",
a_neg:"1",
b_pos:"1",
b_neg:"1",
ab_pos:"1",
ab_neg:"1",
o_pos:"1",
o_neg:"1",
bloodbankphone: "93054418XXX"
},
];
console.log(posts)
let lastId = 1;
async function checkPost() {
const result = await db.query(
"SELECT id, bloodbankname, a_pos, a_neg, b_pos, b_neg, ab_pos, ab_neg, o_pos, o_neg, bloodbankdata FROM bloodata",
);
console.log("resultRows:", result.rows)
result.rows.forEach((post) => {
posts.push(post);
});
}
// 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", async(req, res) => {
console.log("title01", req.body.title)
const newId = lastId += 1;
const post = {
id: newId,
bloodbankname: req.body.bloodbankname,
bloodbankphone: req.body.bloodbankphone,
a_pos: req.body.a_pos,
a_neg: req.body.a_neg,
b_pos: req.body.b_pos,
b_neg: req.body.b_neg,
ab_pos: req.body.ab_pos,
ab_neg: req.body.ab_neg,
o_pos: req.body.o_pos,
o_neg: req.body.o_neg,
};
console.log("post",post);
posts.push(post);
// try {
// await db.query(
// "INSERT INTO blooddata (id, bloodbankname, a_pos, a_neg, b_pos, b_neg, ab_pos, ab_neg, o_pos, o_neg, bloodbankphone) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)",
// [newId, req.body.bloodbankname, req.body.a_pos, req.body.a_neg, req.body.b_pos, req.body.b_neg, req.body.ab_pos, req.body.ab_neg, req.body.o_pos, req.body.o_neg, req.body.bloodbankphone]
// );
// } catch (err) {
// console.log(err);
// }
res.status(201).json(post);
// checkPost()
});
// 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.listen(port, () => {
console.log(`API is running at http://localhost:${port}`);
});