Showing
5 changed files
with
32 additions
and
28 deletions
... | @@ -3,6 +3,10 @@ const Sequelize = require('sequelize'); | ... | @@ -3,6 +3,10 @@ const Sequelize = require('sequelize'); |
3 | module.exports = class Comment extends Sequelize.Model { | 3 | module.exports = class Comment extends Sequelize.Model { |
4 | static init(sequelize) { | 4 | static init(sequelize) { |
5 | return super.init({ | 5 | return super.init({ |
6 | + postid:{ | ||
7 | + type: Sequelize.INTEGER, | ||
8 | + allowNull: false, | ||
9 | + }, | ||
6 | userid:{ | 10 | userid:{ |
7 | type: Sequelize.STRING(30), | 11 | type: Sequelize.STRING(30), |
8 | allowNull: false, | 12 | allowNull: false, |
... | @@ -31,6 +35,6 @@ module.exports = class Comment extends Sequelize.Model { | ... | @@ -31,6 +35,6 @@ module.exports = class Comment extends Sequelize.Model { |
31 | 35 | ||
32 | 36 | ||
33 | static associate(db) { | 37 | static associate(db) { |
34 | - db.Comment.belongsTo(db.Post,{foreignKey: 'postid', targetKey:'id' }); | 38 | + //db.Comment.belongsTo(db.Post,{foreignKey: 'postid', targetKey:'id' }); |
35 | } | 39 | } |
36 | }; | 40 | }; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -35,6 +35,5 @@ module.exports = class Post extends Sequelize.Model { | ... | @@ -35,6 +35,5 @@ module.exports = class Post extends Sequelize.Model { |
35 | 35 | ||
36 | static associate(db) { | 36 | static associate(db) { |
37 | db.Post.belongsTo(db.User,{foreignKey: 'userid', targetKey:'name' }); | 37 | db.Post.belongsTo(db.User,{foreignKey: 'userid', targetKey:'name' }); |
38 | - db.Post.hasMany(db.Post,{foreignKey: 'postid', sourceKey:'id' }); | ||
39 | } | 38 | } |
40 | }; | 39 | }; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -7,11 +7,12 @@ const {User}=require('../models'); //유저정보 db연결 | ... | @@ -7,11 +7,12 @@ const {User}=require('../models'); //유저정보 db연결 |
7 | const {Post}=require('../models'); //게시물정보 db연결 | 7 | const {Post}=require('../models'); //게시물정보 db연결 |
8 | const {Comment}=require('../models'); | 8 | const {Comment}=require('../models'); |
9 | 9 | ||
10 | + | ||
10 | //현재 로그인된 사용자의 게시물 배열 응답 | 11 | //현재 로그인된 사용자의 게시물 배열 응답 |
11 | -router.get('/:boardId',auth,(req,res)=>{ | 12 | +router.post('/reply',auth,(req,res)=>{ |
12 | 13 | ||
13 | Comment.findAll({ | 14 | Comment.findAll({ |
14 | - where:{postid: req.params.boardId}, | 15 | + where:{postid: req.body.id}, |
15 | order: [['created_at', 'ASC']], | 16 | order: [['created_at', 'ASC']], |
16 | }) | 17 | }) |
17 | .then((result)=>{ | 18 | .then((result)=>{ |
... | @@ -28,15 +29,14 @@ router.get('/:boardId',auth,(req,res)=>{ | ... | @@ -28,15 +29,14 @@ router.get('/:boardId',auth,(req,res)=>{ |
28 | // } | 29 | // } |
29 | }) | 30 | }) |
30 | }); | 31 | }); |
31 | - | ||
32 | - | ||
33 | 32 | ||
34 | //게시물 작성 | 33 | //게시물 작성 |
35 | -router.post('/:boardId',auth,(req,res)=>{ | 34 | +router.post('/write',auth,(req,res)=>{ |
35 | + console.log(req.params.id); | ||
36 | try{ | 36 | try{ |
37 | Comment.create({ | 37 | Comment.create({ |
38 | userid : req.session.name, | 38 | userid : req.session.name, |
39 | - postid : req.params.boardId, | 39 | + postid : req.body.postid, |
40 | comment : req.body.comment, | 40 | comment : req.body.comment, |
41 | }) | 41 | }) |
42 | console.log("게시"); | 42 | console.log("게시"); |
... | @@ -55,5 +55,9 @@ router.post('/:boardId',auth,(req,res)=>{ | ... | @@ -55,5 +55,9 @@ router.post('/:boardId',auth,(req,res)=>{ |
55 | */ | 55 | */ |
56 | }); | 56 | }); |
57 | 57 | ||
58 | + | ||
59 | + | ||
60 | + | ||
61 | + | ||
58 | 62 | ||
59 | module.exports = router; | 63 | module.exports = router; |
... | \ No newline at end of file | ... | \ No newline at end of file | ... | ... |
... | @@ -8,9 +8,8 @@ const {Post}=require('../models'); //게시물정보 db연결 | ... | @@ -8,9 +8,8 @@ const {Post}=require('../models'); //게시물정보 db연결 |
8 | 8 | ||
9 | //현재 로그인된 사용자의 게시물 배열 응답 | 9 | //현재 로그인된 사용자의 게시물 배열 응답 |
10 | router.get('/',auth,(req,res)=>{ | 10 | router.get('/',auth,(req,res)=>{ |
11 | - | ||
12 | Post.findAll({ | 11 | Post.findAll({ |
13 | - where:{userid: req.session.name}, | 12 | + // where:{userid: req.session.name}, |
14 | order: [['created_at', 'DESC']], | 13 | order: [['created_at', 'DESC']], |
15 | }) | 14 | }) |
16 | .then((result)=>{ | 15 | .then((result)=>{ | ... | ... |
... | @@ -7,8 +7,8 @@ import '../style/ContentModal.scss' | ... | @@ -7,8 +7,8 @@ import '../style/ContentModal.scss' |
7 | function ContentModal({element}) { | 7 | function ContentModal({element}) { |
8 | const [viewComment,setviewComment] = useState([]); | 8 | const [viewComment,setviewComment] = useState([]); |
9 | useEffect(()=>{ | 9 | useEffect(()=>{ |
10 | - Axios.get('/api/comment/'+element.id).then((response)=>{ | 10 | + Axios.post('/api/comment/reply',{id : element.id}).then((response)=>{ |
11 | - setviewComment(response.data); | 11 | + setviewComment(response.data); |
12 | }) | 12 | }) |
13 | },[viewComment]) | 13 | },[viewComment]) |
14 | 14 | ||
... | @@ -17,22 +17,20 @@ function ContentModal({element}) { | ... | @@ -17,22 +17,20 @@ function ContentModal({element}) { |
17 | setOpen(false); | 17 | setOpen(false); |
18 | } | 18 | } |
19 | const [open, setOpen] = useState(false) | 19 | const [open, setOpen] = useState(false) |
20 | - const [BoardComment, setBoardComment] = useState({ | 20 | + const [BoardComment, setBoardComment] = useState("") |
21 | - id: null, | ||
22 | - content:'' | ||
23 | - }) | ||
24 | const onCommentHandler = (event) => { | 21 | const onCommentHandler = (event) => { |
25 | setBoardComment(event.currentTarget.value) | 22 | setBoardComment(event.currentTarget.value) |
26 | console.log(BoardComment) | 23 | console.log(BoardComment) |
27 | } | 24 | } |
28 | - const onSubmitHandler = () => { | 25 | + const onSubmitHandler = () => { |
29 | - Axios.post('/api/comment',{ | 26 | + Axios.post('/api/comment/write',{ |
30 | - id: element.id, | 27 | + postid: element.id, |
31 | - content: BoardComment | 28 | + comment: BoardComment |
32 | }) | 29 | }) |
33 | .then((res)=>{ | 30 | .then((res)=>{ |
34 | if(res.status === 200){ | 31 | if(res.status === 200){ |
35 | alert("댓글 작성을 완료하였습니다.") | 32 | alert("댓글 작성을 완료하였습니다.") |
33 | + setOpen(false); | ||
36 | } | 34 | } |
37 | }).catch((error) => { | 35 | }).catch((error) => { |
38 | console.log(error.response) | 36 | console.log(error.response) |
... | @@ -57,20 +55,20 @@ function ContentModal({element}) { | ... | @@ -57,20 +55,20 @@ function ContentModal({element}) { |
57 | </Modal.Description> | 55 | </Modal.Description> |
58 | </Modal.Content> | 56 | </Modal.Content> |
59 | <Modal.Content> | 57 | <Modal.Content> |
60 | - {viewComment&&viewComment.map(elem =>{ | 58 | + {viewComment&&viewComment.map(elem =>{ |
61 | return <div className="ui segment"> | 59 | return <div className="ui segment"> |
62 | - <h2>{elem.title}</h2> | 60 | + <h2>{elem.userid}</h2> |
63 | - <h4>{elem.created_at.slice(0,10)+" " +elem.created_at.slice(11,16)}</h4> | 61 | + <h4>{elem.comment}</h4> |
64 | </div>} | 62 | </div>} |
65 | - )} | 63 | + )} |
66 | </Modal.Content> | 64 | </Modal.Content> |
67 | <Modal.Actions> | 65 | <Modal.Actions> |
68 | - <Comment> | 66 | + <Comment> |
69 | <Form reply> | 67 | <Form reply> |
70 | - <Form.TextArea onChange={onCommentHandler}/> | 68 | + <Form.TextArea value={BoardComment} onChange={onCommentHandler}/> |
71 | <div onClick={handleClose}> | 69 | <div onClick={handleClose}> |
72 | - <Button content='댓글 남기기' labelPosition='left' icon='edit' primary onSubmit={onSubmitHandler}/> | 70 | + <Button content='댓글 남기기' onClick={onSubmitHandler} labelPosition='left' icon='edit' primary /> |
73 | - <Button color='black'>닫기</Button> | 71 | + <Button onClick={handleClose} color='black'>닫기</Button> |
74 | </div> | 72 | </div> |
75 | </Form> | 73 | </Form> |
76 | </Comment> | 74 | </Comment> | ... | ... |
-
Please register or login to post a comment