-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
121 lines (97 loc) · 3.05 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
const express = require('express');
const mongo = require('mongodb');
const bodyParser = require('body-parser');
const config = require('./mongoConfig');
// var cors = require('cors');
const path = require('path');
const app = express();
// to parse the req body
app.use(bodyParser.urlencoded());
app.use(bodyParser.json());
// Serve static files from the React app
app.use(express.static(path.join(__dirname, 'expense-monitor-client/build')));
// to accept the CORS reqs
// app.use(cors());
var port = process.env.PORT || 3001;
const router = express.Router();
const mongoClient = mongo.MongoClient;
let mdb;
//console.log(config);
mongoClient.connect(config.mongodbConfig.uriAtlas, (err, db) => {
if (err) throw err;
mdb = db.db(config.mongodbConfig.db);
});
/* endpoint - /getExpenses
* to get all income and expense data base on the year
*
*/
router.get('/getExpenses', (req, res) => {
let { username, year } = req.body;
if(!username && !year) {
username = req.query.username;
year = req.query.year;
}
var query = {year : Number(year)};
mdb.collection(username).find(query).toArray(function(err, result) {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: result });
});
});
/* endpoint - /saveExpense
* to save income or expense data to a document based on the year
* without sorting the document
*/
router.post('/saveExpense', (req, res) => {
let body = req.body;
let queryExp = {year : Number(body.year)};
let data;
if (body.logIncome) {
data = body.income;
} else {
data = body.data;
}
data.date = new Date(data.date);
let reqBody = {};
if (body.logIncome) {
reqBody.income = data;
} else {
reqBody.data = data;
}
mdb.collection(body.username).findOneAndUpdate(queryExp, { $addToSet: reqBody }, function (err, result) {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: result });
});
});
/* endpoint - /saveExpenseWithSort
* to save income or expense data to a document baesd on the year.
* and sort the document based on the date in the income or expense
*/
router.post('/saveExpenseWithSort', (req, res) => {
let body = req.body;
let queryExp = {year : Number(body.year)};
let data;
if (body.logIncome) {
data = body.income;
} else {
data = body.data;
}
data.date = new Date(data.date);
let reqBody = {};
if (body.logIncome) {
reqBody.income = {
$each : [data],
$sort : body.sort
};
} else {
reqBody.data = {
$each : [data],
$sort : body.sort
};
}
mdb.collection(body.username).findOneAndUpdate(queryExp, { $push: reqBody }, function (err, result) {
if (err) return res.json({ success: false, error: err });
return res.json({ success: true, data: result });
});
});
app.use('/api', router);
app.listen(port, () => {console.log('LISTENING ON PORT '+port)});