Skip to content

Commit

Permalink
add formidable to parse form data
Browse files Browse the repository at this point in the history
  • Loading branch information
longtran1904 committed Apr 15, 2023
1 parent f0317f3 commit 9098026
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
34 changes: 25 additions & 9 deletions server/api/controllers/submission.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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 @@ -56,15 +58,29 @@ const updateSubmission = async (req, res) => {
};

const convertCSVtoJSON = async (req, res) => {
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');
}
const form = formidable({multiples: true});

form.parse(req, (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');
// }
};

module.exports = {
Expand Down
2 changes: 1 addition & 1 deletion server/api/routers/submission.route.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ router.patch('/:submissionID', updateSubmission);
* requestBody:
* required: true
* content:
* application/csv:
* text/csv:
* tags:
* - submissions
*/
Expand Down

0 comments on commit 9098026

Please sign in to comment.