Skip to content

Commit

Permalink
configurações iniciais para upload de arquivos
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 10, 2023
1 parent d9a6796 commit bc0164d
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 0 deletions.
32 changes: 32 additions & 0 deletions backend/controllers/ExtractStudents.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const exceljs = require('exceljs');

exports.extractStudents = async (req, res) => {
const arquivo = req.file;

const workbook = new exceljs.Workbook();
try {
await workbook.xlsx.load(arquivo.buffer);
const primeiraPlanilha = workbook.worksheets[0];

const nomesColunas = primeiraPlanilha.getRow(1).values;

const dados = [];

for (let rowIndex = 2; rowIndex <= primeiraPlanilha.rowCount; rowIndex++) {
const linha = primeiraPlanilha.getRow(rowIndex);
const dadosLinha = {};
nomesColunas.forEach((nomeColuna, columnIndex) => {
dadosLinha[nomeColuna] = linha.getCell(columnIndex + 1).value;
});
dados.push(dadosLinha);
}


console.log('Dados extraídos:', dados);

res.json({ mensagem: 'Arquivo Excel recebido e processado com sucesso!' });
} catch (erro) {
console.error('Erro ao processar arquivo Excel:', erro);
res.status(500).json({ erro: 'Erro ao processar arquivo Excel.' });
}
};
2 changes: 2 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const learningPathRoute = require('./views/routes/LearningPaths')
const logoutRoutes = require('./views/routes/Users');
const sequelize = require('sequelize');
const deleteLearningPathsRoute = require('./views/routes/LearningPaths');
const extractStudentsRoutes = require('./views/routes/Extract');

require("dotenv").config();
const app = express();
Expand All @@ -18,6 +19,7 @@ app.use('/auth', userRoute);
app.use('/elective', electiveRoute);
app.use('/learningpath', learningPathRoute);
app.use('/api', logoutRoutes);
app.use('/send-file', extractStudentsRoutes);

let test = process.env.DB_USERNAME

Expand Down
10 changes: 10 additions & 0 deletions backend/views/routes/Extract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require('express');
const multer = require('multer');
const router = express.Router();
const extractController = require('../../controllers/ExtractStudents')

const storage = multer.memoryStorage();
const upload = multer({ storage: storage });

router.post("/extract-students", upload.single('arquivo'), extractController.extractStudents);
module.exports = router;
47 changes: 47 additions & 0 deletions frontend/src/pages/SendStudents/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import React, { useState } from 'react';
import axios from 'axios';
import { Container, Flex} from "@chakra-ui/react";
import Header from "../../components/Header/index.js";
import Footer from "../../components/Footer/index.js";
import { ChakraProvider } from "@chakra-ui/react";

import { Link } from "react-router-dom";

const SendStudent = () => {

const [selectedFile, setSelectedFile] = useState(null);

const handleFileChange = (event) => {
setSelectedFile(event.target.files[0]);
}

const handleUpload = () => {
const formData = new FormData();
formData.append('arquivo', selectedFile);

axios.post('http://localhost:3001/send-file/extract-students', formData)
.then(response => {
console.log('Resposta do servidor:', response.data);
})
.catch(error => {
console.error('Erro ao enviar arquivo:', error);
})
}

return (

<ChakraProvider>
<Flex direction="column" minH="100vh">
<Header />
<Container flex="1">
<input type="file" onChange={handleFileChange} />
<button onClick={handleUpload}>Enviar Arquivo</button>
</Container>
<Footer />
</Flex>
</ChakraProvider>

);
};

export default SendStudent;
2 changes: 2 additions & 0 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ExclusionTrilhas from "../pages/ExclusionTrilhas";
import ExclusionEletivas from "../pages/ExclusionEletivas"
import CreateEletivas from "../pages/CreateEletivas";
import CreateTrilhas from "../pages/CreateTrilhas";
import SendStudent from "../pages/SendStudents"

const Private = ({ Item }) => {
const { signed } = useAuth();
Expand All @@ -27,6 +28,7 @@ const RoutesApp = () => {
<Route path="/excluir-eletivas" element={<ExclusionEletivas />} />
<Route path="/criar-trilha" element={<CreateTrilhas />} />
<Route path="/excluir-trilhas" element={<ExclusionTrilhas />} />
<Route path="/cadastrar-estudantes" element={<SendStudent />} />
</Routes>
</Fragment>
</BrowserRouter>
Expand Down
6 changes: 6 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"dependencies": {
"exceljs": "^4.4.0",
"multer": "^1.4.5-lts.1"
}
}

0 comments on commit bc0164d

Please sign in to comment.