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