This repository has been archived by the owner on Feb 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdonationDB.js
112 lines (109 loc) · 2.85 KB
/
donationDB.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
//jshint esversion:8
const mongoose = require('mongoose');
const ts = require('time-stamp');
const weekday = require('weekday');
//jshint esversion:6
const express = require("express");
const bodyParser = require("body-parser");
const path = require("path");
const app = express();
const cors = require('cors')
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(cors());
// //mongodb
mongoose.connect('mongodb+srv://leo22:[email protected]/donationDB', {
useNewUrlParser: true,
useUnifiedTopology: true
});
let donationSchema = new mongoose.Schema({
senderAccount: String,
totalDonation: Number,
donations: [{
amount: Number,
timeStamp: String,
recipientAccount: String,
url: String
}]
});
//set the name of the collection in portfolioDB=> "visitors"
let donateModel = mongoose.model('donators', donationSchema);
//WRITE TO DB
const writeToDB = async (senderAccount, recipientAccount, amount, url) => {
let res = await donateModel.findOne({
senderAccount: senderAccount
});
if (res == null) {
//create new one if not found
const document = new donateModel({
senderAccount: senderAccount,
totalDonation: amount,
donations: [{
amount: amount,
timeStamp: ts(`YYYY/MM/DD(${weekday()}) HH:mm`),
recipientAccount: recipientAccount,
url: url
}]
});
let res1 = await document.save();
if (res1) {
console.log("created!");
return Promise.resolve(true);
} else {
return Promise.reject(false);
}
} else {
//update if found
let update = {
$set: {
totalDonation: res.totalDonation + amount
},
$push: {
donations: {
amount: amount,
timeStamp: ts(`YYYY/MM/DD(${weekday()}) HH:mm`),
recipientAccount: recipientAccount,
url: url
}
}
};
let res2 = await res.updateOne(update);
console.log("ERR2", res2);
if (res2) {
console.log("updated!");
return Promise.resolve(true);
} else {
return Promise.reject(false);
}
}
};
//READ FROM DB
const readFromDB = async (senderAccount) => {
let res = await donateModel.findOne({
senderAccount: senderAccount
});
if (res) {
return Promise.resolve(res);
} else {
return Promise.reject("no data exist");
}
}
//get request
app.get("/read/:caller", async (req, res) => {
console.log(req.params)
let result = await readFromDB(req.params.caller);
res.json(result);
});
//post request
app.get('/update/sender/:sender/recipient/:recipient/amt/:amt/url/:url',async (req, res) => {
console.log(req.params);
let amount = parseInt(req.params.amt);
await writeToDB(req.params.sender, req.params.recipient, amount, req.params.url);
res.json({'msg':'success'});
});
var port = 5001
app.listen(port, function() {
console.log(port);
});