This repository was archived by the owner on Dec 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
97 lines (76 loc) · 2.41 KB
/
index.js
File metadata and controls
97 lines (76 loc) · 2.41 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
const express = require('express');
const ejs = require('ejs');
const dotenv = require('dotenv');
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const fakeTransactions = require('./transaction.js');
dotenv.config();
const app = express();
const userArray=[];
// To initialize ejs
app.set("view engine", "ejs");
app.use(bodyParser.urlencoded({extended:true}));
app.use(express.static("public"));
// Database Connection:
mongoose.connect("mongodb+srv://ayush002jha:fldQu5TkJQXNaX1L@cluster0.qaiw30b.mongodb.net/financeDB?retryWrites=true&w=majority");
// Schema for User
const { Decimal128 } = require('mongodb')
const FinanceSchema = mongoose.Schema({
currency: String,
amount: Decimal128,
date: String,
description: String,
sender: String,
recipient: String,
paymentMethod: String
})
const FinanceData = mongoose.model("FinanceData", FinanceSchema)
app.get('/', (req, res) => {
res.render("home");
});
app.get("/dashboard", async (req, res) => {
const initialBalance = 10000000;
const data = await FinanceData.find({});
const totalExpense = await FinanceData.aggregate([
{
$group: {
_id: null,
totalAmount: { $sum: '$amount' }
}
}
]);
const totalExpenditure = totalExpense[0].totalAmount;
res.render("dashboard",{allTransactions:data,totalExpenditure:totalExpenditure, initialBalance:initialBalance, totalExpenditure:totalExpenditure, currentBalance:initialBalance-totalExpenditure});
});
app.get("/about", (req, res) => {
res.render("about");
});
app.get("/login", (req, res) => {
res.render("login");
});
app.get("/signup",(req,res)=>{
res.render("signup");
});
app.post("/add",(req,res)=>{
console.log(req.body);
const newTransaction = FinanceData({
"currency": req.body.currency,
"amount": Number(req.body.amount),
"date": req.body.date,
"description": req.body.desc,
"sender": req.body.send,
"recipient": req.body.recieve,
"paymentMethod": req.body.payment
});
newTransaction.save();
res.redirect("/dashboard");
})
app.post("/delete",async (req,res)=>{
console.log(req.body);
await FinanceData.deleteOne({_id:req.body.deleteID})
res.redirect("/dashboard");
});
// Server is Running on port 3000
app.listen(process.env.PORT || 3000, () => {
console.log('Server is running');
});