Skip to content

Commit

Permalink
Merge pull request #18 from SV-Winter-BootCamp-Team-C/be/feat/#15
Browse files Browse the repository at this point in the history
fix: ์„ค๋ฌธ ์ƒ์„ฑ ์ˆ˜์ • api ์ˆ˜์ •
  • Loading branch information
ssojungg committed Jan 18, 2024
2 parents aebb58b + 5bf75de commit ff2fcdc
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 203 deletions.
20 changes: 6 additions & 14 deletions backend/controller/surveyCreate.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
const { sequelize } = require('../models');
const { Survey, Question, Choice } = require('../models');

const createSurveyWithQuestionsAndChoices = async (surveyData, res) => {
const createSurveyWithQuestionsAndChoices = async (req, res) => {
const t = await sequelize.transaction();

try {
console.log('Received survey data:', surveyData);

const {
userId,
title,
Expand All @@ -15,14 +13,12 @@ const createSurveyWithQuestionsAndChoices = async (surveyData, res) => {
font,
color,
buttonStyle,
mainImageUrl,
deadline,
questions,
mainImageUrl, // ๋ฉ”์ธ ์ด๋ฏธ์ง€ URL
//imageUrl, // ์งˆ๋ฌธ ์ด๋ฏธ์ง€ URL ๋ฐฐ์—ด
} = surveyData;

console.log('์š”์ฒญ ๋ณธ๋ฌธ ์ˆ˜์‹ :', surveyData);
} = req.body;

// surveyUrl์„ ๋ฏธ๋ฆฌ ์ •์˜
const surveyUrl = `http://yourwebsite.com/survey/placeholder`;

const survey = await Survey.create(
Expand Down Expand Up @@ -64,20 +60,16 @@ const createSurveyWithQuestionsAndChoices = async (surveyData, res) => {
throw new Error('Invalid question type');
}

const questionImageUrl = question.imageUrl;

const newQuestion = await Question.create(
{
surveyId,
type: question.type,
content: question.content,
imageUrl: questionImageUrl, // ์—ฌ๊ธฐ์—์„œ ์‚ฌ์šฉ
imageUrl: question.imageUrl,
},
{ transaction: t },
);

console.log(`questionId: ${newQuestion.id}`);

if (question.choices && question.choices.length > 0) {
for (const choice of question.choices) {
await Choice.create(
Expand All @@ -99,4 +91,4 @@ const createSurveyWithQuestionsAndChoices = async (surveyData, res) => {
}
};

module.exports = { createSurveyWithQuestionsAndChoices };
module.exports = { createSurveyWithQuestionsAndChoices };
63 changes: 18 additions & 45 deletions backend/controller/surveyModify.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// surveyModify.js
const { sequelize } = require('../models');
const { Survey, Question, Choice, Answer } = require('../models');
const { uploadFileToS3 } = require('./imageUpload'); // ํ•„์š”์— ๋”ฐ๋ผ ๊ฒฝ๋กœ๋ฅผ ์กฐ์ •ํ•˜์„ธ์š”

const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
const ModifySurveyWithQuestionsAndChoices = async (req, res) => {
try {
const surveyId = req.params.id;
const {
id: surveyId,
userId,
title,
description,
Expand All @@ -17,10 +15,10 @@ const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
mainImageUrl,
deadline,
questions,
//questionImageFiles, // ์งˆ๋ฌธ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋ฐฐ์—ด
} = surveyData;
} = req.body;

console.log('Request body:', req.body); // ์š”์ฒญ ๋ณธ๋ฌธ ๋กœ๊ทธ ์ฐ๊ธฐ

// ์š”์ฒญํ•˜๋Š” surveyId๊ฐ€ ์—†์„ ๊ฒฝ์šฐ
const survey = await Survey.findByPk(surveyId);
if (!survey) {
return res
Expand All @@ -42,7 +40,6 @@ const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
.json({ message: '์„ค๋ฌธ์ด ์ž ๊ฒจ์žˆ์–ด ์ ‘๊ทผ ๋ฐ ์ˆ˜์ • ๊ถŒํ•œ์ด ์—†์Šต๋‹ˆ๋‹ค.' });
}

// ์ด๋ฏธ ์„ค๋ฌธ์— ๋‹ต๋ณ€์ด ์กด์žฌํ•˜๋Š” ๊ฒฝ์šฐ
for (const question of questions) {
const answersCount = await Answer.count({
where: {
Expand All @@ -59,18 +56,7 @@ const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
}

await sequelize.transaction(async (t) => {
console.log('ํŠธ๋žœ์žญ์…˜ ์‹œ์ž‘');

// ๋ฉ”์ธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ/์—…๋ฐ์ดํŠธ ์ฒ˜๋ฆฌ
let updatedMainImageUrl = mainImageUrl;
if (surveyData.mainImageFile) {
console.log('๋ฉ”์ธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์ค‘');
updatedMainImageUrl = await uploadFileToS3(surveyData.mainImageFile);
console.log('๋ฉ”์ธ ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ ์™„๋ฃŒ: ', updatedMainImageUrl);
}

// ์„ค๋ฌธ ์ •๋ณด ์—…๋ฐ์ดํŠธ
console.log('์„ค๋ฌธ ์ •๋ณด ์—…๋ฐ์ดํŠธ ์ค‘');
await survey.update(
{
title,
Expand All @@ -79,51 +65,35 @@ const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
font,
color,
buttonStyle,
mainImageUrl: updatedMainImageUrl,
mainImageUrl,
deadline,
},
{ transaction: t },
);
console.log('์„ค๋ฌธ ์ •๋ณด ์—…๋ฐ์ดํŠธ ์™„๋ฃŒ');

// ๊ธฐ์กด ์งˆ๋ฌธ๋“ค ์‚ญ์ œ ํ›„ ์žฌ์ƒ์„ฑ
console.log('๊ธฐ์กด ์งˆ๋ฌธ ์‚ญ์ œ ์ค‘');
// ๊ธฐ์กด ์งˆ๋ฌธ๋“ค์„ ์‚ญ์ œํ•˜๊ณ  ์ƒˆ๋กœ์šด ์งˆ๋ฌธ๋“ค์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
await Question.destroy({
where: { surveyId: surveyId },
transaction: t,
force: true,
force: true, // ํ•˜๋“œ ์‚ญ์ œ๋ฅผ ์ ์šฉ
});
console.log('๊ธฐ์กด ์งˆ๋ฌธ ์‚ญ์ œ ์™„๋ฃŒ');

for (const question of questions) {
if (
![
'MULTIPLE_CHOICE',
'SUBJECTIVE_QUESTION',
'CHECKBOX',
'DROPDOWN',
].includes(question.type)
) {
throw new Error('Invalid question type');
}

// ์ƒˆ๋กœ์šด ์งˆ๋ฌธ ์ƒ์„ฑ
const imageUrl = question.imageUrl || ''; // ์ด๋ฏธ์ง€ URL์„ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

// ์ƒˆ๋กœ์šด ์งˆ๋ฌธ ์ถ”๊ฐ€
const newQuestion = await Question.create(
{
surveyId,
type: question.type,
content: question.content,
imageUrl: imageUrl,
imageUrl: question.imageUrl,
},
{ transaction: t },
);

console.log(`questionId: ${newQuestion.id}`);
console.log(`imageUrl for questionId ${newQuestion.id}: ${imageUrl}`);

if (question.choices && question.choices.length > 0) {
// ์ฒดํฌ๋ฐ•์Šค, ๋‹ค์ค‘ ์„ ํƒ, ๋“œ๋กญ๋‹ค์šด ์งˆ๋ฌธ์˜ ๊ฒฝ์šฐ ์„ ํƒ์ง€ ์ถ”๊ฐ€
if (
['CHECKBOX', 'MULTIPLE_CHOICE', 'DROPDOWN'].includes(question.type)
) {
for (const choice of question.choices) {
await Choice.create(
{
Expand All @@ -141,10 +111,13 @@ const ModifySurveyWithQuestionsAndChoices = async (surveyData, res) => {
});

const updatedSurvey = await Survey.findByPk(surveyId);

console.log('Survey Update Result:', updatedSurvey);

res.status(200).json({ message: '์„ค๋ฌธ ์ˆ˜์ • ์™„๋ฃŒ', updatedSurvey });
} catch (error) {
console.error('์„ค๋ฌธ ์ˆ˜์ • ์˜ค๋ฅ˜:', error.message);
res.status(500).json({ message: '์„ค๋ฌธ ์ˆ˜์ • ์˜ค๋ฅ˜', error: error.message });
res.status(404).json({ message: '์„ค๋ฌธ ์ˆ˜์ • ์˜ค๋ฅ˜', error: error.message });
}
};

Expand Down
148 changes: 4 additions & 144 deletions backend/routers/surveyRouter.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
const express = require('express');
const router = express.Router();
const multer = require('multer');
const { uploadFileToS3 } = require('../controller/imageUpload');
const surveyController = require('../controller/surveyCreate');
const surveyModifyController = require('../controller/surveyModify');
const surveyAllUserController = require('../controller/formAllUser');
Expand All @@ -16,150 +14,10 @@ const getAnswerController = require('../controller/answerReadByuserId');
const { sendSurveyEmailWithSurveyId } = require('../controller/urlShare');
const getResultController = require('../controller/getResultsByRes');

// Multer ์„ค์ • ๋ฐ ์ดˆ๊ธฐํ™”
const storage = multer.memoryStorage(); // ํŒŒ์ผ์„ ๋ฉ”๋ชจ๋ฆฌ์— ์ €์žฅ
const uploadMiddleware = multer({
storage: storage,
// ํŒŒ์ผ๋งŒ ํ—ˆ์šฉํ•˜๋„๋ก ์„ค์ •
fileFilter: (req, file, cb) => {
if (file.fieldname === 'survey') {
cb(null, false); // JSON ๋ฐ์ดํ„ฐ๋Š” ํŒŒ์ผ์ด ์•„๋‹Œ ๊ฒƒ์œผ๋กœ ์ฒ˜๋ฆฌ
} else {
cb(null, true); // ํŒŒ์ผ์€ ํ—ˆ์šฉ
}
},
});

router.post(
'/',
uploadMiddleware.fields([
{ name: 'mainImageUrl', maxCount: 1 },
{ name: 'imageUrl', maxCount: 10 },
]),
async (req, res) => {
try {
console.log('Request received on / endpoint.');
console.log('req.body:', req.body);

if (!req.body.survey) {
return res.status(400).json({ message: 'No survey data provided' });
}

const parsedData = JSON.parse(req.body.survey);
console.log('Parsed survey data:', parsedData);

const surveyData = parsedData.survey;
console.log('Extracted survey data:', surveyData);

if (!Array.isArray(surveyData.questions)) {
console.log('No questions array in surveyData');
return res
.status(400)
.json({ message: 'Invalid survey data: No questions array' });
}

// ๋ฉ”์ธ ์ด๋ฏธ์ง€ URL ์ฒ˜๋ฆฌ
if (req.files['mainImageUrl'] && req.files['mainImageUrl'][0]) {
surveyData.mainImageUrl = await uploadFileToS3(
req.files['mainImageUrl'][0],
);
}

const imageUploadPromises = surveyData.questions.map(
async (question, index) => {
// ํด๋ผ์ด์–ธํŠธ์—์„œ 'imageUrl[]'์˜ ๋ฐฐ์—ด ํ˜•ํƒœ๋กœ ์ด๋ฏธ์ง€๋ฅผ ๋ณด๋ƒˆ๋‹ค๊ณ  ๊ฐ€์ •
if (req.files['imageUrl'] && req.files['imageUrl'][index]) {
const file = req.files['imageUrl'][index];
question.imageUrl = await uploadFileToS3(file);
}
return Promise.resolve();
},
);

// ๋ชจ๋“  ์ด๋ฏธ์ง€ ์—…๋กœ๋“œ๊ฐ€ ์™„๋ฃŒ๋˜๊ธฐ๋ฅผ ๊ธฐ๋‹ค๋ฆฝ๋‹ˆ๋‹ค.
await Promise.all(imageUploadPromises);

// ์„ค๋ฌธ ์ƒ์„ฑ ๋กœ์ง ํ˜ธ์ถœ
await surveyController.createSurveyWithQuestionsAndChoices(
parsedData.survey, // ์ด ๋ถ€๋ถ„์„ ์ˆ˜์ •ํ•ฉ๋‹ˆ๋‹ค.
res,
);
} catch (error) {
console.error('Error processing request:', error);
res.status(400).json({
message: 'Error processing survey data',
error: error.message,
});
}
},
);

router.post('/', surveyController.createSurveyWithQuestionsAndChoices);
router.get('/:userId/answers/:surveyId', getAnswerController.getAnswerByuserId);
router.put(
'/:id',
uploadMiddleware.fields([
{ name: 'mainImageUrl', maxCount: 1 },
{ name: 'imageUrl', maxCount: 10 },
]),
async (req, res) => {
try {
console.log('Request received on PUT endpoint.');
console.log('req.body:', req.body);

if (!req.body.survey) {
return res.status(400).json({
message: '์„ค๋ฌธ์„ ์ˆ˜์ •ํ•˜๋Š”๋ฐ ์‹คํŒจํ•˜์˜€์Šต๋‹ˆ๋‹ค.',
resultCode: 400,
});
}

const parsedData = JSON.parse(req.body.survey);
console.log('Parsed survey data:', parsedData);

const surveyData = {
...parsedData.survey,
id: req.params.id, // surveyId ์ถ”๊ฐ€
};
console.log('Extracted survey data:', surveyData);

// ํด๋ผ์ด์–ธํŠธ๋กœ๋ถ€ํ„ฐ ๋ฐ›์€ ํŒŒ์ผ ๋ฐ์ดํ„ฐ๋ฅผ ์ฒ˜๋ฆฌ
if (req.files['mainImageUrl'] && req.files['mainImageUrl'][0]) {
const mainImageFile = req.files['mainImageUrl'][0]; // ํŒŒ์ผ ๊ฐ์ฒด ์ฐธ์กฐ
console.log('๋ฉ”์ธ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ ์ค‘');
// ํŒŒ์ผ์„ S3์— ์—…๋กœ๋“œํ•˜๊ณ  URL์„ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
const updatedMainImageUrl = await uploadFileToS3(mainImageFile);
console.log('๋ฉ”์ธ ์ด๋ฏธ์ง€ ํŒŒ์ผ ์—…๋กœ๋“œ ์™„๋ฃŒ: ', updatedMainImageUrl);
// ์—…๋กœ๋“œ๋œ ์ด๋ฏธ์ง€ URL์„ surveyData์— ํ• ๋‹นํ•ฉ๋‹ˆ๋‹ค.
surveyData.mainImageUrl = updatedMainImageUrl;
}

// ๊ฐœ๋ณ„ ์งˆ๋ฌธ ์ด๋ฏธ์ง€ ์ฒ˜๋ฆฌ
if (surveyData.questions) {
for (const [index, question] of surveyData.questions.entries()) {
if (req.files['imageUrl'] && req.files['imageUrl'][index]) {
const file = req.files['imageUrl'][index];
question.imageUrl = await uploadFileToS3(file);
} else {
question.imageUrl = ''; // ์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ์ „ํ˜€ ์—…๋กœ๋“œ๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
}
}
}

// ์„ค๋ฌธ ์ˆ˜์ • ๋กœ์ง ํ˜ธ์ถœ
await surveyModifyController.ModifySurveyWithQuestionsAndChoices(
surveyData,
res,
);
} catch (error) {
console.error('Error processing request:', error);
res.status(400).json({
message: 'Error processing survey data',
error: error.message,
});
}
},
);

router.put('/:id', surveyModifyController.ModifySurveyWithQuestionsAndChoices);
router.get('/:id/forms', surveyAllUserController.getUserSurveys);
router.get('/:id/join', surveyAnsweredController.surveyAnswered);
router.get('/:id/results', surveyResultController.surveyResult);
Expand Down Expand Up @@ -189,4 +47,6 @@ router.post('/:id/share', async (req, res) => {
});

router.get('/:id/list', getResultController.getResultsByResponses);


module.exports = router;

0 comments on commit ff2fcdc

Please sign in to comment.