-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
152 lines (139 loc) · 3.68 KB
/
Copy pathserver.js
File metadata and controls
152 lines (139 loc) · 3.68 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
'use strict'
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(express.static('static'));
app.use(bodyParser.json());
const MongoClient = require('mongodb').MongoClient;
// LOOK OVER THIS CODE
//I changed records to assets, but im unsure how to test to see if its working.
app.get('/api/SaveMe', (req, res) => {
db.collection('SaveMe').find().toArray().then(issues => {
const metadata = {
total_count: issues.length,
bank: 0
};
res.json({
_metadata: metadata,
assets: issues
})
}).catch(error => {
console.log(error);
res.status(500).json({
message: `Internal Server Error: ${error}`
});
});
});
app.post('/api/SaveMe', (req, res) => {
const newIssue = req.body;
newIssue.created = new Date();
if (!newIssue.status)
newIssue.status = 'New';
// we do not need this as of now, posibly implement a valid entry later
// const err = validateIssue(newIssue);
//if (err) {
// res.status(422).json({ message: `Invalid request: ${err}` });
// return;
// }
db.collection('SaveMe').insertOne(newIssue).then(result =>
db.collection('SaveMe').find({
_id: result.insertedId
}).limit(1).next()
).then(newIssue => {
res.json(newIssue);
}).catch(error => {
console.log(error);
res.status(500).json({
message: `Internal Server Error: ${error}`
});
});
});
app.post('/api/SaveAll', (req, res) => {
console.log("start-----------------")
let list = req.body;
console.log("List: ", list);
let ending = []
for(let i = 0; i < list.length; i++){
console.log("Run ", i, list[i].category)
db.collection('SaveMe').updateOne({category: list[i].category},
{$set: {category: list[i].category,
budget: list[i].budget,
flow: list[i].flow}}).then(ans => {
if(ans.result.ok && ans.result.n){
console.log("Accepted ", list[i].category);
ending.push(list[i])
}else{
console.log("Rejected")
}
}).catch (error => {
console.log("SAVEALL ERROR")
res.status(500).json({
message: `Internal Server Error: ${error}`
})
})
}
setTimeout(() => {
console.log("ENDING:", ending)
if(ending.length > 0){
res.json({
failure: "False",
results: ending
})
}else{
res.json({
failure: "TRUE",
results: ending
})
}
}, 500)
})
app.post('/api/Money', (req, res) => {
const cash = req.body.money;
if(isNaN(cash)){
console.log("NOT NUMBER")
return;
}
console.log("Updating without filter")
db.collection('Money').updateOne({}, { $set: {money: cash}}).then(result => {
res.json({
success: true,
money: cash
})
}).catch(err => err);
})
app.get('/api/Money', (req, res) => {
db.collection('Money').find().toArray().then(info => {
if(info.length == 0){
console.log("QUICKFIX ACTIVATED => MONEY SET TO 0 IN CASE OF NO INFO")
db.collection('Money').insertOne({
money: 0
})
info = db.collection('Money').find().toArray()
}
res.json({
money: info[0].money
})
}).catch(error => {
console.log(error);
res.status(500).json({
message: `Internal Server Error: ${error}`
});
});
})
//
//old code
//app.listen(3000, function () {
// console.log('App started on port 3000');
//});
//I changed the db to SaveMe
let db;
MongoClient.connect('mongodb://localhost', {
useNewUrlParser: true
}).then(connection => {
db = connection.db('SaveMe');
app.listen(3000, () => {
console.log('App started on port 3000');
});
}).catch(error => {
console.log('ERROR:', error);
});