diff --git a/backend/src/data/articleService.js b/backend/src/data/articleService.js index 6d1eebbe4e95903e34cc176b754cae3117aba1af..1704ab7c1d5299c0015a7ca9befc7fa9ee0cadc4 100644 --- a/backend/src/data/articleService.js +++ b/backend/src/data/articleService.js @@ -62,7 +62,7 @@ const articleService = { const article = await Article.findById(articleId); article.comments.push(commentData); await article.save(); - return article; + return article.populate('author'); } catch (err) { throw err; } @@ -73,7 +73,7 @@ const articleService = { const article = await Article.findById(articleId); article.comments.pull({ _id: commentId }); await article.save(); - return article; + return article.populate('author'); } catch (err) { throw err; } diff --git a/backend/src/post.js b/backend/src/post.js index 7d9c62e1610997ae912c01d559ba485d212d5992..9a2788ceb9107afd3808e5c50110e0104dae1f43 100644 --- a/backend/src/post.js +++ b/backend/src/post.js @@ -6,8 +6,6 @@ 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(); @@ -29,7 +27,6 @@ router.post("/upload", upload.array("img"), async function(req, res, next) { if(!req.session.sessionid){ // �몄뀡�� �놁뿁 } - console.log("�ъ뒪�� '�댁쨾'"); const inputTitle = req.body.title const inputContent = req.body.content @@ -39,7 +36,6 @@ router.post("/upload", upload.array("img"), async function(req, res, next) { const inputkeyword = req.body.keyword const inputlat = req.body.latitude const inputlng = req.body.longitude - // const currentTime = moment().format('YYYY-MM-DD HH:mm') await articleService.createArticle({ title: inputTitle, @@ -68,22 +64,10 @@ router.get("/loadarticle", async (req, res) => { console.log("�몄뀡 X") } 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("/loadarticle/:id", async (req, res) => { - if(req.session.sessionid){ console.log("�몄뀡 O") } @@ -94,4 +78,31 @@ router.get("/loadarticle/:id", 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.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)); +}); + export default router; diff --git a/frontend/src/Context.js b/frontend/src/Context.js index effc8f2097cc4e9314180c352db2e812d02d390c..056890b00117d649850914cbe0daf1ab8b1070fa 100644 --- a/frontend/src/Context.js +++ b/frontend/src/Context.js @@ -8,8 +8,8 @@ const UserContext = createContext({ CheckSession : ()=>{} }); -const LocationContext = createContext({ - +const ArticleContext = createContext({ + Aritcle: {} }); -export {UserContext, LocationContext} \ No newline at end of file +export {UserContext, ArticleContext} \ No newline at end of file diff --git a/frontend/src/components/Comment.js b/frontend/src/components/Comment.js index 7e06f4e0992f450cb8012e7043bc749ceca0ed38..7e52a1a8a667379d3d3e3425fcfad4eaf3d6bac9 100644 --- a/frontend/src/components/Comment.js +++ b/frontend/src/components/Comment.js @@ -1,28 +1,66 @@ -import React, {useEffect, useState} from "react"; import "../css/Article.css" -import {Routes, Route, Link, useNavigate, Navigate } from 'react-router-dom'; -import { useParams } from 'react-router-dom'; +import React, { useEffect, useState, useContext} from 'react'; +import cookie from 'react-cookies'; +import {Routes, Route, Link, useNavigate, Navigate, useParams } from 'react-router-dom'; + +import { UserContext, ArticleContext } from '../Context.js'; + import { DateTime } from 'luxon'; +import axios from 'axios'; +axios.defaults.withCredentials = true; function Comments({data}) { const { _id, content, author, createdAt} = data; - + let params = useParams(); const navigate = useNavigate(); function MoveTo(link){ navigate(link) } - + const articleContext = useContext(ArticleContext); + const userinfo = cookie.load('name') const date = DateTime.fromISO(createdAt).toFormat('yyyy�� MM�� dd�� HH:mm'); - - return ( - <div class="comment"> - <p>{author.nickname}</p> - <p>{content}</p> - <p>{date}</p> - </div> - ); + console.log(articleContext) + function DeleteComment(e) { + const data = {id: _id} + axios + .delete(`http://localhost:8080/post/comment/${params.id}/${_id}`, data, + { + headers: {"Content-Type": 'application/json'} + }) + .then(res => { + alert("The comment is successfully deleted"); + return articleContext.requestLoadArticleById(params.id) + }) + .then(res => { + articleContext.setArticle(res.data) + }) + .catch(err => { + console.error(err); + }); + }; + + if (userinfo.id===author.email) { + return ( + <div class="comment" style={{display: 'flex'}}> + <p>{author.nickname}</p> + <p>{content}</p> + <p>{date}</p> + <button style={{height: '30px'}} onClick={DeleteComment} type="submit">吏��곌린</button> + </div> + ); + } + else { + return ( + <div class="comment" style={{display: 'flex'}}> + <p>{author.nickname}</p> + <p>{content}</p> + <p>{date}</p> + </div> + ); + } + } export default Comments; diff --git a/frontend/src/components/Header.js b/frontend/src/components/Header.js index 0592ecb25f174d3979787db4dc1e9280ac782ca8..02840aa70fa7f30e898c90c50d4b9ea00ad869a6 100644 --- a/frontend/src/components/Header.js +++ b/frontend/src/components/Header.js @@ -41,7 +41,7 @@ function Header({islogged, username}){ <img className="logo_image" alt="logo" src={logo}/> </ButtonLink> <ul> - <p>{userinfo?`${userinfo.name}��, �섏쁺�⑸땲��`:'濡쒓렇�명븯�몄슂.'}</p> + <p>{islogged?`${userinfo.name}��, �섏쁺�⑸땲��`:'濡쒓렇�명븯�몄슂.'}</p> <ul className="menu_list"> <li><ButtonLink link='/'>Home</ButtonLink></li> <li><ButtonLink link='/search'>寃���</ButtonLink></li> diff --git a/frontend/src/pages/Main.js b/frontend/src/pages/Main.js index e467d0f562490c050f994606df10ef6e7705a30b..c9c55c9a933256f6b085570b613b5a02d13b122a 100644 --- a/frontend/src/pages/Main.js +++ b/frontend/src/pages/Main.js @@ -1,5 +1,5 @@ import { useNavigate } from 'react-router-dom'; -import cookie from 'react-cookies'; + import { UserContext } from '../Context.js'; import Article from '../components/Article.js'; import React, { useEffect, useState, useContext} from 'react'; diff --git a/frontend/src/pages/PostRead.js b/frontend/src/pages/PostRead.js index 96390a3bb24fbee1252f86eb45e341d209bc29a6..896cbce241a44cd9fceded3aad205f5d62cf1d99 100644 --- a/frontend/src/pages/PostRead.js +++ b/frontend/src/pages/PostRead.js @@ -1,8 +1,7 @@ -import { useNavigate } from 'react-router-dom'; -import { useParams } from 'react-router-dom'; +import { useNavigate, useParams } from 'react-router-dom'; import React, { useEffect, useState, useContext} from 'react'; import Article from '../components/Article.js'; -import { UserContext } from '../Context.js'; +import { UserContext, ArticleContext } from '../Context.js'; import MapLocator from '../components/MapForLoaction.js'; import Comment from '../components/Comment.js'; @@ -10,81 +9,106 @@ import axios from 'axios'; axios.defaults.withCredentials = true; function PostRead() { - let params = useParams(); - const userContext = useContext(UserContext); - const [article, setArticle] = useState(null) - const navigate = useNavigate(); - function MoveTo(link){ - navigate(link) - } - useEffect(() => { - userContext.CheckSession() - .then((response) => { - if (!response.data) { - alert("�몄뀡�� 留뚮즺�섏뿀�듬땲��. �ㅼ떆 濡쒓렇�� 諛붾엻�덈떎.") - MoveTo('/login') - } - else{ - return requestLoadArticleById(params.id) - } - }) - .then((response) => { - console.log(response) - setArticle(response.data) - }) - .catch((response)=>{ - console.log("error!:LogOut") - console.log(response) - }) + let params = useParams(); + const userContext = useContext(UserContext); + const articleContext = useContext(ArticleContext); - }, []); + const [article, setArticle] = useState(null) + const [inputComment, setInputComment] = useState("") + const [commentList, setCommentList] = useState("") + const navigate = useNavigate(); + function MoveTo(link){ + navigate(link) + } + useEffect(() => { + userContext.CheckSession() + .then((response) => { + if (!response.data) { + alert("�몄뀡�� 留뚮즺�섏뿀�듬땲��. �ㅼ떆 濡쒓렇�� 諛붾엻�덈떎.") + MoveTo('/login') + } + else{ + return requestLoadArticleById(params.id) + } + }) + .then((response) => { + console.log(response) + setArticle(response.data) + }) + .catch((response)=>{ + console.log("error!:LogOut") + console.log(response) + }) - console.log(article); - console.log(article?Object.values(article):"no"); + }, []); - if (article) { - return( - <> - <div className="introduction" style={{display: 'flex'}}> - <MapLocator loc={{lat: article.latitude, lng: article.longitude}} keyword={article.keyword}></MapLocator> - <div> - <Article data={article}></Article> - <button>議곗���</button> - </div> - </div> - <form> - <div style={{display: 'flex'}}> - <button>�볤� �묒꽦</button> - <input type="text"></input> - </div> - </form> - { - article.comments.map((el)=>{ - return( - <Comment key={el._id} data={el}></Comment> - ) - }) - } - </> + const onTextChange = (e) => { + const {value} = e.target; + setInputComment(value); + } - ) - } - else { - return( + const onSubmit = e => { + e.preventDefault(); + if (!inputComment) { + alert("�볤��� �묒꽦�댁<�몄슂."); + return + } + const data = {content: inputComment} + axios.post(`http://localhost:8080/post/comment/${params.id}`, data, + { + headers: {"Content-Type": 'application/json'} + }) + .then(res => { + alert("The comment is successfully uploaded"); + }) + .catch(err => { + console.error(err); + }); + }; + + if (article) { + return( + <> <div className="introduction" style={{display: 'flex'}}> - <p>濡쒕뵫以�</p> + <MapLocator loc={{lat: article.latitude, lng: article.longitude}} keyword={article.keyword}></MapLocator> + <div> + <Article data={article}></Article> + <button>議곗���</button> + </div> </div> - ) - } - + <form onSubmit={onSubmit}> + <div style={{display: 'flex'}}> + <button>�볤� �묒꽦</button> + <input type="text" onChange={onTextChange}></input> + </div> + </form> + <ArticleContext.Provider value={{requestLoadArticleById, setArticle}}> + { + article.comments.map((el)=>{ + return( + <Comment key={el._id} data={el}></Comment> + ) + }) + } + </ArticleContext.Provider> + </> + ) + } + else { + return( + <div className="introduction" style={{display: 'flex'}}> + <p>濡쒕뵫以�</p> + </div> + ) } +} - async function requestLoadArticleById(id) { - const response = await axios({ - url: `http://localhost:8080/post/loadarticle/${id}`, // �듭떊�� �밸Ц�� - method: 'get', // �듭떊�� 諛⑹떇 - }); - return response; - } +async function requestLoadArticleById(id) { + const response = await axios({ + url: `http://localhost:8080/post/loadarticle/${id}`, // �듭떊�� �밸Ц�� + method: 'get', // �듭떊�� 諛⑹떇 + }); + return response; +} - export default PostRead; \ No newline at end of file +export default PostRead; \ No newline at end of file diff --git a/frontend/src/pages/PostWrite.js b/frontend/src/pages/PostWrite.js index 58259a9dfd09f9caf86baa9cb18c02f3dbdda098..4b90e484470bfb33230150130c63b249af429096 100644 --- a/frontend/src/pages/PostWrite.js +++ b/frontend/src/pages/PostWrite.js @@ -1,6 +1,5 @@ import React, { useRef, useState, useEffect, useContext } from 'react'; import { useNavigate } from 'react-router-dom'; - import { UserContext } from '../Context.js'; import SearchMap from '../components/SearchMapByKeyword.js' diff --git a/frontend/src/pages/Search.js b/frontend/src/pages/Search.js index 43eb6a88e80d4c10f21173861d58e845a9a4f4c8..c380e2dbce3f47c05dfab81662276cd101025f0d 100644 --- a/frontend/src/pages/Search.js +++ b/frontend/src/pages/Search.js @@ -5,40 +5,6 @@ import SearchMap from '../components/SearchMapByKeyword.js' import { UserContext } from '../Context.js'; const {kakao} = window; -//移댁뭅�� 吏��� API瑜� �댁슜�� 寃���(�덉젙) -/* -function SearchMap(props){ - // �꾩<���숆탳瑜� 湲곕낯 �꾩튂濡� �ㅼ젙 - const [state, setState] = useState({ - center: { lat: 37.28238488648025, lng: 127.04350967609274 }, - isPanto: true, - }); - const [searchAddress, SetSearchAddress] = useState(); //二쇱냼 �낅젰 - - const handleInput = (e) => { - SetSearchAddress(e.target.value); - } - - const SearchStart = () => { - console.log('Start Search!');// action test - // 二쇱냼瑜� 湲곕컲�쇰줈 �� 寃��� 援ы쁽(�쒖<�밸퀎�먯튂�� �쒖<�� 泥⑤떒濡� 242 瑜� �낅젰�섎㈃ kakao媛� �섏삩��) - const geocoder = new kakao.maps.services.Geocoder(); - let callback = function(result, status) { - if (status === kakao.maps.services.Status.OK) { - const newSearch = result[0] - setState({ - center: { lat: newSearch.y, lng: newSearch.x } - }) - } - }; - geocoder.addressSearch(`${searchAddress}`, callback); - } - - -} -*/ - - function Search(props) { const userContext = useContext(UserContext);