-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
201 lines (156 loc) · 4.63 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
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
197
198
199
200
201
const { faker, tr } = require('@faker-js/faker');
const mysql = require("mysql2");
const { v4: uuidv4 } = require('uuid');
const express = require("express");
const app = express();
const path = require("path");
const methodOverride = require("method-override");
app.use(methodOverride("_method"));
app.set("views engine", "ejs");
app.set("views", path.join(__dirname, "/views"));
app.use(express.urlencoded({ extended: true }));
app.use(express.json());
const port = 3030;
const connection = mysql.createConnection({
host: 'localhost',
user: 'root',
database: 'learnsql',
password: 'ranajihariom@1'
});
//GET - fetch & show total number of users our app
app.get('/', (req, res) => {
let query = `select count(*) from user`;
try {
connection.query(query, (error, result) => {
if (error) {
res.send(error);
}
console.log("query is working properly");
console.log(result[0]["count(*)"]);
// let {count} = result[0]["count(*)"];
// res.render("home.ejs",{count});
let count = result[0]["count(*)"];
console.log(result[0]["id"])
res.render("home.ejs", { count });
})
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
//show user
app.get('/user', (req, res) => {
let query = `select * from user`;
try {
connection.query(query, (error, users) => {
if (error) {
throw error;
}
res.render("showUser.ejs", { users });
})
} catch (error) {
console.log(error);
res.sendStatus(500);
}
});
//edit username
app.get('/user/:id/edit', (req, res) => {
let { id } = req.params;
console.log(id);
let query = 'SELECT * FROM user WHERE id = ?';
try {
connection.query(query, [id], (error, result) => {
if (error) {
console.log(error);
res.sendStatus(500); // Added to handle errors properly
} else {
res.render("edit.ejs", { user: result[0] });
console.log(result);
}
});
} catch (error) {
res.sendStatus(500);
}
});
//update route
app.patch('/user/:id', (req, res) => {
let { id } = req.params;
let q = `select * from user where id = '${id}'`;
try {
connection.query(q, (error, result) => {
if (error) {
res.sendStatus(500);
console.log("error occured");
return;
}
let user = result[0];
let { username: newUsername, password: formpassword } = req.body;
if (formpassword != user.password) {
res.send("wrong password entered.Try again!");
} else {
let q2 = `update user set username = '${newUsername}' where id= '${id}'`;
connection.query(q2, (error, result) => {
if (error) {
res.send(error);
console.log(error);
return;
}
res.redirect('/user');
})
}
})
} catch (error) {
console.log(error);
res.sendStatus(404);
}
});
//view user
app.get('/user/:id/data', (req, res) => {
let { id } = req.params;
let query = `select * from user where id = '${id}'`;
try {
connection.query(query, (error, result) => {
if (error) {
res.send("error occured");
return;
}
let data = result[0];
console.log(data);
res.render("data.ejs", { data });
})
} catch (error) {
console.log(error);
res.sendStatus(404);
}
})
//delete user
app.delete('/user/:id', (req, res) => {
let { id } = req.params;
let query = `delete * from user where id = '${id}'`;
try {
connection.query(query, (error, result) => {
if (error) {
res.send("unable to delete");
return;
}
let user = result[0];
res.send(`user ${user.username} deleted`);
console.log(user);
})
} catch (error) {
res.send(error);
console.log(error);
}
})
app.listen(port, () => {
console.log(`server is listening to the port : ${port}`);
});
// let createRandomUser = () => {
// return [
// faker.datatype.uuid(),
// faker.internet.userName(),
// faker.internet.email(),
// faker.internet.password(),
// ];
// }
// connection.end();