Skip to content

Commit d3d88af

Browse files
committedJun 20, 2023
CSV upload
0 parents  commit d3d88af

25 files changed

+5428
-0
lines changed
 

‎.env

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DATABASE_URL=mongodb+srv://ajaykumar:FLrbtKo1UydPJ3FW@auth-project.p6afx7q.mongodb.net/?retryWrites=true&w=majority
2+
SECRET=thescret

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

‎assets/css/footer.css

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
footer {
2+
height: 10vh;
3+
background-color:#ebedf1;
4+
display: flex;
5+
align-items: center;
6+
justify-content: space-between;
7+
gap: 1rem;
8+
color: rgb(102, 152, 124);
9+
padding: 0 10%; }
10+
footer .footer-links {
11+
display: flex;
12+
align-items: center;
13+
justify-content: space-between;
14+
gap: 1rem; }
15+
footer a {
16+
text-decoration: none;
17+
color: rgb(62, 60, 60);
18+
}
19+
footer a:hover {
20+
color: #03a9f4; }
21+
22+
23+
@media only screen and (max-width: 600px) {
24+
footer h4{
25+
display: none;
26+
}
27+
}

‎assets/css/header.css

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
header {
2+
height: 10vh;
3+
background-color: #ebedf1;
4+
color: rgb(125, 159, 139);
5+
display: flex;
6+
align-items: center;
7+
justify-content: space-between;
8+
font-size: 1.5rem;
9+
padding: 0 5%; }
10+
header ul {
11+
display: flex;
12+
gap: 1rem;
13+
align-items: center; }
14+
header li {
15+
display: flex;
16+
gap: 1rem; }
17+
header a {
18+
list-style: none;
19+
color: #0044d4;
20+
background-color: white;
21+
padding: 8px 10px;
22+
text-decoration: none;
23+
border-radius: 5px;
24+
font-size: 1rem;
25+
font-weight: 500; }

‎assets/css/home.css

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
.main{
2+
height: 80vh;
3+
}
4+
5+
.home-container{
6+
display: flex;
7+
flex-direction: column;
8+
align-items: center;
9+
justify-content: center;
10+
color:#0044d4;
11+
}
12+
13+
.home-container form{
14+
display: flex;
15+
justify-content: space-between;
16+
width: 20%;
17+
align-items: center;
18+
margin-bottom: 3rem;
19+
}
20+
21+
input[type="file"] {
22+
display: none;
23+
}
24+
25+
.custom-file-upload {
26+
color: #0044d4;
27+
padding: 7px 14px;
28+
cursor: pointer;
29+
font-size: 1.2rem;
30+
border-radius: 5px;
31+
border: 1px solid #0044d4;
32+
}
33+
34+
.custom-file-upload:hover{
35+
color: white;
36+
background-color: #0044d4;
37+
}
38+
39+
button{
40+
border: none;
41+
outline: none;
42+
padding: 10px 20px;
43+
color: white;
44+
background-color: #0044d4;
45+
font-size: 1.2rem;
46+
border-radius: 5px;
47+
48+
}
49+
50+
button:hover{
51+
background-color: #033092;
52+
53+
}
54+
55+
.uploaded-files{
56+
display: flex;
57+
flex-direction: column;
58+
margin-top: 1rem;
59+
background-color: whitesmoke;
60+
padding: 20px;
61+
font-size: 1.5rem;
62+
border-radius: 5px;
63+
box-shadow: rgba(0, 0, 0, 0.35) 0px 5px 15px;
64+
gap: 1.5rem;
65+
width: 35%;
66+
}
67+
68+
.uploaded-files ul{
69+
margin: 0;
70+
padding: 0;
71+
display: flex;
72+
align-items: center;
73+
justify-content: space-between;
74+
75+
}
76+
77+
.uploaded-files ul a{
78+
text-decoration: none;
79+
color: #0044d4;
80+
}
81+
82+
@media only screen and (max-width: 600px) {
83+
.home-container form{
84+
width: 80%;
85+
margin-bottom: 2rem;
86+
}
87+
88+
.uploaded-files{
89+
font-size: 1.2rem;
90+
width: 75%;
91+
}
92+
}

‎assets/css/layout.css

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
body {
2+
margin: 0;
3+
padding: 0;
4+
position: relative;
5+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
6+
7+
8+
}
9+
10+
.main {
11+
margin: auto 3%;
12+
height: auto; }
13+
14+
.chat-window-app {
15+
right: 60px;
16+
bottom: 50px;
17+
position: fixed;
18+
z-index: 10000;
19+
background-color: #129bf4;
20+
padding: 10px;
21+
height: 25vh; }

‎assets/css/table.css

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
.container{
2+
margin: 1rem 0px;
3+
padding: 1rem;
4+
}
5+
6+
.container h2{
7+
color: #0044d4;
8+
font-size: 1.2rem;
9+
margin-bottom: 20px;
10+
}
11+
12+
.container h2 a{
13+
text-decoration: none;
14+
color: #0044d4;
15+
}
16+
17+
#dataTable_wrapper{
18+
width: 107%;
19+
}
20+
21+
.row{
22+
margin-bottom: 10px;
23+
24+
}
25+
26+
.col-sm-12{
27+
overflow-x: auto;
28+
margin-bottom: 5px;
29+
}
30+
31+
.table{
32+
font-size: 14px;
33+
}

‎assets/images/csv-logo.png

1.4 KB
Loading

‎assets/js/app.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
$(document).ready(function() {
2+
$('#dataTable').DataTable();
3+
});

‎config/middleware.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//middleware to set res locals flash
2+
module.exports.setFlash = (req, res, next) => {
3+
res.locals.flash = {
4+
'success' : req.flash('success'),
5+
'error': req.flash('error')
6+
}
7+
8+
next();
9+
}

‎config/mongoose.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
require('dotenv').config()
2+
const mongoose = require('mongoose');
3+
4+
exports.connectMonggose =()=>{
5+
mongoose.connect(process.env.DATABASE_URL,
6+
{
7+
useNewUrlParser: true
8+
})
9+
.then((e)=>console.log("Connected to Mongodb =>> CSV Upload"))
10+
.catch((e)=>console.log("Not Connect Mongodb"))
11+
}
12+
13+
14+
// const db = mongoose.connection;
15+
// db.on('error', console.error.bind('error in connecting to the db'));
16+
// db.once('open', function(){
17+
// console.log('Successfully connected to the DB');
18+
// })

‎controllers/home_controller.js

+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
const CSVFile = require('../models/mongoose');
2+
const fs = require('fs');
3+
const path = require('path');
4+
const papa = require('papaparse');
5+
6+
7+
//render homepage
8+
module.exports.homePage = async (req, res) => {
9+
let files = await CSVFile.find({});
10+
res.render('home',{
11+
title: 'CSV Upload | Home',
12+
files: files
13+
});
14+
15+
}
16+
17+
//create and parse CSV
18+
module.exports.uploadFile = (req, res) => {
19+
20+
CSVFile.uploadedCSV(req, res, async function(err){
21+
try{
22+
23+
let csvFile = await CSVFile.findOne({name:req.file.originalname});
24+
if(csvFile){
25+
req.flash('error', 'CSV already exists! 😧')
26+
return res.redirect('back');
27+
}
28+
29+
//parsing CSV using papaparse
30+
const CSVFileUP = req.file.path;
31+
const csvData = fs.readFileSync(CSVFileUP, 'utf8');
32+
33+
const conversedFile = papa.parse(csvData, {
34+
header: false
35+
36+
});
37+
38+
//allowing only CSV input type
39+
if(req.file && req.file.mimetype == 'text/csv'){
40+
//inserting the converted JSON to DB
41+
let csvFile = CSVFile.create({
42+
name: req.file.originalname,
43+
file: conversedFile.data
44+
});
45+
req.flash('success', 'CSV uploaded successfully 🤙');
46+
return res.redirect('back');
47+
}else{
48+
req.flash('error', 'only CSV file allowed');
49+
return res.redirect('back');
50+
}
51+
52+
53+
}catch(err){
54+
//cathching errors and rendering common error page in the FE along with notification
55+
console.log("error", err);
56+
req.flash('error', 'something went wrong ☹️');
57+
return res.render('servererror');
58+
59+
60+
}
61+
})
62+
}
63+
64+
//display CSV Data
65+
module.exports.displayCSV = async (req, res) => {
66+
let displayData = await CSVFile.findById(req.params.id);
67+
return res.render('table',{
68+
title: 'CSV Upload | Details',
69+
file: displayData.name,
70+
keys: displayData.file[0],
71+
results: displayData.file
72+
})
73+
};
74+
75+
//delete CSV from DB
76+
module.exports.deleteCSV = async (req, res) => {
77+
let deleteCSV = await CSVFile.findByIdAndDelete(req.params.id);
78+
req.flash('success', 'CSV removed successfully 🤘');
79+
return res.redirect('back');
80+
}

‎index.js

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
require('dotenv').config()
2+
const express = require('express');
3+
const app = express();
4+
// const db = require('./config/mongoose');
5+
const expressLayouts = require('express-ejs-layouts');
6+
const session = require('express-session');
7+
const flash = require('connect-flash');
8+
const customeMW = require('.//config/middleware');
9+
10+
//middleware to use assets
11+
app.use(express.static('./assets'));
12+
app.use(express.urlencoded());
13+
app.use(expressLayouts);
14+
15+
//extract styles and scripts from layouts
16+
app.set('layout extractStyles', true);
17+
app.set('layout extractScripts', true);
18+
19+
20+
//setting view engine as ejs
21+
app.set('view engine', 'ejs');
22+
app.set('views', './views');
23+
24+
// ************************ Database Connection **********************************//
25+
const {connectMonggose} = require('./config/mongoose')
26+
connectMonggose();
27+
28+
29+
//to create an duse sessions
30+
app.use(session({
31+
secret: process.env.SECRET,
32+
saveUninitialized: true,
33+
resave: true
34+
}));
35+
36+
//using connect-flash to display flash notification in FE
37+
app.use(flash());
38+
app.use(customeMW.setFlash);
39+
40+
//router
41+
app.use('/', require('./routes'));
42+
43+
// ************************ Port Start ********************************//
44+
const PORT = process.env.PORT || 8500;
45+
app.listen(PORT,()=>{
46+
console.log(`My server start on this port ${PORT}`)
47+
})
48+

0 commit comments

Comments
 (0)
Please sign in to comment.