Skip to content

Commit 0536088

Browse files
committed
profile section: user data fetching | middleware
0 parents  commit 0536088

File tree

130 files changed

+12367
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+12367
-0
lines changed

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
please fork this repo
2+
3+
please clone this repo
4+
5+
git clone https://github.com/Forwardkite/Buyying.git
6+
7+
note: clone this repo in the empty or home or Desktop dir
8+
9+
make another branch and push new branches
10+
11+
note: please clone the dir with the both frontend and backend dir

backend/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
node_modules/
2+
.env

backend/README.md

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
server: localhost:5000/
2+
3+
route for batch creation form :- localhost:5000/admin
4+
5+
____________________________________________________________________
6+
7+
Example: suppose created Batch name was = testing
8+
9+
route for displaying created batch datas:- localhost:5000/admin/testing
10+
11+
ie, localhost:5000/admin/created-batch-name
12+
13+
______________________________________________________________________
14+
15+
registration Page
16+
17+
localhost:5000/registration
18+
19+
Login
20+
21+
localhost:5000/login
22+
23+
24+
_______________________________________________________________________
25+
26+
27+
<h1>ACTIONS</h1>
28+
29+
<h3>NODE</h3>
30+
31+
machine must have node v15 or more
32+
33+
machine must have mongodb with a latest stable version
34+
35+
machine must have to install dependencies:
36+
37+
38+
npm i randomatic bcrypt express-session express

backend/app.js

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
const createError = require("http-errors");
2+
const express = require("express");
3+
const path = require("path");
4+
const cookieParser = require("cookie-parser");
5+
const logger = require("morgan");
6+
// const session = require("express-session");
7+
const bcrypt = require("bcryptjs");
8+
const connectionDB = require("./config/connection");
9+
const cors = require("cors");
10+
const passport = require("./controllers/passport");
11+
12+
const app = express();
13+
connectionDB();
14+
15+
16+
17+
app.use(cors({
18+
origin: ["http://localhost:3000"],
19+
methods: ["POST", "GET", "DELETE"],
20+
credentials: true
21+
}));
22+
23+
/*_________________________________________VIEW ENGINE SETUP________________________________________*/
24+
25+
app.set("views", path.join(__dirname, "views"));
26+
app.set("view engine", "hbs");
27+
28+
/*____________________________________________________________________________________________________*/
29+
30+
app.use(logger("dev"));
31+
app.use(express.json());
32+
app.use(express.urlencoded({ extended: false }));
33+
app.use(cookieParser());
34+
app.use(express.static(path.join(__dirname, "public")));
35+
app.use(passport.initialize());
36+
37+
/*__________________________________________________*ADMIN_ROUTES*____________________________________________*/
38+
39+
const adminRouter = require("./routes/admin");
40+
app.use("/admin", adminRouter);
41+
app.use("/uploads", express.static("uploads"));
42+
43+
/*__________________________________________________*ROUTES*____________________________________________*/
44+
45+
const indexRouter = require("./routes/index");
46+
const usersRouter = require("./routes/users");
47+
const registrationRoutes = require("./routes/registration");
48+
const productsRoutes = require("./routes/products");
49+
const loginRoutes = require("./routes/login");
50+
const dashboardRoutes = require("./routes/dashboard");
51+
const logoutRoute = require("./routes/logoutRoute")
52+
53+
app.use("/", indexRouter);
54+
app.use("/registration", registrationRoutes);
55+
app.use("/logout",logoutRoute);
56+
app.use("/login", loginRoutes);
57+
app.use("/users", usersRouter);
58+
app.use("/products", productsRoutes);
59+
app.use("/dashboard", dashboardRoutes);
60+
// app.use('/api',routesMiddleware);
61+
62+
/*_________________________________________________ERROR HANDLERS____________________________________________*/
63+
64+
// catch 404 and forward to error handler
65+
app.use(function (req, res, next) {
66+
next(createError(404));
67+
});
68+
69+
// error handler
70+
app.use(function (err, req, res, next) {
71+
// set locals, only providing error in development
72+
res.locals.message = err.message;
73+
res.locals.error = req.app.get("env") === "development" ? err : {};
74+
75+
// render the error page
76+
res.status(err.status || 500);
77+
res.render("error");
78+
});
79+
80+
/*______________________________________________________________________________________________________________*/
81+
82+
module.exports = app;

backend/bin/www

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Module dependencies.
5+
*/
6+
7+
var app = require('../app');
8+
var debug = require('debug')('backend:server');
9+
var http = require('http');
10+
11+
/**
12+
* Get port from environment and store in Express.
13+
*/
14+
15+
var port = normalizePort(process.env.PORT || '5000');
16+
app.set('port', port);
17+
18+
/**
19+
* Create HTTP server.
20+
*/
21+
22+
var server = http.createServer(app);
23+
24+
/**
25+
* Listen on provided port, on all network interfaces.
26+
*/
27+
28+
server.listen(port);
29+
server.on('error', onError);
30+
server.on('listening', onListening);
31+
32+
/**
33+
* Normalize a port into a number, string, or false.
34+
*/
35+
36+
function normalizePort(val) {
37+
var port = parseInt(val, 10);
38+
39+
if (isNaN(port)) {
40+
// named pipe
41+
return val;
42+
}
43+
44+
if (port >= 0) {
45+
// port number
46+
return port;
47+
}
48+
49+
return false;
50+
}
51+
52+
/**
53+
* Event listener for HTTP server "error" event.
54+
*/
55+
56+
function onError(error) {
57+
if (error.syscall !== 'listen') {
58+
throw error;
59+
}
60+
61+
var bind = typeof port === 'string'
62+
? 'Pipe ' + port
63+
: 'Port ' + port;
64+
65+
// handle specific listen errors with friendly messages
66+
switch (error.code) {
67+
case 'EACCES':
68+
console.error(bind + ' requires elevated privileges');
69+
process.exit(1);
70+
break;
71+
case 'EADDRINUSE':
72+
console.error(bind + ' is already in use');
73+
process.exit(1);
74+
break;
75+
default:
76+
throw error;
77+
}
78+
}
79+
80+
/**
81+
* Event listener for HTTP server "listening" event.
82+
*/
83+
84+
function onListening() {
85+
var addr = server.address();
86+
var bind = typeof addr === 'string'
87+
? 'pipe ' + addr
88+
: 'port ' + addr.port;
89+
debug('Listening on ' + bind);
90+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
const randomatic = require('randomatic');
2+
3+
//_________Generate a random alphanumeric token of length 6________//
4+
5+
function LotteryGenerator() {
6+
const lottery = randomatic('A0', 6);
7+
console.log('Random token:', lottery);
8+
return lottery
9+
}
10+
11+
module.exports = LotteryGenerator
12+
13+
// const randomatic = require('randomatic');
14+
15+
// // Generate a random alphanumeric token of length 6
16+
// function LotteryGenerator() {
17+
// return randomatic('A0', 6);
18+
// }
19+
20+
// module.exports = LotteryGenerator;

backend/config/connection.js

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// const mongoose = require("mongoose");
2+
3+
// const connectionDB = async () => {
4+
// try {
5+
// await mongoose.connect(
6+
// process.env.MONGODB_URI,
7+
// {}
8+
// );
9+
10+
// console.log("MongoDB connected successfully!");
11+
// } catch (error) {
12+
// console.error("Error connecting to MongoDB:", error.message);
13+
// }
14+
// };
15+
16+
// module.exports = connectionDB;
17+
18+
const mongoose = require("mongoose");
19+
require('dotenv').config(); // Load environment variables from .env file
20+
21+
const connectionDB = async () => {
22+
try {
23+
await mongoose.connect(
24+
process.env.MONGODB_URI, // Use environment variable for connection URL
25+
{}
26+
);
27+
28+
console.log("MongoDB connected successfully!");
29+
} catch (error) {
30+
console.error("Error connecting to MongoDB:", error.message);
31+
}
32+
};
33+
34+
module.exports = connectionDB;
35+
36+
37+
// 97fvTcy1zllzpUcY
38+
// I7nfqAXi6kTt518y
39+
// 0WnAhdaletmY6FAF
40+
// XORJNaCZ0pM63gJU
41+
// VDQ0UXJE8kPVFl8s
42+
//mongodb+srv://sreeraj:[email protected]/?retryWrites=true&w=majority//
43+
// mongodb+srv://sreerajmack7:[email protected]/?retryWrites=true&w=majority
44+
//mongodb+srv://abhinavshyjupc:[email protected]/?retryWrites=true&w=majority
+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
// Import the ProductDB model
3+
const Slot = require('../models/slotDB');
4+
5+
// Function to handle '/admin/view' route
6+
const displaySlotData = async (req, res) => {
7+
try {
8+
// Retrieve all products from the database
9+
const slots = await Slot.find({});
10+
11+
// Send the retrieved products as a response
12+
res.json(slots);
13+
} catch (error) {
14+
console.error("Error in Server:", error);
15+
res.status(500).json({ error: "Internal Server Error" });
16+
}
17+
};
18+
19+
module.exports = {
20+
displaySlotData
21+
};
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
2+
// Import the ProductDB model
3+
const User = require('../models/usersDB');
4+
5+
// Function to handle '/admin/view' route
6+
const displayUserData = async (req, res) => {
7+
try {
8+
// Retrieve all products from the database
9+
const UserData = await User.find({});
10+
11+
// Send the retrieved products as a response
12+
res.json(UserData);
13+
} catch (error) {
14+
console.error("Error in Server:", error);
15+
res.status(500).json({ error: "Internal Server Error" });
16+
}
17+
};
18+
19+
const displayUserDataById = async (req, res) => {
20+
try {
21+
const UserId = req.params.id;
22+
const UserDataById = await User.findById(UserId);
23+
24+
if (!UserDataById) {
25+
return res.status(404).json({ error: 'Product not found' });
26+
}
27+
28+
res.json(UserDataById);
29+
} catch (error) {
30+
console.error(error);
31+
res.status(500).json({ error: 'Internal Server Error' });
32+
}
33+
};
34+
35+
module.exports = {
36+
displayUserData,
37+
displayUserDataById
38+
};
+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// controllers/exportController.js
2+
3+
const { ProductDB } = require('../models/productDB'); // Import your ProductDB model or relevant database logic
4+
5+
const exportDataAsCSV = async (req, res) => {
6+
try {
7+
const purchases = await ProductDB.find({}, '-_id -__v');
8+
if (purchases.length === 0) {
9+
return res.status(404).json({ message: 'No data found' });
10+
}
11+
12+
const csvFields = Object.keys(purchases[0].toJSON());
13+
const csvData = [
14+
csvFields.join(','),
15+
...purchases.map((purchase) =>
16+
csvFields.map((field) => purchase[field]).join(',')
17+
),
18+
];
19+
const csv = csvData.join('\n');
20+
21+
res.setHeader('Content-Type', 'text/csv');
22+
res.setHeader('Content-Disposition', 'attachment; filename=purchase_data.csv');
23+
24+
res.status(200).send(csv);
25+
} catch (err) {
26+
console.error(err);
27+
res.status(500).json({ message: 'Server Error' });
28+
}
29+
};
30+
31+
module.exports = {
32+
exportDataAsCSV,
33+
};

0 commit comments

Comments
 (0)