From b414b86532ab65be1e3e76271a597ef0db766986 Mon Sep 17 00:00:00 2001 From: Hyun Woo Jeong <jhw0714@ajou.ac.kr> Date: Mon, 4 Dec 2023 21:20:46 +0900 Subject: [PATCH] =?UTF-8?q?=EB=8C=93=EA=B8=80=20=EC=9E=91=EC=84=B1,=20?= =?UTF-8?q?=EC=82=AD=EC=A0=9C,=20=EC=A2=8B=EC=95=84=EC=9A=94=20=EC=B6=94?= =?UTF-8?q?=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- backend/src/post.js | 32 ++++++++++++++++++++++++++++-- frontend/src/App.js | 6 +++++- frontend/src/components/Comment.js | 1 - frontend/src/components/Header.js | 11 +++++----- frontend/src/pages/Main.js | 2 +- frontend/src/pages/PostRead.js | 23 +++++++++++++++++++-- 6 files changed, 62 insertions(+), 13 deletions(-) diff --git a/backend/src/post.js b/backend/src/post.js index 9a2788ce..1dc1e7f7 100644 --- a/backend/src/post.js +++ b/backend/src/post.js @@ -55,7 +55,7 @@ router.post("/upload", upload.array("img"), async function(req, res, next) { }); -router.get("/loadarticle", async (req, res) => { +router.get("/article", async (req, res) => { console.log(path.join(process.cwd(), '/public')) if(req.session.sessionid){ console.log("�몄뀡 O") @@ -67,7 +67,7 @@ router.get("/loadarticle", async (req, res) => { res.send(JSON.stringify(articles)); }); -router.get("/loadarticle/:id", async (req, res) => { +router.get("/article/:id", async (req, res) => { if(req.session.sessionid){ console.log("�몄뀡 O") } @@ -105,4 +105,32 @@ router.delete("/comment/:articleid/:commentid", async (req, res) => { 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/frontend/src/App.js b/frontend/src/App.js index 5224d062..bcce9e7f 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -1,6 +1,8 @@ import "./css/App.css"; import {Routes, Route, Link, useNavigate, Navigate } from 'react-router-dom'; import React, { useEffect, useState, useContext} from 'react'; +import cookie from 'react-cookies'; + import { UserContext } from './Context.js'; import Main from "./pages/Main.js"; @@ -22,6 +24,8 @@ function App() { function MoveTo(link){ navigate(link) } + + const userinfo = cookie.load('name') // �ㅻ뜑 �낅뜲�댄듃 const LogIn = (name)=>{ // API 濡쒓렇�꾩썐 �붿껌 @@ -60,7 +64,7 @@ function App() { return( <div className="App"> <UserContext.Provider value={{isloggedIn, userName, LogIn, LogOut, CheckSession}}> - <Header islogged={isloggedIn} username={userName}></Header> + <Header cookie={userinfo} username={userName}></Header> <Routes> <Route path="/" element={<Main/>}></Route> <Route path="/login" element={<Login/>}></Route> diff --git a/frontend/src/components/Comment.js b/frontend/src/components/Comment.js index 7e52a1a8..872328bc 100644 --- a/frontend/src/components/Comment.js +++ b/frontend/src/components/Comment.js @@ -21,7 +21,6 @@ function Comments({data}) { const articleContext = useContext(ArticleContext); const userinfo = cookie.load('name') const date = DateTime.fromISO(createdAt).toFormat('yyyy�� MM�� dd�� HH:mm'); - console.log(articleContext) function DeleteComment(e) { const data = {id: _id} axios diff --git a/frontend/src/components/Header.js b/frontend/src/components/Header.js index 02840aa7..05cd0ac4 100644 --- a/frontend/src/components/Header.js +++ b/frontend/src/components/Header.js @@ -5,8 +5,7 @@ import { UserContext } from '../Context.js'; import React, { useEffect, useState, useContext} from 'react'; import { Link, useNavigate, Navigate } from "react-router-dom"; -import cookie from 'react-cookies'; -import {useCookies} from 'react-cookie' + function ButtonLink({link, status, children}){ @@ -33,20 +32,20 @@ function ButtonLink({link, status, children}){ } -function Header({islogged, username}){ - const userinfo = cookie.load('name') +function Header({cookie}){ + return( <div className="header"> <ButtonLink link='/'> <img className="logo_image" alt="logo" src={logo}/> </ButtonLink> <ul> - <p>{islogged?`${userinfo.name}��, �섏쁺�⑸땲��`:'濡쒓렇�명븯�몄슂.'}</p> + <p>{cookie?`${cookie.name}��, �섏쁺�⑸땲��`:'濡쒓렇�명븯�몄슂.'}</p> <ul className="menu_list"> <li><ButtonLink link='/'>Home</ButtonLink></li> <li><ButtonLink link='/search'>寃���</ButtonLink></li> <li><ButtonLink link='/postwrite'>�ъ뒪�� �묒꽦</ButtonLink></li> - <li><ButtonLink link={islogged?'/':'/login'} status={islogged}>{islogged?'濡쒓렇�꾩썐':'濡쒓렇��'}</ButtonLink></li> {/*濡쒓렇�� �щ� 濡쒖쭅 援ы쁽 �꾩슂*/} + <li><ButtonLink link={cookie?'/':'/login'} status={cookie?true:false}>{cookie?'濡쒓렇�꾩썐':'濡쒓렇��'}</ButtonLink></li> {/*濡쒓렇�� �щ� 濡쒖쭅 援ы쁽 �꾩슂*/} {/* { Object.keys(user).length != 0 ? <li><Link to={`/profile/${getUserId()}`}>profile</Link>/<span onClick={logout}>logout</span></li> : <li><Link to="/login">login</Link></li> diff --git a/frontend/src/pages/Main.js b/frontend/src/pages/Main.js index c9c55c9a..d4fe5848 100644 --- a/frontend/src/pages/Main.js +++ b/frontend/src/pages/Main.js @@ -52,7 +52,7 @@ function Main() { async function requestLoadArticle() { const response = await axios({ - url: 'http://localhost:8080/post/loadarticle', // �듭떊�� �밸Ц�� + url: 'http://localhost:8080/post/article', // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; diff --git a/frontend/src/pages/PostRead.js b/frontend/src/pages/PostRead.js index 896cbce2..54dab046 100644 --- a/frontend/src/pages/PostRead.js +++ b/frontend/src/pages/PostRead.js @@ -34,6 +34,7 @@ function PostRead() { .then((response) => { console.log(response) setArticle(response.data) + }) .catch((response)=>{ console.log("error!:LogOut") @@ -42,6 +43,20 @@ function PostRead() { }, []); + function SetLike(){ + axios.put(`http://localhost:8080/post/comment/like/${params.id}`) + .then(res => { + alert("The comment is successfully uploaded"); + return requestLoadArticleById(params.id) + }) + .then(res => { + setArticle(res.data) + }) + .catch(err => { + console.error(err); + }); + } + const onTextChange = (e) => { const {value} = e.target; setInputComment(value); @@ -60,6 +75,10 @@ function PostRead() { }) .then(res => { alert("The comment is successfully uploaded"); + return requestLoadArticleById(params.id) + }) + .then(res => { + setArticle(res.data) }) .catch(err => { console.error(err); @@ -73,7 +92,7 @@ function PostRead() { <MapLocator loc={{lat: article.latitude, lng: article.longitude}} keyword={article.keyword}></MapLocator> <div> <Article data={article}></Article> - <button>議곗���</button> + <button onClick={SetLike}>議곗���</button> </div> </div> <form onSubmit={onSubmit}> @@ -105,7 +124,7 @@ function PostRead() { async function requestLoadArticleById(id) { const response = await axios({ - url: `http://localhost:8080/post/loadarticle/${id}`, // �듭떊�� �밸Ц�� + url: `http://localhost:8080/post/article/${id}`, // �듭떊�� �밸Ц�� method: 'get', // �듭떊�� 諛⑹떇 }); return response; -- GitLab