Skip to content

Commit

Permalink
return JSON info from csv file
Browse files Browse the repository at this point in the history
  • Loading branch information
longtran1904 committed Apr 15, 2023
1 parent 9098026 commit 19a53b8
Show file tree
Hide file tree
Showing 4 changed files with 462 additions and 86 deletions.
30 changes: 12 additions & 18 deletions server/api/controllers/submission.controller.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const formidable = require('formidable');
const {
findSubmissions,
findSubmissionById,
Expand All @@ -6,8 +7,6 @@ const {
} = require('../services/submission.service.js');
const { convertCSV } = require('../helpers/convertCSV');
const { generateSubmission } = require('../../testing/testObjectGenerator.js');
const formidable = require('formidable');


const getSubmissions = async (req, res) => {
const foundUsers = await findSubmissions();
Expand Down Expand Up @@ -58,29 +57,24 @@ const updateSubmission = async (req, res) => {
};

const convertCSVtoJSON = async (req, res) => {
const form = formidable({multiples: true});
const form = formidable({ multiples: true });
let filepath;

form.parse(req, (err, fields, files) => {
form.parse(req, async (err, fields, files) => {
if (err) {
res.writeHead(err.httpCode || 400, { 'Content-Type': 'text/plain' });
res.end(String(err));
return;
}
//TODO: parse csv file to convertCSV
//TODO: return submission model to res
res.writeHead(200, { 'Content-Type': 'application/json' });
res.end(JSON.stringify({ fields, files }, null, 2));
});

//const JSONSubmissions = await convertCSV();
// if (JSONSubmissions) {
// res.status(200).json({
// message: 'Successfully converted submissions',
// submissions: JSONSubmissions,
// });
// } else {
// res.status(500).send('Submissions converting not done correctly');
// }
filepath = files.file.path;
const JSONSubmissions = await convertCSV(filepath);
if (JSONSubmissions) {
res.send(JSON.stringify(JSONSubmissions));
} else {
res.status(500).send('Submissions converting not done correctly');
}
});
};

module.exports = {
Expand Down
4 changes: 2 additions & 2 deletions server/api/helpers/convertCSV.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const csv = require('csv-parser');
const fs = require('fs');
const SubmissionModel = require('../../models/submission.model');

const convertCSV = () => {
const convertCSV = (filepath) => {
const results = [];
return new Promise((resolve, reject) => {
fs.createReadStream('./testing/data/projects-fall-2021.csv')
fs.createReadStream(filepath)
.pipe(csv())
.on('data', (data) => {
const submission = new SubmissionModel();
Expand Down
Loading

0 comments on commit 19a53b8

Please sign in to comment.