Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
students
survey
Commits
eb70b0c6
Commit
eb70b0c6
authored
Aug 22, 2022
by
Yoon, Daeki
😅
Browse files
Merge branch 'jiwon0820'
parents
f3fc15f0
7275ba42
Changes
6
Show whitespace changes
Inline
Side-by-side
frontend/src/apis/answer.api.ts
View file @
eb70b0c6
...
@@ -13,6 +13,7 @@ export const save = async (answers: IAnswerRequestData[]) => {
...
@@ -13,6 +13,7 @@ export const save = async (answers: IAnswerRequestData[]) => {
};
};
export
const
saveForm
=
async
(
answerForm
:
FormData
)
=>
{
export
const
saveForm
=
async
(
answerForm
:
FormData
)
=>
{
console
.
log
(
"
formdata
"
,
answerForm
);
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/answers/upload`
,
answerForm
);
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/answers/upload`
,
answerForm
);
return
data
;
return
data
;
};
};
...
...
frontend/src/surveys/AnswerSurvey.tsx
View file @
eb70b0c6
...
@@ -20,7 +20,9 @@ export const AnswerSurvey = () => {
...
@@ -20,7 +20,9 @@ export const AnswerSurvey = () => {
const
handleSubmit
=
async
(
e
:
FormEvent
)
=>
{
const
handleSubmit
=
async
(
e
:
FormEvent
)
=>
{
e
.
preventDefault
();
e
.
preventDefault
();
console
.
log
(
"
answers:
"
,
answers
);
console
.
log
(
"
answers:
"
,
answers
);
const
needAnswer
=
answers
.
some
((
answer
)
=>
!
answer
.
requiredCheck
);
const
needAnswer
=
answers
.
some
(
(
answer
)
=>
answer
.
question
.
isRequired
&&
!
answer
.
requiredCheck
);
if
(
needAnswer
)
{
if
(
needAnswer
)
{
alert
(
"
필수질문에 응답하셔야 합니다.
"
);
alert
(
"
필수질문에 응답하셔야 합니다.
"
);
return
;
return
;
...
@@ -46,12 +48,14 @@ export const AnswerSurvey = () => {
...
@@ -46,12 +48,14 @@ export const AnswerSurvey = () => {
formData
.
append
(
"
guestId
"
,
"
guest
"
);
formData
.
append
(
"
guestId
"
,
"
guest
"
);
const
files
:
FileList
=
answer
.
content
;
const
files
:
FileList
=
answer
.
content
;
files
&&
[...
files
].
map
((
f
)
=>
{
[...
files
].
map
((
f
)
=>
{
console
.
log
(
"
파일 없음
"
,
f
);
formData
.
append
(
"
uploadFiles
"
,
f
);
formData
.
append
(
"
uploadFiles
"
,
f
);
});
});
return
formData
;
return
formData
;
});
});
console
.
log
(
"
forms
"
,
forms
);
setError
(
""
);
setError
(
""
);
const
results
=
await
answerApi
.
save
(
const
results
=
await
answerApi
.
save
(
otherAnswers
.
map
((
answer
)
=>
({
otherAnswers
.
map
((
answer
)
=>
({
...
...
frontend/src/surveys/ResultSurvey.tsx
View file @
eb70b0c6
...
@@ -25,9 +25,9 @@ export const ResultSurvey = () => {
...
@@ -25,9 +25,9 @@ export const ResultSurvey = () => {
async
function
getAnswers
()
{
async
function
getAnswers
()
{
try
{
try
{
if
(
surveyId
)
{
if
(
surveyId
)
{
const
survey
=
await
answerApi
.
getAnswers
(
surveyId
);
const
result
=
await
answerApi
.
getAnswers
(
surveyId
);
//
console.log(
survey
);
console
.
log
(
result
);
setSurvey
(
survey
);
setSurvey
(
result
);
}
else
{
}
else
{
setLoading
(
true
);
setLoading
(
true
);
}
}
...
...
frontend/src/surveys/SurveyCard.tsx
View file @
eb70b0c6
...
@@ -22,8 +22,8 @@ export const SurveyCard = ({ survey, handleDelete }: Props) => {
...
@@ -22,8 +22,8 @@ export const SurveyCard = ({ survey, handleDelete }: Props) => {
return
(
return
(
<
div
className
=
"w-40 h-48 md:w-52 md:h-60 rounded border-2 hover:border-2 hover:border-themeColor"
>
<
div
className
=
"w-40 h-48 md:w-52 md:h-60 rounded border-2 hover:border-2 hover:border-themeColor"
>
<
Link
to
=
{
`
${
survey
.
_id
}
`
}
state
=
{
survey
}
className
=
"w-full
pt-1
"
>
<
Link
to
=
{
`
${
survey
.
_id
}
`
}
state
=
{
survey
}
className
=
"w-full"
>
<
p
className
=
"font-bold"
>
<
p
className
=
"font-bold
text-center mt-1.5
"
>
{
survey
.
title
?
survey
.
title
:
"
제목없는 설문조사
"
}
{
survey
.
title
?
survey
.
title
:
"
제목없는 설문조사
"
}
</
p
>
</
p
>
...
@@ -32,7 +32,7 @@ export const SurveyCard = ({ survey, handleDelete }: Props) => {
...
@@ -32,7 +32,7 @@ export const SurveyCard = ({ survey, handleDelete }: Props) => {
{
survey
.
comment
?
survey
.
comment
:
"
설명없는 설문조사
"
}
{
survey
.
comment
?
survey
.
comment
:
"
설명없는 설문조사
"
}
</
p
>
</
p
>
</
div
>
</
div
>
<
p
className
=
"text-gray-500 text-sm"
>
<
p
className
=
"text-gray-500 text-sm
text-center
"
>
{
survey
.
updatedAt
?.
substring
(
0
,
10
)
}
{
survey
.
updatedAt
?.
substring
(
0
,
10
)
}
</
p
>
</
p
>
</
Link
>
</
Link
>
...
...
src/controllers/answer.controller.ts
View file @
eb70b0c6
...
@@ -90,6 +90,7 @@ export const createAnswerWithFile = asyncWrap(async (reqExp, res) => {
...
@@ -90,6 +90,7 @@ export const createAnswerWithFile = asyncWrap(async (reqExp, res) => {
let
fileInfos
;
let
fileInfos
;
const
files
=
formidableFilesToArray
(
req
.
files
.
uploadFiles
);
const
files
=
formidableFilesToArray
(
req
.
files
.
uploadFiles
);
console
.
log
(
"
files=
"
,
files
);
if
(
files
)
{
if
(
files
)
{
fileInfos
=
await
Promise
.
all
(
fileInfos
=
await
Promise
.
all
(
files
.
map
(
async
(
file
)
=>
await
fileDb
.
createFile
(
file
))
files
.
map
(
async
(
file
)
=>
await
fileDb
.
createFile
(
file
))
...
@@ -103,30 +104,41 @@ export const createAnswerWithFile = asyncWrap(async (reqExp, res) => {
...
@@ -103,30 +104,41 @@ export const createAnswerWithFile = asyncWrap(async (reqExp, res) => {
res
.
json
(
newAnswer
);
res
.
json
(
newAnswer
);
});
});
// export const getAnswers = asyncWrap(async (reqExp, res) => {
// const req = reqExp as TypedRequest;
// const { surveyId } = req.params;
// try {
// const survey = await surveyDb.getSurveyById(surveyId);
// const answers = await answerDb.getAnswers(surveyId);
// console.log(answers);
// const jsonSurvey = survey?.toJSON();
// if (jsonSurvey && answers) {
// const a = answers.map(async (a) => {
// const targetObj = jsonSurvey.questions.find(
// (q: any) => String(q._id) === String(a._id)
// ) as any;
// if (targetObj) {
// if (a.file.length) {
// targetObj.answers = a.file;
// } else {
// targetObj.answers = a.answers;
// }
// }
// });
// await Promise.all(a);
// }
// return res.json(jsonSurvey);
// } catch (error: any) {
// res.status(422).send(error.message || "설문조사 결과 불러오기 오류");
// }
// });
export
const
getAnswers
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
export
const
getAnswers
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
const
req
=
reqExp
as
TypedRequest
;
const
req
=
reqExp
as
TypedRequest
;
const
{
surveyId
}
=
req
.
params
;
const
{
surveyId
}
=
req
.
params
;
try
{
try
{
const
survey
=
await
surveyDb
.
getSurveyById
(
surveyId
);
const
result
=
await
answerDb
.
getAnswers
(
surveyId
);
const
answers
=
await
answerDb
.
getAnswers
(
surveyId
);
console
.
log
(
"
result===
"
,
result
);
console
.
log
(
answers
);
return
res
.
json
(
result
);
const
jsonSurvey
=
survey
?.
toJSON
();
if
(
jsonSurvey
&&
answers
)
{
const
a
=
answers
.
map
(
async
(
a
)
=>
{
const
targetObj
=
jsonSurvey
.
questions
.
find
(
(
q
:
any
)
=>
String
(
q
.
_id
)
===
String
(
a
.
_id
)
)
as
any
;
if
(
targetObj
)
{
if
(
a
.
file
.
length
)
{
targetObj
.
answers
=
a
.
file
;
}
else
{
targetObj
.
answers
=
a
.
answers
;
}
}
});
await
Promise
.
all
(
a
);
}
return
res
.
json
(
jsonSurvey
);
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
res
.
status
(
422
).
send
(
error
.
message
||
"
설문조사 결과 불러오기 오류
"
);
res
.
status
(
422
).
send
(
error
.
message
||
"
설문조사 결과 불러오기 오류
"
);
}
}
...
...
src/db/answer.db.ts
View file @
eb70b0c6
...
@@ -7,22 +7,65 @@ export const createAnswer = async (answer: IAnswer) => {
...
@@ -7,22 +7,65 @@ export const createAnswer = async (answer: IAnswer) => {
};
};
export
const
getAnswers
=
async
(
surveyId
:
string
)
=>
{
export
const
getAnswers
=
async
(
surveyId
:
string
)
=>
{
const
answers
=
await
Answer
.
aggregate
([
const
result
=
await
Answer
.
aggregate
([
// surveyId에 해당하는 답변들 find
{
$match
:
{
surveyId
:
new
Types
.
ObjectId
(
surveyId
)
}
},
{
$match
:
{
surveyId
:
new
Types
.
ObjectId
(
surveyId
)
}
},
// 같은 question에 대한 답변들을 answers[]에 push
// {surveyId,questionId,guestId,content} => {_id:questionId, surveyId, answers:[content,content]}
{
{
$group
:
{
$group
:
{
_id
:
"
$questionId
"
,
_id
:
"
$questionId
"
,
surveyId
:
{
$first
:
"
$surveyId
"
},
answers
:
{
$push
:
"
$content
"
},
answers
:
{
$push
:
"
$content
"
},
},
},
},
},
// question DB popluate
{
{
$lookup
:
{
$lookup
:
{
from
:
"
fileinfo
s
"
,
from
:
"
question
s
"
,
localField
:
"
answers
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
file
"
,
as
:
"
questionInfo
"
,
},
},
{
$unwind
:
"
$questionInfo
"
,
},
{
$set
:
{
"
questionInfo.answers
"
:
"
$answers
"
}
},
{
$unset
:
"
answers
"
},
// 질문 순서대로 정렬
{
$sort
:
{
"
questionInfo.order
"
:
1
}
},
// surveyId로 묶고 questions 내에 { questionInfo, answers }[]
{
$group
:
{
_id
:
"
$surveyId
"
,
questions
:
{
$push
:
"
$questionInfo
"
,
},
},
},
},
},
// survey DB populate
{
$lookup
:
{
from
:
"
surveys
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
survey
"
,
},
},
{
$unwind
:
"
$survey
"
,
},
//밖에 있던 questions를 survey 내부로 이동시키고 survey를 가장 root로 변경
{
$set
:
{
"
survey.questions
"
:
"
$questions
"
}
},
{
$replaceRoot
:
{
newRoot
:
"
$survey
"
}
},
]);
]);
return
answers
;
return
result
[
0
]
;
};
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment