-
Notifications
You must be signed in to change notification settings - Fork 0
/
mobileApi.js
196 lines (191 loc) · 5.97 KB
/
mobileApi.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
const express = require("express");
const app = express();
app.use(express.json());
app.use(function (req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header(
"Access-Control-Allow-Headers",
"Origin, X-Requested-With, Content-Type, Accept"
);
res.header(
"Access-Control-Allow-Methods",
"GET, POST, OPTIONS, PUT, PATCH, DELETE, HEAD"
);
next();
});
const { Client } = require("pg");
const client = new Client({
user: "postgres",
password: "RgrohitG@938",
database: "postgres",
port: 5432,
host: "db.ybwkusxubwtotgyapcob.supabase.co",
ssl: { rejectUnauthorized: false },
});
const port = process.env.PORT || 2410;
app.listen(port, () => console.log(`Node app listening on port ${port}`));
client.connect(function (err) {
if (err) {
console.error("Error connecting to the database:", err);
} else {
console.log("Connected to the database!");
}
});
app.get("/svr/mobile", function (req, res) {
const sql = "SELECT * FROM mobile";
client.query(sql, function (err, result) {
if (err) {
console.error("Error fetching Mobile:", err);
res.status(500).send("An error occurred while fetching Mobile.");
} else {
res.send(result.rows);
}
});
});
app.get("/svr/mobile/:id", function (req, res) {
const id = +req.params.id;
const sql = "SELECT * FROM mobile WHERE id = $1";
client.query(sql, [id], function (err, result) {
if (err) {
console.error("Error fetching mobile:", err);
res.status(500).send("An error occurred while fetching Mobile.");
} else {
res.send(result.rows);
}
});
});
app.get("/svr/mobile/RAM/:ram", function (req, res) {
let ram = req.params.ram;
console.log(ram);
let sql = `select * from mobile where ram = $1`;
client.query(sql, [ram], function (err, result) {
if (err) res.status(404).send(err);
else {
res.send(result.rows);
}
});
});
app.get("/svr/mobile/ROM/:rom", function (req, res) {
let rom = req.params.rom;
console.log(rom);
let sql = `select * from mobile where rom = $1`;
client.query(sql, [rom], function (err, result) {
if (err) res.status(404).send(err);
else {
res.send(result.rows);
}
});
});
app.get("/svr/mobile/brand/:brand", function (req, res) {
let brand = req.params.brand;
console.log(brand);
let sql = "select * from mobile where brand = $1";
client.query(sql, [brand], function (err, result) {
if (err) res.status(404).send(err);
else {
res.send(result.rows);
}
});
});
app.get("/svr/mobile/OS/:os", function (req, res) {
let os = req.params.os;
console.log(os);
let sql = "select * from mobile where os = $1";
client.query(sql, [os], function (err, result) {
if (err) res.status(404).send(err);
else {
res.send(result.rows);
}
});
});
app.get("/svr/resetData", function (req, res) {
const deleteQuery = "DELETE FROM mobile";
client.query(deleteQuery, function (err, deleteResult) {
if (err) {
console.error("Error while deleting data:", err);
res.status(500).send("An error occurred while deleting data.");
} else {
const { mobiles } = require("./mobileData");
const insertQuery =
"INSERT INTO mobile (name, price, brand, ram, rom, os) VALUES ";
const values = mobiles.map(
(p) =>
`('${p.name}', ${p.price}, '${p.brand}', '${p.RAM}', '${p.ROM}', '${p.OS}')`
);
const fullQuery = insertQuery + values.join(",");
client.query(fullQuery, function (err, insertResult) {
if (err) {
console.error("Error while resetting data:", err);
res.status(500).send("An error occurred while resetting data.");
} else {
res.send(
"Successfully reset data. Affected rows: " + insertResult.rowCount
);
}
});
}
});
});
app.post("/svr/mobile", function (req, res, next) {
console.log("Request Body:", req.body);
let { name, price, brand, ram, rom, os } = req.body;
const insertQuery =
"INSERT INTO mobile (name, price, brand, ram, rom,os) VALUES ($1, $2, $3, $4, $5, $6)";
const values = [name, price, brand, ram, rom, os];
client.query(insertQuery, values, function (err, result) {
if (err) {
console.error("Error while inserting data:", err);
res.status(500).send("An error occurred while inserting data.");
} else {
console.log(result);
res.send(`${result.rowCount} insertion Successful`);
}
});
});
app.put("/svr/mobile/:id", function (req, res) {
const id = +req.params.id;
let { name, price, brand, ram, rom, os } = req.body;
if (id) {
const selectQuery = "SELECT * FROM mobile WHERE id = $1";
client.query(selectQuery, [id], function (err, result) {
if (err) {
res.status(404).send(err);
} else {
if (result.rows.length === 0) {
res.status(404).send("Employee with the given ID not found.");
} else {
const updateQuery =
"UPDATE mobile SET name=$1, price=$2, brand=$3, ram=$4, rom=$5 ,os=$6 WHERE id=$7";
const values = [name, price, brand, ram, rom, os, id];
client.query(updateQuery, values, function (err, result) {
if (err) {
console.error("Error while updating data:", err);
res.status(500).send("An error occurred while updating data.");
} else {
console.log(result);
res.send(`${result.rowCount} Updated Successful`);
}
});
}
}
});
}
});
app.delete("/svr/mobile/del/:id", function (req, res) {
const id = +req.params.id;
console.log(id);
if (id) {
const deleteQuery = "DELETE FROM mobile WHERE id = $1";
client.query(deleteQuery, [id], function (err, result) {
if (err) {
res.status(404).send("Error while deleting employee.");
} else {
res.send(
"Successfully deleted employee. Affected rows: " + result.rowCount
);
}
});
} else {
res.status(400).send("Invalid employee ID.");
}
});