forked from cs4241-21a/final_project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdbManager.mongodb.js
More file actions
145 lines (128 loc) · 4.94 KB
/
dbManager.mongodb.js
File metadata and controls
145 lines (128 loc) · 4.94 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
const crypto = require('./crypto');
const MongoDB = require('mongodb');
const ObjectId = require('mongodb').ObjectId
const MongoClient = MongoDB.MongoClient;
const url = 'mongodb+srv://kezhao:Zhaoke328@cluster0.2zapp.mongodb.net/myFirstDatabase?retryWrites=true&w=majority';
//mongodb+srv://' + process.env.DB_USER + ':' + process.env.DB_PASS + '@a3.8ty2i.mongodb.net/myFirstDatabase?retryWrites=true&w=majority
const client = new MongoClient(url, { useNewUrlParser: true, useUnifiedTopology: true });
const connectClient = function () {
return new Promise(resolve => {
resolve(client.connect())
})
};
const DB = function () {
return new Promise(resolve =>
connectClient().then(client => resolve(client.db('Scheduler')))
);
};
// DB().then(db => {
// db.createCollection('content');
// db.createCollection('users');
// });
const ContentCollection = function () {
return new Promise(resolve =>
DB().then(db =>
resolve(db.collection('content'))));
};
const UsersCollection = function () {
return new Promise(resolve =>
DB().then(db =>
resolve(db.collection('users'))));
};
exports.getAllContent = function () {
return new Promise(resolve => {
ContentCollection().then(collection => {
collection.find().sort({ artist: 1 }).toArray().then(data => resolve(data));
});
})
};
exports.getContentForUser = function (user) {
return new Promise(resolve => {
ContentCollection().then(collection => {
collection.find({ username: user.username }).sort({ artist: 1 }).toArray().then(data => resolve(data));
});
})
};
exports.getContentById = function (_id) {
return new Promise(resolve => {
ContentCollection().then(collection => {
collection.find({ _id: ObjectId(_id) }).toArray().then(data => resolve(data));
});
})
};
exports.getContectByInfo = function (title, artist) {
return new Promise(resolve => {
ContentCollection().then(collection => {
collection.find({title: title, artist: artist}).toArray().then(data => resolve(data))
});
})
};
exports.addOrUpdateContent = function (user, title, artist, coverart, comments, id) {
return new Promise(resolve => {
if (id === undefined || id === '') {
console.log("Adding ", title, " by ", artist, " for ", user.username);
ContentCollection().then(collection => collection.insertOne({
username: user.username,
title: title,
artist: artist,
coverart: coverart,
comments: []
})).then(result => resolve(result));
} else {
console.log('Updating content ', id);
ContentCollection().then(collection => collection.updateOne({ _id: ObjectId(id) }, {
$set: {
title: title,
artist: artist,
coverart: coverart,
comments: comments
}
})).then(result => resolve(result));
}
})
};
exports.deleteContent = function (user, contentID) {
return new Promise(resolve =>
ContentCollection().then(collection => collection.deleteOne({ _id: ObjectId(contentID) })
.then(result => resolve(result))))
};
exports.checkPass = function (username, password) {
return new Promise((resolve, reject) => {
this.getUser(username).then(user => {
console.log("CHECK PASS: Got user document for " + username + ": " + user);
if (user !== null && crypto.compareString(user.password, password)) resolve(user);
else reject();
}).catch(() => reject());
});
};
exports.getUser = function (username) {
return new Promise((resolve, reject) => {
UsersCollection().then(collection =>
collection.findOne({ username: username }).then(user => {
console.log("GET USER: Got user document for " + username + ": " + user);
resolve(user);
if (user === null) reject("User not found!");
}).catch(error => reject(error))
);
})
};
exports.CreateUser = function (username, displayName, password) {
return new Promise((resolve, reject) => {
UsersCollection().then(collection => {
collection.findOne({ username: username }).then(data => {
if (data === null) {
console.log("Creating User: ", username, " with password: ", password);
let passHash = crypto.encrypt(password);
collection.insertOne({
username: username,
displayName: displayName,
password: passHash
}).catch(error => reject(error));
resolve("Account created successfully.");
}
reject("Username is taken.");
}
)
});
});
};