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
accfcc36
Commit
accfcc36
authored
Sep 05, 2022
by
Jiwon Yoon
Browse files
answer없을 경우에도 result 볼 수 있도록 수정
parent
7275ba42
Changes
2
Hide whitespace changes
Inline
Side-by-side
src/controllers/answer.controller.ts
View file @
accfcc36
...
...
@@ -137,7 +137,7 @@ export const getAnswers = asyncWrap(async (reqExp, res) => {
const
{
surveyId
}
=
req
.
params
;
try
{
const
result
=
await
answerDb
.
getAnswers
(
surveyId
);
console
.
log
(
"
result===
"
,
result
);
//
console.log("result===", result);
return
res
.
json
(
result
);
}
catch
(
error
:
any
)
{
res
.
status
(
422
).
send
(
error
.
message
||
"
설문조사 결과 불러오기 오류
"
);
...
...
src/db/answer.db.ts
View file @
accfcc36
import
{
Answer
,
IAnswer
}
from
"
../models
"
;
import
{
Answer
,
IAnswer
,
Survey
}
from
"
../models
"
;
import
{
model
,
Schema
,
Types
}
from
"
mongoose
"
;
export
const
createAnswer
=
async
(
answer
:
IAnswer
)
=>
{
...
...
@@ -7,6 +7,7 @@ export const createAnswer = async (answer: IAnswer) => {
};
export
const
getAnswers
=
async
(
surveyId
:
string
)
=>
{
const
survey
=
await
Survey
.
findById
(
surveyId
).
populate
(
"
questions
"
);
const
result
=
await
Answer
.
aggregate
([
// surveyId에 해당하는 답변들 find
{
$match
:
{
surveyId
:
new
Types
.
ObjectId
(
surveyId
)
}
},
...
...
@@ -27,45 +28,27 @@ export const getAnswers = async (surveyId: string) => {
from
:
"
questions
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
question
Info
"
,
as
:
"
question
"
,
},
},
{
$unwind
:
"
$questionInfo
"
,
},
{
$set
:
{
"
questionInfo.answers
"
:
"
$answers
"
}
},
{
$unset
:
"
answers
"
},
// 질문 순서대로 정렬
{
$sort
:
{
"
questionInfo.order
"
:
1
}
},
// surveyId로 묶고 questions 내에 { questionInfo, answers }[]
{
$group
:
{
_id
:
"
$surveyId
"
,
questions
:
{
$push
:
"
$questionInfo
"
,
$replaceRoot
:
{
newRoot
:
{
$mergeObjects
:
[{
$arrayElemAt
:
[
"
$question
"
,
0
]
},
"
$$ROOT
"
],
},
},
},
{
$unset
:
"
question
"
},
{
$sort
:
{
order
:
1
}
},
]);
// survey DB populate
{
$lookup
:
{
from
:
"
surveys
"
,
localField
:
"
_id
"
,
foreignField
:
"
_id
"
,
as
:
"
survey
"
,
},
},
{
$unwind
:
"
$survey
"
,
},
console
.
log
(
"
result:
"
,
result
);
//밖에 있던 questions를 survey 내부로 이동시키고 survey를 가장 root로 변경
{
$set
:
{
"
survey.questions
"
:
"
$questions
"
}
},
{
$replaceRoot
:
{
newRoot
:
"
$survey
"
}
},
]);
return
result
[
0
];
if
(
survey
&&
result
[
0
])
{
const
Jsurvey
=
survey
.
toJSON
();
Jsurvey
.
questions
=
result
;
return
Jsurvey
;
}
return
survey
;
};
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