Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixes the account balance section #27

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,19 @@ app.use(express.json());

// Method override
app.use(
methodOverride(function (req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method;
}
})
methodOverride(function(req, res) {
if (req.body && typeof req.body === "object" && "_method" in req.body) {
// look in urlencoded POST bodies and delete it
let method = req.body._method;
delete req.body._method;
return method;
}
})
);

// Logging
if (process.env.NODE_ENV === "development") {
app.use(morgan("dev"));
app.use(morgan("dev"));
}

// EJS
Expand All @@ -60,12 +60,12 @@ app.use(expressLayouts);

// Sessions
app.use(
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
session({
secret: "keyboard cat",
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection }),
})
);

// Passport middleware
Expand All @@ -86,6 +86,6 @@ const PORT = process.env.PORT || 3000;

// Server Listening
app.listen(
PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`)
);
PORT,
console.log(`Server running in ${process.env.NODE_ENV} mode on port ${PORT}`)
);
16 changes: 16 additions & 0 deletions models/Transaction.js
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
// Adding / Spending Balance History
const uuid = require("uuid");
const { random, set } = require("lodash");

function uniqueID() {
random_number = [];
for (let i = 0; i < 1000000; i++) {
random_number.push(Math.random()) // math.random .to fixed(13)// depends decimal default 16

}
if (i === 1000000)
console.log("before checking duplicate")
console.log(random_number.length)
console.log("After checking duplicate ")
random_set = new set(random_number)
console.log([...random_set].length) // the length is still same
}
73 changes: 45 additions & 28 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,50 @@ const mongoose = require("mongoose");
var Float = require("mongoose-float").loadType(mongoose);

const UserSchema = new mongoose.Schema({
googleId: {
type: String,
required: true,
},
displayName: {
type: String,
required: true,
},
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
image: {
type: String,
},
balance: {
type: Float,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
googleId: {
type: String,
required: true,
},
displayName: {
type: String,
required: true,
},
firstName: {
type: String,
required: true,
},
lastName: {
type: String,
required: true,
},
image: {
type: String,
},
balance: {
type: Float,
required: true,
},
createdAt: {
type: Date,
default: Date.now,
},
});

module.exports = mongoose.model("User", UserSchema);
UserSchema.method.findSimilarType = function addAmount(id, balance) {

account.id = id; // id of the person
account.balance = balance; // balance in the account

account.deposit = function addAmount(depositAmount) {
newBalance = account.balance - depositAmount; // new balance which is current amount + added amout
console.log("Your balance is now " + newBalance);
if (newBalance <= 0) {
console.log("You have insufficient funds!!!"); // if person don't have balance
}
};
console.log("id: " + id + "balance: " + balance); //to dispaly the final balance with the person id
}



module.exports = mongoose.model("User", UserSchema);
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions routes/api/addBalance.js
Original file line number Diff line number Diff line change
@@ -1 +1,18 @@
// Add Balance Route
const express = require("express");
const router = express.Router();
const { ensureAuth, ensureGuest } = require("../../middleware/auth");

// @desc Login/Landing page
// @route GET /
// @access Public
router.get("/", ensureGuest, (req, res) => {
res.status(200).render("addaccount/add", {
viewTitle: "Add Balance"
});
});
router.post('/', ensureGuest, (req, res) => {
console.log('added');
});

module.exports = router;
10 changes: 10 additions & 0 deletions views/addaccount/add.ejs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<h3>{{viewTitle}}</h3>
<form action="/addaccount" method="POST" class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" "> Add amount </label>
<input type="number " class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline " name="add amount " placeholder="add amount " >
</div>
<div class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline " type="submit " onclick="addAmount() ">
ADD
</div>
</form>
27 changes: 11 additions & 16 deletions views/layouts/app.ejs
Original file line number Diff line number Diff line change
@@ -1,26 +1,21 @@
<!DOCTYPE html>
<html lang="en">
<head>

<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css"
integrity="sha256-46r060N2LrChLLb5zowXQ72/iKKNiw/lAmygmHExk/o="
crossorigin="anonymous"
/>
<link
href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css"
rel="stylesheet"
/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.12.0-2/css/all.min.css" integrity="sha256-46r060N2LrChLLb5zowXQ72/iKKNiw/lAmygmHExk/o=" crossorigin="anonymous" />
<link href="https://unpkg.com/tailwindcss@^1.0/dist/tailwind.min.css" rel="stylesheet" />
<link rel="stylesheet" href="css/style.css" />
<title>TradeByte</title>
</head>
</head>

<body>
<body>
<div class="grid md:grid-cols-5">
<%- include('../partials/_nav.ejs') %> <%- body %>
<%- include('../partials/_nav.ejs') %>
<%- body %>
</div>
<script src="javascript/script.js"></script>
</body>
</html>
</body>

</html>