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
travel
Commits
d686ba4f
Commit
d686ba4f
authored
Jul 25, 2022
by
Lee Soobeom
Browse files
update Set
parent
769ea9f4
Changes
8
Show whitespace changes
Inline
Side-by-side
frontend/src/apis/post.api.ts
View file @
d686ba4f
...
@@ -2,26 +2,24 @@ import axios from "axios";
...
@@ -2,26 +2,24 @@ import axios from "axios";
import
baseUrl
from
"
./baseUrl
"
;
import
baseUrl
from
"
./baseUrl
"
;
import
{
PostType
}
from
"
../types
"
;
import
{
PostType
}
from
"
../types
"
;
//Create
export
const
createFileAndPost
=
async
(
formdata
:
FormData
)
=>
{
export
const
createFileAndPost
=
async
(
formdata
:
FormData
)
=>
{
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/posts/`
,
formdata
);
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/posts/`
,
formdata
);
return
data
;
return
data
;
};
};
//Read
export
const
getData
=
async
()
=>
{
export
const
getData
=
async
()
=>
{
const
{
data
}
=
await
axios
.
get
(
`
${
baseUrl
}
/posts/`
);
const
{
data
}
=
await
axios
.
get
(
`
${
baseUrl
}
/posts/`
);
return
data
;
return
data
;
};
//board
};
export
const
getFileByPostId
=
async
(
postId
:
string
)
=>
{
export
const
getFileByPostId
=
async
(
postId
:
string
)
=>
{
const
{
data
}
=
await
axios
.
get
(
`
${
baseUrl
}
/posts/files/
${
postId
}
`
);
const
{
data
}
=
await
axios
.
get
(
`
${
baseUrl
}
/posts/files/
${
postId
}
`
);
return
data
;
return
data
;
};
//
export
const
getImgData
=
async
(
name
:
string
)
=>
{
const
{
data
}
=
await
axios
.
get
(
`/images/
${
name
}
`
);
return
data
;
};
};
//Update
export
const
addCounts
=
async
(
postId
:
string
,
counts
:
number
)
=>
{
export
const
addCounts
=
async
(
postId
:
string
,
counts
:
number
)
=>
{
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/posts/
${
postId
}
`
,
{
const
{
data
}
=
await
axios
.
post
(
`
${
baseUrl
}
/posts/
${
postId
}
`
,
{
counts
:
counts
+
1
,
counts
:
counts
+
1
,
...
@@ -29,12 +27,13 @@ export const addCounts = async (postId: string, counts: number) => {
...
@@ -29,12 +27,13 @@ export const addCounts = async (postId: string, counts: number) => {
return
data
;
return
data
;
};
};
export
const
delete
Post
=
async
(
postId
:
string
)
=>
{
export
const
updateImgAnd
Post
=
async
(
postId
:
string
,
formdata
:
FormData
)
=>
{
const
{
data
}
=
await
axios
.
delete
(
`
${
baseUrl
}
/posts/
${
postId
}
`
);
const
{
data
}
=
await
axios
.
put
(
`
${
baseUrl
}
/posts/
${
postId
}
`
,
formdata
);
return
data
;
return
data
;
};
};
export
const
updateImgAndPost
=
async
(
postId
:
string
,
formdata
:
FormData
)
=>
{
//Delete
const
{
data
}
=
await
axios
.
put
(
`
${
baseUrl
}
/posts/
${
postId
}
`
,
formdata
);
export
const
deletePost
=
async
(
postId
:
string
)
=>
{
const
{
data
}
=
await
axios
.
delete
(
`
${
baseUrl
}
/posts/
${
postId
}
`
);
return
data
;
return
data
;
};
};
frontend/src/board/board.tsx
View file @
d686ba4f
...
@@ -13,7 +13,7 @@ export default function BoardPage() {
...
@@ -13,7 +13,7 @@ export default function BoardPage() {
useEffect
(()
=>
{
useEffect
(()
=>
{
getDataList
();
getDataList
();
},
[]);
},
[
posts
]);
// posts
// posts
const
getDataList
=
async
()
=>
{
const
getDataList
=
async
()
=>
{
const
res
=
await
postApi
.
getData
();
const
res
=
await
postApi
.
getData
();
...
...
frontend/src/post/editpost.tsx
View file @
d686ba4f
...
@@ -41,13 +41,13 @@ export function EditPost() {
...
@@ -41,13 +41,13 @@ export function EditPost() {
getFilesList
(
post
.
_id
);
getFilesList
(
post
.
_id
);
},
[]);
},
[]);
const
imgArr
=
new
Array
();
const
getFilesList
=
async
(
postId
:
string
)
=>
{
const
getFilesList
=
async
(
postId
:
string
)
=>
{
const
res
=
await
postApi
.
getFileByPostId
(
postId
);
//_id는 req.params에 항상 같이 보낸다
const
res
=
await
postApi
.
getFileByPostId
(
postId
);
setFilesList
(
res
);
setFilesList
(
res
);
};
};
const
imgArr
=
new
Array
();
const
updateImg2Db
=
async
(
filelist
:
FileList
)
=>
{
const
updateImg2Db
=
async
(
filelist
:
FileList
)
=>
{
const
formdata
=
new
FormData
();
const
formdata
=
new
FormData
();
formdata
.
append
(
"
title
"
,
user
.
title
);
formdata
.
append
(
"
title
"
,
user
.
title
);
...
@@ -250,6 +250,7 @@ export function EditPost() {
...
@@ -250,6 +250,7 @@ export function EditPost() {
onChange
=
{
titleChange
}
onChange
=
{
titleChange
}
placeholder
=
"제목을 입력해 주세요!"
placeholder
=
"제목을 입력해 주세요!"
className
=
"flex w-96 border-2 border-sky-500 rounded"
className
=
"flex w-96 border-2 border-sky-500 rounded"
defaultValue
=
{
post
.
title
}
/>
/>
<
div
className
=
"flex flex-col mt-1 mb-1"
>
<
div
className
=
"flex flex-col mt-1 mb-1"
>
<
div
className
=
"flex gap-x-2 h-48 overflow-x-scroll"
>
<
div
className
=
"flex gap-x-2 h-48 overflow-x-scroll"
>
...
@@ -268,6 +269,7 @@ export function EditPost() {
...
@@ -268,6 +269,7 @@ export function EditPost() {
name
=
"text"
name
=
"text"
placeholder
=
"여행 후기를 알려주세요!"
placeholder
=
"여행 후기를 알려주세요!"
className
=
"flex w-96 h-96 border-2 border-sky-500 rounded"
className
=
"flex w-96 h-96 border-2 border-sky-500 rounded"
defaultValue
=
{
post
.
text
}
/>
/>
</
div
>
</
div
>
</
form
>
</
form
>
...
...
frontend/src/post/intopost.tsx
View file @
d686ba4f
...
@@ -34,7 +34,6 @@ export function IntoPost() {
...
@@ -34,7 +34,6 @@ export function IntoPost() {
useEffect
(()
=>
{
useEffect
(()
=>
{
getFilesList
(
post
.
_id
);
getFilesList
(
post
.
_id
);
console
.
log
(
"
newfilename
"
,
filesList
?.[
0
].
newfilename
);
},
[]);
},
[]);
const
getFilesList
=
async
(
postId
:
string
)
=>
{
const
getFilesList
=
async
(
postId
:
string
)
=>
{
...
...
src/controllers/post.controller.ts
View file @
d686ba4f
...
@@ -5,6 +5,18 @@ import { asyncWrap } from "../helpers";
...
@@ -5,6 +5,18 @@ import { asyncWrap } from "../helpers";
import
{
postDb
,
userDb
}
from
"
../db
"
;
import
{
postDb
,
userDb
}
from
"
../db
"
;
import
{
TypedRequest
}
from
"
../types
"
;
import
{
TypedRequest
}
from
"
../types
"
;
export
const
userByPostId
=
(
reqExp
:
Request
,
res
:
Response
,
next
:
NextFunction
,
postId
:
string
)
=>
{
const
req
=
reqExp
as
TypedRequest
;
req
.
user
=
userDb
.
findUserByPostId
(
postId
);
next
();
};
//Create
export
const
createFileAndPost
=
asyncWrap
(
async
(
reqExp
,
res
,
next
)
=>
{
export
const
createFileAndPost
=
asyncWrap
(
async
(
reqExp
,
res
,
next
)
=>
{
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
...
@@ -66,6 +78,8 @@ export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
...
@@ -66,6 +78,8 @@ export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
user
:
userId
,
user
:
userId
,
file
:
fileIdArr
,
file
:
fileIdArr
,
});
});
return
res
.
json
(
postRes
);
}
}
}
}
}
}
...
@@ -73,88 +87,154 @@ export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
...
@@ -73,88 +87,154 @@ export const createFileAndPost = asyncWrap(async (reqExp, res, next) => {
});
});
});
});
//Read
export
const
getAllPost
=
asyncWrap
(
async
(
req
,
res
)
=>
{
export
const
getAllPost
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
posts
=
await
postDb
.
getPosts
();
const
posts
=
await
postDb
.
getPosts
();
// console.log(posts);
return
res
.
json
(
posts
);
return
res
.
json
(
posts
);
});
});
export
const
getFiles
=
asyncWrap
(
async
(
req
,
res
)
=>
{
export
const
getFiles
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
postId
}
=
req
.
params
;
const
{
postId
}
=
req
.
params
;
// console.log("나는 말하는 고구마.", postId);
const
files
=
await
postDb
.
getFilesByPostId
(
postId
);
const
files
=
await
postDb
.
getFilesByPostId
(
postId
);
return
res
.
json
(
files
);
return
res
.
json
(
files
);
});
});
//Update
export
const
addCounts
=
asyncWrap
(
async
(
req
,
res
)
=>
{
export
const
addCounts
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
postId
}
=
req
.
params
;
const
{
postId
}
=
req
.
params
;
const
{
counts
}
=
req
.
body
as
{
const
{
counts
}
=
req
.
body
as
{
counts
:
number
;
counts
:
number
;
};
};
// console.log(postId, counts);
const
updateCounts
=
await
postDb
.
addOneCount
(
postId
,
counts
);
const
updateCounts
=
await
postDb
.
addOneCount
(
postId
,
counts
);
return
res
.
json
(
updateCounts
);
return
res
.
json
(
updateCounts
);
});
});
export
const
userByPostId
=
(
export
const
updatePost
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
reqExp
:
Request
,
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
res
:
Response
,
next
:
NextFunction
,
postId
:
string
)
=>
{
const
req
=
reqExp
as
TypedRequest
;
req
.
user
=
userDb
.
findUserByPostId
(
postId
);
next
();
};
export
const
getOnePost
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
userId
=
req
.
auth
.
userId
;
const
{
postId
}
=
req
.
params
;
const
{
postId
}
=
req
.
params
;
const
post
=
await
postDb
.
getPost
(
postId
);
return
res
.
json
(
post
);
const
form
=
formidable
({
});
uploadDir
:
"
uploads
"
,
keepExtensions
:
true
,
multiples
:
true
,
});
export
const
deleteOnePost
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
fileIdArr
=
new
Array
();
const
{
postId
}
=
req
.
params
;
// console.log(postId);
const
deleteCount
=
await
postDb
.
deletePost
(
postId
);
return
res
.
json
(
deleteCount
);
const
oldSet
=
new
Set
(
);
}
);
const
newSet
=
new
Set
(
);
export
const
updatePost
=
asyncWrap
(
async
(
reqExp
,
res
)
=>
{
form
.
parse
(
req
,
async
(
err
,
fields
,
files
)
=>
{
const
req
=
reqExp
as
TypedRequestAuth
<
{
userId
:
string
}
>
;
if
(
!
Array
.
isArray
(
fields
.
title
))
{
const
title
=
fields
.
title
;
if
(
!
Array
.
isArray
(
fields
.
text
))
{
const
text
=
fields
.
text
;
if
(
!
Array
.
isArray
(
fields
.
theme
))
{
const
theme
=
fields
.
theme
;
if
(
!
Array
.
isArray
(
fields
.
city
))
{
const
city
=
fields
.
city
;
if
(
!
Array
.
isArray
(
fields
.
change
))
{
const
change
=
fields
.
change
;
const
oldFiles
=
await
postDb
.
getFilesByPostId
(
postId
);
if
(
!
(
oldFiles
===
undefined
))
{
for
(
var
i
=
0
;
(
i
=
oldFiles
.
length
);
i
++
)
{
const
oldFileName
=
postDb
.
getOriginalFileName
(
oldFiles
[
i
]);
if
(
!
(
oldFileName
===
undefined
))
{
oldSet
.
add
(
oldFileName
);
}
}
}
const
{
title
,
text
,
theme
,
city
,
date
}
=
req
.
body
as
{
if
(
Array
.
isArray
(
files
.
picture
))
{
title
:
string
;
for
(
var
i
=
0
;
i
<
files
.
picture
.
length
;
i
++
)
{
text
:
string
;
const
newFileName
=
files
.
picture
?.[
i
].
originalFilename
;
theme
:
string
;
if
(
!
(
newFileName
===
undefined
))
{
city
:
string
;
newSet
.
add
(
newFileName
)
;
date
:
Date
;
}
counts
:
number
;
}
}
;
}
const
userId
=
req
.
auth
.
userId
;
const
deleteFiles
=
new
Array
()
;
const
{
postId
}
=
req
.
params
;
const
addFiles
=
new
Array
()
;
const
updatePost
=
await
postDb
.
updateOnePost
(
oldSet
.
forEach
((
oldName
)
=>
{
newSet
.
forEach
((
newName
)
=>
{
if
(
!
(
oldName
===
newName
))
{
deleteFiles
.
push
(
oldName
);
addFiles
.
push
(
newName
);
}
});
});
for
(
var
i
=
0
;
i
<
deleteFiles
.
length
;
i
++
)
{
const
originalfilename
=
deleteFiles
[
i
];
const
delRes
=
await
postDb
.
deleteFileByName
(
originalfilename
);
}
if
(
Array
.
isArray
(
files
.
picture
))
{
for
(
var
i
=
0
;
i
<
files
.
picture
.
length
;
i
++
)
{
const
originalfilename
=
files
.
picture
?.[
i
].
originalFilename
;
const
newfilename
=
files
.
picture
?.[
i
].
newFilename
;
const
filepath
=
files
.
picture
?.[
i
].
filepath
;
for
(
var
i
=
0
;
i
<
addFiles
.
length
;
i
++
)
{
const
original
=
addFiles
[
i
];
if
(
original
===
originalfilename
)
{
const
addRes
=
await
postDb
.
createFilesRow
(
originalfilename
,
newfilename
,
filepath
);
fileIdArr
.
push
(
addRes
);
}
}
}
}
const
updateRes
=
await
postDb
.
updatedFileId
(
postId
);
const
fileId
=
fileIdArr
.
concat
(
updateRes
);
const
postRes
=
await
postDb
.
updatePostRow
(
{
{
title
,
title
,
text
,
text
,
theme
,
theme
,
city
,
city
,
date
:
Date
.
now
(),
date
:
Date
.
now
(),
counts
:
req
.
body
.
counts
,
user
:
userId
,
user
:
userId
,
file
:
fileId
,
},
},
postId
postId
);
);
console
.
log
(
"
plzplzplzpllzplzlpzplzzplz
"
,
postRes
);
return
res
.
json
(
postRes
);
}
}
}
}
}
});
});
//Delete
export
const
deleteOnePost
=
asyncWrap
(
async
(
req
,
res
)
=>
{
const
{
postId
}
=
req
.
params
;
// console.log(postId);
const
deleteCount
=
await
postDb
.
deletePost
(
postId
);
const
deleteFileId
=
await
postDb
.
getFilesByPostId
(
postId
);
// console.log("게시글 수정 후", updatePost);
if
(
!
(
deleteFileId
===
undefined
))
{
for
(
var
i
=
0
;
i
<
deleteFileId
.
length
;
i
++
)
{
const
deleteId
=
deleteFileId
[
i
];
const
deleteFile
=
await
postDb
.
deleteFile
(
deleteId
);
}
}
return
res
.
json
(
updatePos
t
);
return
res
.
json
(
deleteCoun
t
);
});
});
src/db/mainimg.db.ts
View file @
d686ba4f
...
@@ -18,7 +18,7 @@ export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => {
...
@@ -18,7 +18,7 @@ export const createMainimg = async (mainimg: MainimgType, pic: IFileInfo) => {
};
};
export
const
getMainimg
=
async
()
=>
{
export
const
getMainimg
=
async
()
=>
{
const
img
=
await
Mainimg
.
find
({}).
populate
(
"
pic
"
);
const
img
=
await
Mainimg
.
find
({}).
populate
(
"
fileInfo
"
);
return
img
;
return
img
;
};
};
...
...
src/db/post.db.ts
View file @
d686ba4f
...
@@ -2,6 +2,7 @@ import { Types, ObjectId } from "mongoose";
...
@@ -2,6 +2,7 @@ import { Types, ObjectId } from "mongoose";
import
{
Post
,
PostType
}
from
"
../models
"
;
import
{
Post
,
PostType
}
from
"
../models
"
;
import
{
FileInfo
,
IFileInfo
}
from
"
../models
"
;
import
{
FileInfo
,
IFileInfo
}
from
"
../models
"
;
//Create
export
const
createPostRow
=
async
(
post
:
PostType
)
=>
{
export
const
createPostRow
=
async
(
post
:
PostType
)
=>
{
const
newPostRow
=
await
Post
.
create
({
const
newPostRow
=
await
Post
.
create
({
title
:
post
.
title
,
title
:
post
.
title
,
...
@@ -30,6 +31,7 @@ export const createFilesRow = async (
...
@@ -30,6 +31,7 @@ export const createFilesRow = async (
return
newFileRow
.
_id
;
return
newFileRow
.
_id
;
};
};
//Read
export
const
getPosts
=
async
()
=>
{
export
const
getPosts
=
async
()
=>
{
const
posts
=
await
Post
.
find
({}).
sort
({
date
:
-
1
});
const
posts
=
await
Post
.
find
({}).
sort
({
date
:
-
1
});
return
posts
;
return
posts
;
...
@@ -40,6 +42,7 @@ export const getFilesByPostId = async (postId: string) => {
...
@@ -40,6 +42,7 @@ export const getFilesByPostId = async (postId: string) => {
return
files
?.
file
;
return
files
?.
file
;
};
};
//Update
export
const
addOneCount
=
async
(
_id
:
string
,
counts
:
number
)
=>
{
export
const
addOneCount
=
async
(
_id
:
string
,
counts
:
number
)
=>
{
const
newCounts
=
await
Post
.
findOneAndUpdate
(
const
newCounts
=
await
Post
.
findOneAndUpdate
(
{
_id
:
_id
},
{
_id
:
_id
},
...
@@ -49,29 +52,45 @@ export const addOneCount = async (_id: string, counts: number) => {
...
@@ -49,29 +52,45 @@ export const addOneCount = async (_id: string, counts: number) => {
return
newCounts
;
return
newCounts
;
};
};
export
const
getPost
=
async
(
_id
:
string
)
=>
{
export
const
updatePostRow
=
async
(
post
:
PostType
,
postId
:
string
)
=>
{
const
post
=
await
Post
.
findOne
({
_id
:
_id
});
return
post
;
};
export
const
deletePost
=
async
(
_id
:
string
)
=>
{
const
res
=
await
Post
.
deleteOne
({
_id
:
_id
});
return
res
;
};
export
const
updateOnePost
=
async
(
post
:
PostType
,
_id
:
string
)
=>
{
const
newPost
=
await
Post
.
findOneAndUpdate
(
const
newPost
=
await
Post
.
findOneAndUpdate
(
{
_id
:
_i
d
},
{
_id
:
postI
d
},
{
{
title
:
post
.
title
,
title
:
post
.
title
,
text
:
post
.
text
,
text
:
post
.
text
,
theme
:
post
.
theme
,
theme
:
post
.
theme
,
city
:
post
.
city
,
city
:
post
.
city
,
date
:
post
.
date
,
date
:
post
.
date
,
counts
:
post
.
counts
,
user
:
post
.
user
,
user
:
post
.
user
,
file
:
post
.
file
,
},
},
{
new
:
true
}
{
new
:
true
}
);
);
return
newPost
;
return
newPost
;
};
};
export
const
getOriginalFileName
=
async
(
_id
:
Types
.
ObjectId
)
=>
{
const
file
=
await
FileInfo
.
findOne
({
_id
:
_id
});
return
file
?.
originalfilename
;
};
export
const
updatedFileId
=
async
(
_id
:
string
)
=>
{
const
updatedFile
=
await
Post
.
findOne
({
_id
:
_id
}).
populate
(
"
file
"
);
return
updatedFile
?.
file
;
};
//Delete
export
const
deletePost
=
async
(
_id
:
string
)
=>
{
const
res
=
await
Post
.
deleteOne
({
_id
:
_id
});
return
res
;
};
export
const
deleteFile
=
async
(
_id
:
Types
.
ObjectId
)
=>
{
const
deleteOne
=
await
FileInfo
.
deleteOne
({
_id
:
_id
});
return
deleteOne
;
};
export
const
deleteFileByName
=
async
(
originalfilename
:
string
)
=>
{
const
deleteFile
=
await
FileInfo
.
deleteOne
({
originalfilename
});
return
deleteFile
;
};
src/routes/post.route.ts
View file @
d686ba4f
...
@@ -12,7 +12,6 @@ router.route("/files/:postId").get(authCtrl.requireLogin, postCtrl.getFiles);
...
@@ -12,7 +12,6 @@ router.route("/files/:postId").get(authCtrl.requireLogin, postCtrl.getFiles);
router
router
.
route
(
"
/:postId
"
)
.
route
(
"
/:postId
"
)
.
post
(
authCtrl
.
requireLogin
,
postCtrl
.
addCounts
)
.
post
(
authCtrl
.
requireLogin
,
postCtrl
.
addCounts
)
.
get
(
authCtrl
.
requireLogin
,
postCtrl
.
getOnePost
)
.
delete
(
authCtrl
.
requireLogin
,
postCtrl
.
deleteOnePost
)
// +authenticate
.
delete
(
authCtrl
.
requireLogin
,
postCtrl
.
deleteOnePost
)
// +authenticate
.
put
(
authCtrl
.
requireLogin
,
postCtrl
.
updatePost
);
.
put
(
authCtrl
.
requireLogin
,
postCtrl
.
updatePost
);
...
...
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