-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
166 lines (147 loc) · 4.55 KB
/
index.js
File metadata and controls
166 lines (147 loc) · 4.55 KB
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
//modules import area
const express = require("express");
const { json } = require("express/lib/response");
const app = express();
const fs = require("fs");
//dummy database import
const db = require("./db.json");
// Just to parse the body "req.body"
app.use(express.json());
///******************************************************************************************** */
// First route start for Create user & Song library
app.post("/", (req, res) => {
const { Email, Password, Song } = req.body;
//for checking all details are given or not
if (!Email || !Password || !Song) {
res.status(409).json({ "massage": "Please insert all fields" })
} else {
let Emailcount = 0;
let count = 0
const isSongExist = db.find((data) => {
if (data.Email === Email) {
Emailcount++;
if (data.Password === Password) {
count++;
if (data.Song === Song) {
return true;
}
}
}
})
if (count > 0) { //check user authentication
// console.log(auth);
if (isSongExist) {
res.status(409).json({ message: "Song already exist in your playlist" });
} else {
db.push({ Email, Password, Song });
//Updating json file
fs.writeFile('./db.json', JSON.stringify(db),
(err) => {
if (err) {
res.status(500).json({ message: err });
}
else {
res.status(200).json({ Email, Password, Song });
}
}
);
}
} else {
// non authanticated users
if (Emailcount > 0) { //check email is exist or not
res.status(401).json({ message: "Enter Correct Credantial" });
} else {
db.push({ Email, Password, Song });
//Updating json file
fs.writeFile('./db.json', JSON.stringify(db),
(err) => {
if (err) {
res.status(500).json({ message: err });
}
else {
res.status(200).json({ "massage": "New User Created", Email, Password, Song });
}
}
);
}
}
}
})
///******************************************************************************************** */
// second route start for display the data
app.get("/", (req, res) => {
const { Email } = req.body;
const tempDB = [];
db.filter((data) => {
if (data.Email === Email) {
tempDB.push({ Song: data.Song });
}
})
if (tempDB.length) {
res.status(200).json(tempDB)
} else res.status(404).json({ "massage": "Data Not Found" })
})
///******************************************************************************************** */
// Third route start for update
app.put("/", (req, res) => {
const { Email, Password, newPassword } = req.body;
if (!Email || !Password) {
res.status(401).json({ massage: "Please insert Email and Password" })
} else {
const auth = db.find((data) => {
if (data.Email === Email) {
if (data.Password === Password) {
return true;
}
}
})
if (auth) {
if (newPassword) {
db.find((data) => { //all password change for corresponding email
if (data.Email === auth.Email) {
data.Password = newPassword
}
});
fs.writeFile('./db.json', JSON.stringify(db),
(err) => {
if (err) {
res.status(500).json({ message: err });
}
else {
res.status(200).json({ "massage": "Password Changed successfully" });
}
}
);
} else {
res.status(400).json({ "massage": "Enter New Password" });
}
} else {
res.status(401).json({ message: "Enter Correct Credantial" });
}
}
})
///******************************************************************************************** */
// fourth route start for Delete
app.delete("/", (req, res) => {
const { Email, Password, Song } = req.body;
let countpwd = 0, countSong = 0;
db.find((data) => {
if (data.Email === Email) {
if (data.Password === Password) {
countpwd++;
if (data.Song === Song) {
countSong++;
let index = db.indexOf(data);
db.splice(index, 1);
fs.writeFile('./db.json', JSON.stringify(db), () => {
res.status(200).json({ "Massage": "Song deleted from your library" })
});
}
}
}
})
if (!countSong) {
res.status(404).json({"massage":"Song not exist in your playlist"})
}
})
app.listen(9000, () => console.log("server is running at port no 9000"))