-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.js
More file actions
132 lines (115 loc) · 3.78 KB
/
db.js
File metadata and controls
132 lines (115 loc) · 3.78 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
const mongoose = require('mongoose');
let adminPassword = '123';
// mongodb+srv://nitingupta9935:9115275119@cluster0.ci83y.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
// mongoose.connect('mongodb://localhost/Renting')
mongoose.connect('mongodb+srv://nitingupta9935:9115275119@cluster0.ci83y.mongodb.net/myFirstDatabase?retryWrites=true&w=majority')
.then(() => console.log('Connected to MongoDB'))
.catch(err => console.error('Could not connect to Database', err));
const loginSchema = new mongoose.Schema({
firstName: String,
lastName: String,
name: String,
email: String,
password: String,
married: Boolean,
people: Number,
job: String,
photo: String,
id: String,
vichele: String,
number: Number
});
const UserData = mongoose.model('UserData', loginSchema);
class DB {
async signUp(firstName, lastName, email, password, married, people, job, photo, id, vichele, number) {
let name = firstName + ' ' + lastName;
let userData = new UserData({
firstName,
lastName,
name,
email,
password,
married,
people,
job,
photo,
id,
vichele,
number
});
await userData.save();
userData = await UserData.find();
for (let i = 0; i < userData.length; i++){
if (userData[i].email === email)
return userData[i]._id;
}
};
async getName(_id) {
const userData = await UserData.findById(_id);
return userData.name;
}
async isEmailExist(email) {
const userData = await UserData.find();
for (let i = 0; i < userData.length; i++){
if (userData[i].email === email)
return true;
}
return false;
}
async isPasswordExist(email, password) {
const userData = await UserData.find();
for (let i = 0; i < userData.length; i++){
if (userData[i].email === email && userData[i].password === password)
return true;
}
return false;
}
async login(email, password) {
const userData = await UserData.find();
for (let i = 0; i < userData.length; i++){
if (userData[i].email === email && userData[i].password === password)
return userData[i]._id;
}
}
async reserPassword(email, password) {
const userData = await UserData.findOneAndUpdate({ email: email }, { password: password });
// return false;
}
async isAdminPasswordCorrect(password) {
// const userData = await UserData.findById("620d1755663565745a07c64d");
// console.log(userData);
// console.log(userData._id);
// console.log(userData.admin);
if (adminPassword === password) {
return true;
}
return false;
}
async getUserData() {
return await UserData.find();
}
async getUserDataById(_id) {
return await UserData.findById(_id);
}
async update(_id ,firstName, lastName, email, password, isMarried, people, job, photo, id, vichele, number) {
const userData = await UserData.findById(_id);
userData.firstName = firstName;
userData.lastName = lastName;
userData.email = email;
userData.password = password;
userData.isMarried = isMarried;
userData.people = people;
userData.job = job;
if (photo !== '')
userData.photo = photo;
if (id !== '')
userData.id = id;
userData.vichele = vichele;
userData.number = number;
await userData.save();
}
async delete(_id) {
await await UserData.findByIdAndDelete(_id);
}
}
module.exports = DB;