-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
137 lines (119 loc) · 3.51 KB
/
server.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
const express = require('express');
const cors = require('cors');
const mongoose = require('mongoose');
const { MongoClient } = require('mongodb');
const app = express();
app.use(express.json());
app.use(cors());
const main = async () => {
const uri = "mongodb+srv://Mankind:[email protected]/myFirstDatabase?retryWrites=true&w=majority";
const client = new MongoClient(uri, { useNewUrlParser: true, useUnifiedTopology: true });
// Connect to the MongoDB cluster
await client.connect();
// Make the appropriate DB calls
app.get('/', (req,res) => {
findAll(client)
.then(result => res.json("All set"))
.catch(err => console.error(err))
})
app.post('/register', (req,res) => {
const { username, name, password } = req.body;
if(username === "" || password === "" || name === "" ) {
res.json("Error")
} else {
const newUser = {
name: name,
username: username,
password: password,
entries: 0,
joined: new Date()
}
createUser(client, newUser)
.then(result => {
if (result) {
return res.json({
name: name,
username: username,
entries: 0,
joined: new Date()
});
}
})
}
})
app.post('/signin', (req,res) => {
const { username, password } = req.body;
findOneUserByUsername(client, username, password)
.then(result => {
if(result.name) {
return res.json({
name: result.name,
username: result.username,
entries: result.entries,
joined: result.joined
})
} else {
return res.json(result)
}
})
})
app.put('/imageEntry', (req, res) => {
const { username } = req.body;
let found = false;
updateEntriesByUsername(client, username)
.then(result => {
getIndexByEntries(client, result)
.then(count => {
res.json({entries: result, rank: count})
})
})
})
}
main().catch(console.error)
const createUser = async (client, newUser) => {
const result = await client.db("users").collection("data").insertOne(newUser);
if (result) {
return `New user created with the following id: ${result.insertedId}`;
} else {
return false;
}
}
const findAll = async (client) => {
const result = await client.db("users").collection("data").find({}).toArray();
if (result) {
return result;
} else {
return `No users found`;
}
}
const findOneUserByUsername = async (client, usernameOfUser, passwordOfUser) => {
const result = await client.db("users").collection("data").findOne({ username: usernameOfUser, password: passwordOfUser });
if (result) {
return result;
} else {
return `Username/Password incorrect`;
}
}
const updateEntriesByUsername = async (client, usernameOfUser) => {
const result = await client.db("users").collection("data")
.updateOne({ username: usernameOfUser }, { $inc: {entries: 1} });
if (result) {
const user = await client.db("users").collection("data").findOne({ username: usernameOfUser });
if (user) {
return user.entries;
}
} else {
return "Could not update entries count"
}
}
const getIndexByEntries = async (client, entriesOfUser) => {
const result = await client.db("users").collection("data")
.find({entries: { $gt : entriesOfUser }}).count() + 1;
if (result) {
return result;
} else {
return "Could not count"
}
}
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => console.log(`Server currently running on http://localhost:${PORT}`));