diff --git a/README.md b/README.md index 7cf6568a4933926b35fa1da6310017cbcc536e70..aa858b2cc71071cde642b0426f3dd73a1442877d 100644 --- a/README.md +++ b/README.md @@ -7,3 +7,23 @@ #### �꾨줎��: cd frontend; npm run start #### 諛깆븻��: cd backend; npm run start �꾨줎�� 諛� 紐⑤몢 臾몄꽌 ���λ릺硫� �ㅼ떆 濡쒕뵫 �섎땲源� �먭컻 �쒕쾲�� 耳쒕넃怨� �섏젙�섏떆硫� �섏떆寃좎뒿�덈떎. + +## Auth Specification +|Method|Path|Message Format(JSON)|�ㅻ챸| +|---|---|---|---| +|POST|/auth/login|{<br>"email": string,<br>"name": string,<br>"sub": string,<br>"picture": string,<br>}|濡쒓렇�� �붿껌| +|GET|/auth/logout||濡쒓렇�꾩썐 �붿껌| +|GET|/auth/session||�몄뀡 �곌껐 �뺤씤 �붿껌| + +## Article Specification +|Method|Path|Message Format(JSON)|�ㅻ챸| +|---|---|---|---| +|POST|/article|{<br>"title": string,<br>"content": string,<br>"img": string[],<br>"keyword": string,<br>"latitiude":number,<br>"longitude": number<br>}|寃뚯떆湲� �묒꽦 �붿껌| +|DELETE|/article/[id]||�뱀젙 寃뚯떆湲� ��젣 �붿껌| +|GET|/article||�꾩껜 寃뚯떆湲� �붿껌| +|GET|/article/[id]||�뱀젙 寃뚯떆湲� �붿껌| +|GET|/article/search/[keyword]||�ㅼ썙�쒕줈 寃뚯떆湲� 寃��� �붿껌| +|GET|/article/sort/[crit]||寃뚯떆湲� �뺣젹 �붿껌| +|POST|/article/[id]/comment|{<br>"content": string<br>}|�볤� �묒꽦 �붿껌| +|DELETE|/article/[articleId]/comment/[commentId]||�뱀젙 �볤� ��젣 �붿껌| +|PUT|/article/[id]/like||寃뚯떆湲� 醫뗭븘�� �붿껌| diff --git a/backend/src/models/article.js b/backend/src/article/article.js similarity index 96% rename from backend/src/models/article.js rename to backend/src/article/article.js index 9b027464af4dd30551a453096ae1e3b6feb3e295..4f0579b6a41b937cf7aebd0af76d996f7059fa0c 100644 --- a/backend/src/models/article.js +++ b/backend/src/article/article.js @@ -1,5 +1,5 @@ import mongoose from 'mongoose'; -import UserSchema from './user.js'; +import UserSchema from '../user/user.js'; const CommentSchema = new mongoose.Schema({ content: { diff --git a/backend/src/article/articleController.js b/backend/src/article/articleController.js new file mode 100644 index 0000000000000000000000000000000000000000..04e4683b49d3b15cf8da81111e525c4ae1659dcc --- /dev/null +++ b/backend/src/article/articleController.js @@ -0,0 +1,147 @@ +import express from 'express'; +import multer from 'multer'; +import path from 'path' +import moment from 'moment' + +import userService from '../user/userService.js'; +import articleService from './articleService.js'; + +const __dirname = path.resolve(); + +export const router = express.Router(); + +const upload = multer({ + storage: multer.diskStorage({ // ���ν븳怨듦컙 �뺣낫 : �섎뱶�붿뒪�ъ뿉 ���� + destination(req, file, done) { // ���� �꾩튂 + done(null, 'public/uploads/'); // uploads�쇰뒗 �대뜑 �덉뿉 ���� + }, + filename(req, file, done) { // �뚯씪紐낆쓣 �대뼡 �대쫫�쇰줈 �щ┫吏� + const ext = path.extname(file.originalname); // �뚯씪�� �뺤옣�� + done(null, path.basename(file.originalname, ext) + Date.now() + ext); // �뚯씪�대쫫 + �좎쭨 + �뺤옣�� �대쫫�쇰줈 ���� + } + }), + limits: { fileSize: 5 * 1024 * 1024 } // 5硫붽�濡� �⑸웾 �쒗븳 +}); + +router.post("/", upload.array("img"), async function (req, res, next) { + if (!req.session.sessionid) { + console.log("No session error"); + return; + } + console.log("POST /article"); + + const inputTitle = req.body.title + const inputContent = req.body.content + const inputImage = req.files.map(el => el.path); + const useremail = req.session.sessionid.email + const author = await userService.findUserByEmail(useremail); + const inputkeyword = req.body.keyword + const inputlat = req.body.latitude + const inputlng = req.body.longitude + + await articleService.createArticle({ + title: inputTitle, + content: inputContent, + imageUrls: inputImage, + author: author, + keyword: inputkeyword, + latitude: inputlat, + longitude: inputlng, + comments: [], + likes: [], + }); + + console.log('saved.') + res.send(); +}); + + +router.get("/", async (req, res) => { + console.log(path.join(process.cwd(), '/public')) + if (!req.session.sessionid) { + console.log("No session"); + } + const articles = await articleService.findAllArticle(); + articles.forEach((article) => { + article.imageUrls.forEach( + (urls) => { + try { + //res.sendFile(path.join(__dirname, urls)); + } + catch (err) { + console.log(err) + } + }); + }); + res.send(JSON.stringify(articles)); +}); + +router.get("/:id", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session"); + } + const articles = await articleService.findArticleById(req.params.id); + res.send(JSON.stringify(articles)); +}).delete("/:id", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session - del"); + } + const articles = await articleService.deleteArticle(req.params.id); + res.send(JSON.stringify(articles)); +}); + +router.post("/:id/comment", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session"); + } + const user = await userService.findUserByEmail(req.session.sessionid.email); + const data = { + content: req.body.content, + author: user._id + } + const articles = await articleService.createComment(req.params.id, data); + res.send(JSON.stringify(articles)); +}) + +router.delete("/:articleid/comment/:commentid", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session"); + } + const articles = await articleService.deleteComment(req.params.articleid, req.params.commentid); + res.send(JSON.stringify(articles)); +}); + +router.put("/:id/like", async (req, res) => { + if (!req.session.sessionid) { + console.log("No session"); + } + const user = await userService.findUserByEmail(req.session.sessionid.email); + const articles = await articleService.setLike(req.params.id, user._id) + res.send(JSON.stringify(articles)); +}); + +router.get("/search/:keyword", async (req, res) => { + if(req.session.sessionid){ + console.log("�몄뀡 O") + } + else { + console.log("�몄뀡 X") + } + console.log(req.params.keyword) + const articles = await articleService.findArticlesByKeyword(req.params.keyword); + res.send(JSON.stringify(articles)); +}); + +router.get("/sort/:crit", async (req, res) => { + if(req.session.sessionid){ + console.log("�몄뀡 O") + } + else { + console.log("�몄뀡 X") + } + console.log(req.params.crit) + const articles = await articleService.findAllAndSortArticle(req.params.crit); + res.send(JSON.stringify(articles)); +}); + +export default router; diff --git a/backend/src/data/articleService.js b/backend/src/article/articleService.js similarity index 56% rename from backend/src/data/articleService.js rename to backend/src/article/articleService.js index 1704ab7c1d5299c0015a7ca9befc7fa9ee0cadc4..7d8dc24624f37a28f505d57ffc6ac75685ad5612 100644 --- a/backend/src/data/articleService.js +++ b/backend/src/article/articleService.js @@ -1,4 +1,4 @@ -import Article from '../models/article.js'; +import Article from './article.js'; const articleService = { async createArticle(articleData) { @@ -23,6 +23,63 @@ const articleService = { } }, + async findAllAndSortArticle(criteria) { + if (criteria==="time"){ + try { + const articles = await Article + .find({}) + .populate('author') + .populate('likes') + .sort({"createdAt": -1}); + + return articles; + } catch (err) { + throw err; + } + } + else if (criteria==="like") { + try { + const agg = + await Article.aggregate( + [ + { "$project": + { + "title": 1, + "content": 1, + "imageUrls": 1, + "author": 1, + "keyword": 1, + "latitude": 1, + "longitude": 1, + "comments": 1, + "likes": 1, + "createdAt": 1, + "length": { "$size": "$likes" } + } + }, + { "$sort": { "length": -1 } }, + ] + ) + + let articles = null; + const result = await Article.populate(agg, {path: "author"}).then( + (res)=>{return Article.populate(res, {path: "likes"})} + ).then( + (res)=>{articles = res;} + ); + return articles; + + } catch (err) { + throw err; + } + } + else { + console.log("invalid criteria."); + return; + } + + }, + async findArticleById(articleId) { try { const article = await Article @@ -47,6 +104,18 @@ const articleService = { throw err; } }, + + async findArticlesByKeyword(keyword) { + try { + const articles = await Article + .find({ keyword: keyword }) + .populate('author') + .populate('likes'); + return articles; + } catch (err) { + throw err; + } + }, async deleteArticle(articleId) { try { diff --git a/backend/src/auth.js b/backend/src/auth/authController.js similarity index 91% rename from backend/src/auth.js rename to backend/src/auth/authController.js index 534ab743351c7631794c8b4f008a4d68b5a29f2c..34ee420d6fe1bd62c4300a684c42d7c79384bcf8 100644 --- a/backend/src/auth.js +++ b/backend/src/auth/authController.js @@ -1,6 +1,6 @@ import express from 'express'; import moment from 'moment'; -import userService from './data/userService.js'; +import userService from '../user/userService.js'; export const router = express.Router(); @@ -39,20 +39,21 @@ router.post('/login', async (req, res) => { }); router.get("/logout", (req, res) => { + res.clearCookie('name'); if (req.session.sessionid) { req.session.destroy((err) => { if (err) { + console.log(err) return; } }); - res.clearCookie('name'); res.send(req.body.name); } else { res.send(req.body.name); } }); -router.get("/check", (req, res) => { +router.get("/session", (req, res) => { if(req.session.sessionid){ res.send(true); } diff --git a/backend/src/db.js b/backend/src/db.js index 539799b6345742235f017623c2629e8249975aa2..24e8546bd71aca65f942b29c436357f63e5adca3 100644 --- a/backend/src/db.js +++ b/backend/src/db.js @@ -3,81 +3,6 @@ import mongoose from 'mongoose'; dotenv.config(); -// const GoogleProviderSchema = new mongoose.Schema({ -// id: { -// type: String, -// required: true, -// //unique: true, -// }, -// profileUrl: { -// type: String, -// }, -// }); - -// const UserSchema = new mongoose.Schema({ -// nickname: { -// type: String, -// }, -// email: { -// type: String, -// //unique: true, -// }, -// google: { -// type: GoogleProviderSchema, -// } -// }); - - -// const UserModel = mongoose.model("User", UserSchema); - -// const CommentSchema = new mongoose.Schema({ -// content: { -// type: String, -// required: true, -// }, -// author: { -// type: UserSchema, -// required: true, -// }, -// createdAt: { -// type: Date, -// default: Date.now, -// }, -// }); -// CommentSchema.index( { commentId: 1 } , { sparse: true } ) - -// const ArticleSchema = new mongoose.Schema({ -// title: { -// type: String, -// required: true, -// }, -// content: { -// type: String, -// required: true, -// }, -// imageUrls: { -// type: [String], -// }, -// author: { -// type: UserSchema, -// required: true, -// }, -// comments: { -// type: [CommentSchema], -// unique: false, -// }, -// likes: { -// type: [UserSchema], -// }, -// createdAt: { -// type: Date, -// default: Date.now, -// }, -// }); - -// ArticleSchema.index({articleId:1}, { sparse: true }) -// const ArticleModel = mongoose.model("Article", ArticleSchema); - const connectDB = async () => { const url = process.env.MONGODB_URI; try { diff --git a/backend/src/index.js b/backend/src/index.js index 255e987a7d4de4009670aa34fc47178a07da3e36..33a62e67742eefbc7e0e9a92c8e317ef5526a69b 100644 --- a/backend/src/index.js +++ b/backend/src/index.js @@ -6,8 +6,8 @@ import cookieParser from 'cookie-parser'; import session from 'express-session'; -import auth from './auth.js'; -import post from './post.js'; +import auth from './auth/authController.js'; +import article from './article/articleController.js'; import connectDB from './db.js'; @@ -38,7 +38,7 @@ app.use( ); //cors �ㅼ젙�� �쒕떎.. // app.use(express.static(path.join(process.cwd(), '../public'))); -app.use(express.static('files')) +app.use(express.static('public')); app.use(cookieParser()); app.use(express.json()); @@ -51,4 +51,4 @@ app.listen(PORT, () => { app.use('/auth', auth); -app.use('/post', post); \ No newline at end of file +app.use('/article', article); \ No newline at end of file diff --git a/backend/src/post.js b/backend/src/post.js deleted file mode 100644 index 1dc1e7f7b4717ba914cb15cb457254cefd21ae4f..0000000000000000000000000000000000000000 --- a/backend/src/post.js +++ /dev/null @@ -1,136 +0,0 @@ -import express from 'express'; -import multer from 'multer'; -import path from 'path' -import moment from 'moment' - -import userService from './data/userService.js'; -import articleService from './data/articleService.js'; - -const __dirname = path.resolve(); - -export const router = express.Router(); - -const upload = multer({ - storage: multer.diskStorage({ // ���ν븳怨듦컙 �뺣낫 : �섎뱶�붿뒪�ъ뿉 ���� - destination(req, file, done) { // ���� �꾩튂 - done(null, 'files'); // uploads�쇰뒗 �대뜑 �덉뿉 ���� - }, - filename(req, file, done) { // �뚯씪紐낆쓣 �대뼡 �대쫫�쇰줈 �щ┫吏� - const ext = path.extname(file.originalname); // �뚯씪�� �뺤옣�� - done(null, path.basename(file.originalname, ext) + Date.now() + ext); // �뚯씪�대쫫 + �좎쭨 + �뺤옣�� �대쫫�쇰줈 ���� - } - }), - limits: { fileSize: 5 * 1024 * 1024 } // 5硫붽�濡� �⑸웾 �쒗븳 - }); - -router.post("/upload", upload.array("img"), async function(req, res, next) { - if(!req.session.sessionid){ - // �몄뀡�� �놁뿁 - } - - const inputTitle = req.body.title - const inputContent = req.body.content - const inputImage = req.files.map(el => el.path); - const useremail = req.session.sessionid.email - const author = await userService.findUserByEmail(useremail); - const inputkeyword = req.body.keyword - const inputlat = req.body.latitude - const inputlng = req.body.longitude - - await articleService.createArticle({ - title: inputTitle, - content: inputContent, - imageUrls: inputImage, - author: author, - keyword: inputkeyword, - latitude: inputlat, - longitude: inputlng, - comments: [], - likes: [], - }); - - console.log('saved.') - res.send(); - res.send(); - }); - - -router.get("/article", async (req, res) => { - console.log(path.join(process.cwd(), '/public')) - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const articles = await articleService.findAllArticle(); - res.send(JSON.stringify(articles)); -}); - -router.get("/article/:id", async (req, res) => { - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const articles = await articleService.findArticleById(req.params.id); - res.send(JSON.stringify(articles)); -}); - -router.post("/comment/:id", async (req, res) => { - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const user = await userService.findUserByEmail(req.session.sessionid.email); - const data = { - content: req.body.content, - author: user._id - } - const articles = await articleService.createComment(req.params.id, data); - res.send(JSON.stringify(articles)); -}) - -router.delete("/comment/:articleid/:commentid", async (req, res) => { - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const articles = await articleService.deleteComment(req.params.articleid, req.params.commentid); - res.send(JSON.stringify(articles)); -}); - -router.post("/comment/:id", async (req, res) => { - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const user = await userService.findUserByEmail(req.session.sessionid.email); - const data = { - content: req.body.content, - author: user._id - } - const articles = await articleService.createComment(req.params.id, data); - res.send(JSON.stringify(articles)); -}) - -router.put("/comment/like/:id", async (req, res) => { - if(req.session.sessionid){ - console.log("�몄뀡 O") - } - else { - console.log("�몄뀡 X") - } - const user = await userService.findUserByEmail(req.session.sessionid.email); - const articles = await articleService.setLike(req.params.id, user._id) - res.send(JSON.stringify(articles)); -}); - -export default router; diff --git a/backend/src/models/user.js b/backend/src/user/user.js similarity index 100% rename from backend/src/models/user.js rename to backend/src/user/user.js diff --git a/backend/src/data/userService.js b/backend/src/user/userService.js similarity index 94% rename from backend/src/data/userService.js rename to backend/src/user/userService.js index ee8128799db59d628c77a1d097f1af2c392bb456..dd7cb22d913e32504dd40fc74ee2ba1b1d9fb2ff 100644 --- a/backend/src/data/userService.js +++ b/backend/src/user/userService.js @@ -1,4 +1,4 @@ -import User from '../models/user.js'; +import User from './user.js'; const userService = { async createUser(userData) { diff --git a/frontend/src/App.js b/frontend/src/App.js index bcce9e7fe97ba1854055d3de2cef1991e3742d10..feb13bad280e6d322cbb5f51db7c5b1d0e018508 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -88,7 +88,7 @@ async function requestLogout() { async function requestCheckSession() { const response = await axios({ - url: 'http://localhost:8080/auth/check', // �듭떊�� �밸Ц�� + url: 'http://localhost:8080/auth/session', // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; diff --git a/frontend/src/components/Article.js b/frontend/src/components/Article.js index bdefbd816a702012514ae0fbec5edaf6feab2ff6..1c778fd22bc888b6443e9c3ccf5f283e6d11dbc7 100644 --- a/frontend/src/components/Article.js +++ b/frontend/src/components/Article.js @@ -20,8 +20,8 @@ function Article({ data }) { // http://localhost:8080/uploads/21701487062905.png return ( <img - src={`http://localhost:8080/${el.replaceAll("\\", "/").substring(5)}`} - alt={`http://localhost:8080/${el.replaceAll("\\", "/").substring(5)}`} + src={`http://localhost:8080/${el.replaceAll("\\", "/").substring(7)}`} + alt={`http://localhost:8080/${el.replaceAll("\\", "/").substring(7)}`} style={{ width: "100px", height: "100px" }} /> ) }); diff --git a/frontend/src/components/Comment.js b/frontend/src/components/Comment.js index 872328bc0d738c059ffb74271181e11d71d8f05b..d002ab71881a8fe4f48ecac97f5bd69d209c9514 100644 --- a/frontend/src/components/Comment.js +++ b/frontend/src/components/Comment.js @@ -24,7 +24,7 @@ function Comments({data}) { function DeleteComment(e) { const data = {id: _id} axios - .delete(`http://localhost:8080/post/comment/${params.id}/${_id}`, data, + .delete(`http://localhost:8080/article/${params.id}/comment/${_id}`, data, { headers: {"Content-Type": 'application/json'} }) diff --git a/frontend/src/pages/Main.js b/frontend/src/pages/Main.js index fd7ccb29cb33fc8767b2d56f72f5fb5a0d85599d..f70c62a00bce9d5b5222bcb6cb0d0da93f27e8fb 100644 --- a/frontend/src/pages/Main.js +++ b/frontend/src/pages/Main.js @@ -38,13 +38,29 @@ function Main() { }) }, []); + const handleSortChange = (event) => { + console.log(event.target.value) + requestSortArticle(event.target.value) + .then((response) => { + console.log(response) + setArticleList(response.data) + }) + }; + return( <div className="App"> <h1>�앸룄�쎌뿉 �ㅼ떊 寃껋쓣 �섏쁺�⑸땲��. </h1> <div className="introduction"> <p>�앸떦 由щ럭瑜� �먯돺寃� 紐⑥븘蹂� �� �덈뒗 �쒕퉬��</p> </div> - <Button>寃���</Button> + <div style={{display: 'flex'}}> + <Button>寃���</Button> + <select id="addInputPrior" onChange={handleSortChange}> + <option value="time">理쒖떊��</option> + <option value="like">�멸린��</option> + </select> + </div> + {listItem} </div>) ; @@ -52,10 +68,18 @@ function Main() { async function requestLoadArticle() { const response = await axios({ - url: 'http://localhost:8080/post/article', // �듭떊�� �밸Ц�� + url: 'http://localhost:8080/article', // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; } + async function requestSortArticle(crit) { + const response = await axios({ + url: `http://localhost:8080/article/sort/${crit}`, // �듭떊�� �밸Ц�� + method: 'get', // �듭떊�� 諛⑹떇 + }); + return response; + } + export default Main; diff --git a/frontend/src/pages/PostRead.js b/frontend/src/pages/PostRead.js index 54dab046f44782054b8e4cbe4fe1444f44315aa6..af80f9e8d85f5e74b9b5ae753d2ccc4960112705 100644 --- a/frontend/src/pages/PostRead.js +++ b/frontend/src/pages/PostRead.js @@ -4,6 +4,7 @@ import Article from '../components/Article.js'; import { UserContext, ArticleContext } from '../Context.js'; import MapLocator from '../components/MapForLoaction.js'; import Comment from '../components/Comment.js'; +import cookie from 'react-cookies'; import axios from 'axios'; axios.defaults.withCredentials = true; @@ -12,7 +13,7 @@ function PostRead() { let params = useParams(); const userContext = useContext(UserContext); const articleContext = useContext(ArticleContext); - + const userinfo = cookie.load('name') const [article, setArticle] = useState(null) const [inputComment, setInputComment] = useState("") const [commentList, setCommentList] = useState("") @@ -44,7 +45,7 @@ function PostRead() { }, []); function SetLike(){ - axios.put(`http://localhost:8080/post/comment/like/${params.id}`) + axios.put(`http://localhost:8080/article/${params.id}/like`) .then(res => { alert("The comment is successfully uploaded"); return requestLoadArticleById(params.id) @@ -69,7 +70,7 @@ function PostRead() { return } const data = {content: inputComment} - axios.post(`http://localhost:8080/post/comment/${params.id}`, data, + axios.post(`http://localhost:8080/article/${params.id}/comment`, data, { headers: {"Content-Type": 'application/json'} }) @@ -85,6 +86,26 @@ function PostRead() { }); }; + function DelButton({isThatYou, target}){ + function deleteArticle(){ + console.log(target._id) + requestDeleteArticleById(target._id).then(res => { + alert("The article is successfully deleted"); + MoveTo('/') + }) + .catch(err => { + console.error(err); + }); + } + + if (isThatYou) { + return(<button onClick={deleteArticle}>吏��곌린</button>) + } + else { + return null + } + + } if (article) { return( <> @@ -92,7 +113,11 @@ function PostRead() { <MapLocator loc={{lat: article.latitude, lng: article.longitude}} keyword={article.keyword}></MapLocator> <div> <Article data={article}></Article> - <button onClick={SetLike}>議곗���</button> + <div style={{display: 'flex'}}> + <button onClick={SetLike}>議곗���</button> + <DelButton isThatYou={userinfo.id === article.author.user_id} target={article}></DelButton> + </div> + </div> </div> <form onSubmit={onSubmit}> @@ -124,10 +149,18 @@ function PostRead() { async function requestLoadArticleById(id) { const response = await axios({ - url: `http://localhost:8080/post/article/${id}`, // �듭떊�� �밸Ц�� + url: `http://localhost:8080/article/${id}`, // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; } +async function requestDeleteArticleById(id) { + const response = await axios({ + url: `http://localhost:8080/article/${id}`, // �듭떊�� �밸Ц�� + method: 'delete', // �듭떊�� 諛⑹떇 + }); + return response; +} + export default PostRead; \ No newline at end of file diff --git a/frontend/src/pages/PostWrite.js b/frontend/src/pages/PostWrite.js index 4b90e484470bfb33230150130c63b249af429096..3016ca9dc6ea6dc7b867fa5c71490d2aa08d9ee1 100644 --- a/frontend/src/pages/PostWrite.js +++ b/frontend/src/pages/PostWrite.js @@ -113,7 +113,7 @@ function PostWrite(){ formData.append("latitude", location.center.lat); formData.append("longitude", location.center.lng); axios - .post("http://localhost:8080/post/upload", formData, + .post("http://localhost:8080/article", formData, { headers: {"Content-Type": "multipart/form-data"} }) diff --git a/frontend/src/pages/Search.js b/frontend/src/pages/Search.js index c380e2dbce3f47c05dfab81662276cd101025f0d..5f01b8213624c0cac1c06d0befe20e0d585e0742 100644 --- a/frontend/src/pages/Search.js +++ b/frontend/src/pages/Search.js @@ -3,10 +3,13 @@ import React, { useState, useEffect, useContext } from 'react'; import {Routes, Route, Link, useNavigate, Navigate } from 'react-router-dom'; import SearchMap from '../components/SearchMapByKeyword.js' import { UserContext } from '../Context.js'; +import Article from '../components/Article.js'; +import axios from 'axios'; +axios.defaults.withCredentials = true; const {kakao} = window; - function Search(props) { + const [articleList, setArticleList] = useState([]) const userContext = useContext(UserContext); const navigate = useNavigate(); function MoveTo(link){ @@ -24,21 +27,87 @@ function Search(props) { if (!response.data) { alert("�몄뀡�� 留뚮즺�섏뿀�듬땲��. �ㅼ떆 濡쒓렇�� 諛붾엻�덈떎.") MoveTo('/login') - } + } + else { + // requestLoadArticle() + // .then((response) => { + // console.log(response) + // setArticleList(response.data) + // }) + } }) .catch((response)=>{ console.log("error!:LogOut") console.log(response) }) - }); + }, []); + + let listItem = []; + listItem = articleList.map((article)=>{ + return( + <Article + key={article._id} + data={article} + ></Article> + ) + }) + + const onSubmit = e => { + e.preventDefault(); + if (!location.keyword){ + alert("寃��됰��곸쓣吏��뺥빐二쇱꽭��"); + return + } + requestLoadArticleByKeyword(location.keyword) + .then((response) => { + console.log(response) + setArticleList(response.data) + }) + }; + + return ( + <div style={{display: 'flex'}}> + <div className="search"> + <h1>寃��됲럹�댁��낅땲��.</h1> + <p>臾댁뾿�� �쒖떆怨� �띕굹��? �대뵒 怨꾩떆二�?</p> + <SearchMap loc={location} setLoc={setLocation}></SearchMap> + + </div> + + <div className="searchresult"> + <p>�좏깮�� �ㅼ썙�쒕줈 寃��됲빀�덈떎.</p> + <form onSubmit={onSubmit} style={{display: 'flex'}}> + <input readonly value={location.keyword} type="text"></input> + <button type="submit">寃���!</button> + <button type="button" onClick={()=>{ + setLocation({ + keyword: "", + center: { lat: null, lng: null } + })}}>�좏깮 �댁젣</button> + </form> + <h1>寃��됯껐怨� {listItem.length} 媛� �뺤씤</h1> + {listItem} + </div> + </div> + + - return ( - <div className="search"> - <h1>寃��됲럹�댁��낅땲��.</h1> - <SearchMap loc={location} setLoc={setLocation}></SearchMap> - </div> ); } +async function requestLoadArticle() { + const response = await axios({ + url: 'http://localhost:8080/article', // �듭떊�� �밸Ц�� + method: 'get', // �듭떊�� 諛⑹떇 + }); + return response; + } +async function requestLoadArticleByKeyword(keyword) { + const response = await axios({ + url: `http://localhost:8080/article/search/${keyword}`, // �듭떊�� �밸Ц�� + method: 'get', // �듭떊�� 諛⑹떇 + }); + return response; +} export default Search; \ No newline at end of file