-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo.js
111 lines (109 loc) · 3.98 KB
/
mongo.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
const MongoClient = require('mongodb');
var url = "mongodb+srv://dietrich-admin:[email protected]/savesCluster?retryWrites=true&w=majority";
exports.sendUser = (obj) => {
MongoClient.MongoClient.connect(url, (err, db) => {
if(err) throw err;
let savesDB = db.db("savesDB");
savesDB.collection("users").insertOne(obj, (err, res) => {
if(err) throw err;
db.close();
});
});
}
exports.sendSave = (obj) => {
MongoClient.MongoClient.connect(url, (err, db) => {
if(err) throw err;
let savesDB = db.db("savesDB");
savesDB.collection("saves").insertOne(obj, (err, res) => {
if(err) throw err;
db.close();
});
});
}
exports.userExists = (username, fn) => {
MongoClient.MongoClient.connect(url, async (err, db) => {
if(err) throw err;
let savesDB = db.db("savesDB")
let query = {username: username};
let cursor = savesDB.collection("users").find(query);
await cursor.forEach(dodo => {rt = dodo;});
if(typeof rt !== 'undefined') {
fn(true);
} else {
fn(false);
}
});
}
exports.getUserSaves = (uname, fn) => {
MongoClient.MongoClient.connect(url, async (err, db) => {
if(err) throw err;
let savesDB = db.db("savesDB")
let query = {username: uname};
savesDB.collection("saves").find(query).toArray((err, result) => {
if(err) throw err;
fn(result);
})
});
}
exports.savesLength = fn => {
MongoClient.MongoClient.connect(url, (err, db) => {
if(err) throw err;
let savesDB = db.db("savesDB");
savesDB.collection("saves").find({}).toArray((err, result) => {
if(err) throw err;
db.close();
fn(result.length)
});
});
}
exports.getSaves = (range, req, res) => {
//[limit, page]
var skippers;
MongoClient.MongoClient.connect(url, (err, db) => {
if(err) throw err;
var dbo = db.db("savesDB");
if(parseInt(range[1]) < 2) {
skip = 0;
} else {
skip = (parseInt(range[1]) - 1) * parseInt(range[0]);
}
if(isNaN(skip) || isNaN(parseInt(range[0]))) {
res.redirect('/')
} else {
dbo.collection("saves").find({}).skip(skip).limit(parseInt(range[0])).toArray((err, result) => {
if(err) throw err
db.close();
if(typeof req.user !== 'undefined') {
exports.getUserSaves(req.user.id, ans => {
if(ans.length > 0) {
exports.savesLength(length => {
var lengthArr = [];
for(i=1;i < length + 1; i++) {
lengthArr.push(i);
}
res.render('index.ejs', {user: req.user, data : result, hasSaves: true, length:lengthArr});
})
} else {
exports.savesLength(length => {
var lengthArr = [];
for(i=1;i < length + 1; i++) {
lengthArr.push(i);
}
res.render('index.ejs', {user: req.user, data : result, hasSaves: false, length:lengthArr});
})
}
})
}
else {
exports.savesLength(length => {
var lengthArr = [];
for(i=1;i < length + 1; i++) {
lengthArr.push(i);
}
res.render('index.ejs', {user : {name:null}, data : result, hasSaves: false, length:lengthArr});
})
}
});
}
});
}