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
48e9f032
Commit
48e9f032
authored
Jul 22, 2022
by
Jiwon Yoon
Browse files
answer.controller
parent
0293a649
Changes
3
Show whitespace changes
Inline
Side-by-side
frontend/src/commons/SurveyForm.tsx
View file @
48e9f032
...
@@ -70,7 +70,7 @@ export const SurveyForm = () => {
...
@@ -70,7 +70,7 @@ export const SurveyForm = () => {
formData
.
append
(
"
guestId
"
,
""
);
formData
.
append
(
"
guestId
"
,
""
);
formData
.
append
(
"
answers
"
,
JSON
.
stringify
(
answer
.
answers
));
formData
.
append
(
"
answers
"
,
JSON
.
stringify
(
answer
.
answers
));
files
.
map
((
f
)
=>
{
files
.
map
((
f
)
=>
{
formData
.
append
(
"
f
iles
"
,
f
.
file
);
formData
.
append
(
"
uploadF
iles
"
,
f
.
file
);
});
});
const
newAnswer
:
AnswerType
=
await
answerApi
.
saveAnswers
(
formData
);
const
newAnswer
:
AnswerType
=
await
answerApi
.
saveAnswers
(
formData
);
// console.log(newAnswer);
// console.log(newAnswer);
...
...
src/controllers/answer.controller.ts
View file @
48e9f032
...
@@ -10,32 +10,38 @@ export const createAnswers = asyncWrap(async (reqExp, res) => {
...
@@ -10,32 +10,38 @@ export const createAnswers = asyncWrap(async (reqExp, res) => {
const
answer
=
req
.
body
;
const
answer
=
req
.
body
;
const
answers
=
JSON
.
parse
(
answer
.
answers
);
const
answers
=
JSON
.
parse
(
answer
.
answers
);
answer
.
answers
=
answers
;
answer
.
answers
=
answers
;
cons
ole
.
log
(
answers
)
;
cons
t
files
=
req
.
files
.
uploadFiles
as
formidable
.
File
[]
;
cons
t
file
=
req
.
files
.
img
as
formidable
.
File
;
cons
ole
.
log
(
"
controller의 files
"
,
files
)
;
let
img
;
let
uploadFile
;
try
{
try
{
// 1) 파일을 DB에 저장
if
(
files
)
{
if
(
file
)
{
// 1) 파일을 DB에 저장 후 다시 retFile가져와서
// img = new FileInfo({
// *근데 파일이 여러 개일 수 있기 때문에 순회해야 됨
// name: file.originalFilename,
files
.
map
(
async
(
file
)
=>
{
// url: file.newFilename,
uploadFile
=
new
FileInfo
({
// isNew: true,
name
:
file
.
originalFilename
,
// });
url
:
file
.
newFilename
,
// await fileDb.createFile(file);
isNew
:
true
,
// 2) answer에 img 항목 추가
});
answer
.
img
=
img
;
const
retFile
=
await
fileDb
.
createFile
(
file
);
console
.
log
(
retFile
);
// 2) answers의 type이 file인 친구들 찾아서 그 친구의 answer와 filename을 비교 후 같으면
const
targetObj
=
answer
.
find
((
ans
:
any
)
=>
ans
.
type
===
"
file
"
);
// 3) answer에다가 retFile의 _id 넣어주기
targetObj
.
answer
=
retFile
.
_id
;
});
}
}
// 3) Answer 만들기(map을 돌려서 하나씩 추가시켜야 함)
// 3) Answer
DB
만들기(map을 돌려서 하나씩 추가시켜야 함)
console
.
log
(
answer
);
//
console.log(answer);
// const newAnswer = await answerDb.createAnswer(answer);
// const newAnswer = await answerDb.createAnswer(answer);
// 주의: ref는 반드시 save를 해야 디비에 생성이 됩니다.
// 주의: ref는 반드시 save를 해야 디비에 생성이 됩니다.
return
res
.
json
();
return
res
.
json
();
}
catch
(
error
:
any
)
{
}
catch
(
error
:
any
)
{
console
.
log
(
"
error in create user:
"
,
error
);
console
.
log
(
"
error in create user:
"
,
error
);
// 오류 발생시 저장된 파일 제거
// 오류 발생시 저장된 파일 제거
if
(
file
)
{
if
(
file
s
)
{
//
img
&& (await fileDb.deleteFileById(
img
._id.toString()));
//
uploadFiles
&& (await fileDb.deleteFileById(
uploadFiles
._id.toString()));
await
fs
.
unlink
(
file
.
filepath
);
//
await fs.unlink(file
s
.filepath);
}
}
res
.
status
(
422
).
send
(
error
.
message
||
"
사용자 생성 오류
"
);
res
.
status
(
422
).
send
(
error
.
message
||
"
사용자 생성 오류
"
);
}
}
...
...
src/controllers/file.controller.ts
View file @
48e9f032
...
@@ -4,7 +4,7 @@ import { TypedRequest } from "../types";
...
@@ -4,7 +4,7 @@ import { TypedRequest } from "../types";
export
const
uploadFile
=
asyncWrap
(
async
(
reqExp
,
res
,
next
)
=>
{
export
const
uploadFile
=
asyncWrap
(
async
(
reqExp
,
res
,
next
)
=>
{
const
req
=
reqExp
as
TypedRequest
;
const
req
=
reqExp
as
TypedRequest
;
const
form
=
formidable
({
multiples
:
fals
e
,
uploadDir
:
"
uploads
"
});
const
form
=
formidable
({
multiples
:
tru
e
,
uploadDir
:
"
uploads
"
});
await
new
Promise
((
resolve
,
reject
)
=>
{
await
new
Promise
((
resolve
,
reject
)
=>
{
form
.
parse
(
req
,
(
err
,
fields
,
files
)
=>
{
form
.
parse
(
req
,
(
err
,
fields
,
files
)
=>
{
...
...
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