고병후

Create user information router

...@@ -29,9 +29,6 @@ COMMENT = 'post information' ...@@ -29,9 +29,6 @@ COMMENT = 'post information'
29 DEFAULT CHARACTER SET = utf8 29 DEFAULT CHARACTER SET = utf8
30 ENGINE = InnoDB; 30 ENGINE = InnoDB;
31 31
32 -<후 처리>
33 -ALTER TABLE tunnel.posts MODIFY post TEXT CHARACTER SET utf8mb4;
34 -:게시물에 이모지 사용가능하게 칼럼 수정.
35 32
36 <댓글> 33 <댓글>
37 CREATE TABLE tunnel.comments( 34 CREATE TABLE tunnel.comments(
...@@ -44,8 +41,6 @@ PRIMARY KEY(id), ...@@ -44,8 +41,6 @@ PRIMARY KEY(id),
44 INDEX postid_idx (postid ASC), 41 INDEX postid_idx (postid ASC),
45 CONSTRAINT post_id FOREIGN KEY (postid) REFERENCES tunnel.posts (id) 42 CONSTRAINT post_id FOREIGN KEY (postid) REFERENCES tunnel.posts (id)
46 ON DELETE CASCADE 43 ON DELETE CASCADE
47 -ON UPDATE CASCADE,
48 -CONSTRAINT user_id FOREIGN KEY (userid) REFERENCES tunnel.users (name)
49 ON UPDATE CASCADE 44 ON UPDATE CASCADE
50 ) 45 )
51 46
...@@ -53,6 +48,11 @@ COMMENT = 'post information' ...@@ -53,6 +48,11 @@ COMMENT = 'post information'
53 DEFAULT CHARACTER SET = utf8 48 DEFAULT CHARACTER SET = utf8
54 ENGINE = InnoDB; 49 ENGINE = InnoDB;
55 50
51 +[후처리 보류]
52 +
53 +<후 처리>
54 +ALTER TABLE tunnel.posts MODIFY post TEXT CHARACTER SET utf8mb4;
55 +:게시물에 이모지 사용가능하게 칼럼 수정.
56 <후 처리> 56 <후 처리>
57 ALTER TABLE tunnel.comments MODIFY comment TEXT CHARACTER SET utf8mb4; 57 ALTER TABLE tunnel.comments MODIFY comment TEXT CHARACTER SET utf8mb4;
58 : 댓글에 이모지 사용가능하게 칼럼 수정. 58 : 댓글에 이모지 사용가능하게 칼럼 수정.
...@@ -60,6 +60,8 @@ ALTER TABLE tunnel.comments MODIFY comment TEXT CHARACTER SET utf8mb4; ...@@ -60,6 +60,8 @@ ALTER TABLE tunnel.comments MODIFY comment TEXT CHARACTER SET utf8mb4;
60 60
61 61
62 <명령어> 62 <명령어>
63 +SELECT * FROM tunnel.users
64 +mysql -h localhost -u root -p
63 show databases; 65 show databases;
64 use tunnel; 66 use tunnel;
65 show tables; 67 show tables;
......
...@@ -3,6 +3,11 @@ const Sequelize = require('sequelize'); ...@@ -3,6 +3,11 @@ 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 + userid:{
7 + type: Sequelize.STRING(30),
8 + allowNull: false,
9 + unique:true,
10 + },
6 comment:{ 11 comment:{
7 type: Sequelize.TEXT, 12 type: Sequelize.TEXT,
8 allowNull: false, 13 allowNull: false,
...@@ -25,5 +30,7 @@ module.exports = class Comment extends Sequelize.Model { ...@@ -25,5 +30,7 @@ module.exports = class Comment extends Sequelize.Model {
25 } 30 }
26 31
27 32
28 - static associate(db) {} 33 + static associate(db) {
34 + db.Comment.belongsTo(db.Post,{foreignKey: 'postid', targetKey:'id' });
35 + }
29 }; 36 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -33,5 +33,7 @@ module.exports = class Post extends Sequelize.Model { ...@@ -33,5 +33,7 @@ module.exports = class Post extends Sequelize.Model {
33 } 33 }
34 34
35 35
36 - static associate(db) {} 36 + static associate(db) {
37 + db.Post.belongsTo(db.User,{foreignKey: 'userid', targetKey:'name' });
38 + }
37 }; 39 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -33,5 +33,7 @@ module.exports = class User extends Sequelize.Model { ...@@ -33,5 +33,7 @@ module.exports = class User extends Sequelize.Model {
33 } 33 }
34 34
35 35
36 - static associate(db) {} 36 + static associate(db) {
37 + db.User.hasMany(db.Post,{foreignKey: 'userid', sourceKey:'name' });
38 + }
37 }; 39 };
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -25,7 +25,7 @@ router.post('/',(req,res)=>{ ...@@ -25,7 +25,7 @@ router.post('/',(req,res)=>{
25 res.status(401).send("pwError"); 25 res.status(401).send("pwError");
26 } 26 }
27 else if(result.dataValues.pw == req.body.Password){ 27 else if(result.dataValues.pw == req.body.Password){
28 - req.session.id = result.dataValues.id; 28 + req.session.userid = result.dataValues.id;
29 req.session.name = result.dataValues.name; 29 req.session.name = result.dataValues.name;
30 req.session.personality = result.dataValues.personality; 30 req.session.personality = result.dataValues.personality;
31 req.session.status = result.dataValues.status; 31 req.session.status = result.dataValues.status;
......
1 const express = require("express"); 1 const express = require("express");
2 const bodyParser = require("body-parser"); 2 const bodyParser = require("body-parser");
3 const router = express.Router(); 3 const router = express.Router();
4 - 4 +const auth = require("../obj/authorize");
5 const {User}=require('../models'); 5 const {User}=require('../models');
6 6
7 -router.get('/',(req,res)=>{
8 -
9 -});
10 7
11 //로그아웃 8 //로그아웃
12 -router.post('/',(req,res)=>{ 9 +router.get('/',auth,(req,res)=>{
13 - if(result.dataValues.pw == req.body.Password){ 10 + req.session.userid = null;
14 - req.session.id = null;
15 req.session.name = null; 11 req.session.name = null;
16 req.session.personality = null; 12 req.session.personality = null;
17 req.session.status = null; 13 req.session.status = null;
18 req.session.isAuthorized = null; 14 req.session.isAuthorized = null;
19 console.log("Logout"); 15 console.log("Logout");
20 res.sendStatus(200); 16 res.sendStatus(200);
21 - }
22 }) 17 })
23 18
24 module.exports = router; 19 module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
......
...@@ -9,7 +9,7 @@ router.get('/',auth,(req,res)=>{ ...@@ -9,7 +9,7 @@ router.get('/',auth,(req,res)=>{
9 //메인페이지 9 //메인페이지
10 10
11 //테스트 코드 11 //테스트 코드
12 - console.log(req.session.id); 12 + console.log(req.session.userid);
13 res.send(req.session.name); 13 res.send(req.session.name);
14 }); 14 });
15 15
......
1 +const express = require("express");
2 +const bodyParser = require("body-parser");
3 +const router = express.Router();
4 +const auth = require("../obj/authorize");
5 +
6 +const {User}=require('../models'); //유저정보 db연결
7 +const {Post}=require('../models'); //게시물정보 db연결
8 +
9 +//현재 로그인된 사용자의 게시물 배열 응답
10 +router.get('/',auth,(req,res)=>{
11 + Post.findAll({
12 + where:{userid: req.session.name}
13 + })
14 + .then((result)=>{
15 + //게시물이 0개인 경우
16 + if(result === null || result === undefined){
17 + console.log("해당유저의 게시물이 없습니다.")
18 + res.status(401).send("null");
19 + }
20 + else{
21 + console.log(result.length);
22 + res.sendStatus(200);
23 + }
24 + })
25 +});
26 +
27 +//게시물 작성
28 +router.post('/',auth,(req,res)=>{
29 + try{
30 + Post.create({
31 + userid : req.session.name,
32 + title : "프로트랑 맞추기",
33 + post:"프론트랑 맞추기",
34 + status: false
35 + })
36 + console.log("게시");
37 + res.sendStatus(200);
38 + } catch(err){
39 + console.log("실패");
40 + res.send(err);
41 + }
42 + /*
43 + User.create({
44 +
45 + name: req.body.Id,
46 + pw:req.body.Password,
47 + personality:req.body.Personality,
48 + status:false
49 + */
50 +});
51 +
52 +
53 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
1 +const express = require("express");
2 +const bodyParser = require("body-parser");
3 +const router = express.Router();
4 +
5 +const auth = require("../obj/authorize");
6 +
7 +//로그인 된 회원정보 반환
8 +router.get('/',auth,(req,res)=>{
9 + res.status(200).json({
10 + Id :req.session.userid,
11 + Password :req.session.name,
12 + Personality : req.session.personality,
13 + Status: req.session.status
14 + })
15 +});
16 +
17 +
18 +
19 +
20 +
21 +module.exports = router;
...\ No newline at end of file ...\ No newline at end of file
...@@ -16,6 +16,8 @@ const registerRouter = require('./routes/register.js'); ...@@ -16,6 +16,8 @@ const registerRouter = require('./routes/register.js');
16 const mainRouter = require('./routes/main.js'); 16 const mainRouter = require('./routes/main.js');
17 const logoutRouter = require('./routes/logout.js'); 17 const logoutRouter = require('./routes/logout.js');
18 const authRouter = require('./routes/auth.js'); 18 const authRouter = require('./routes/auth.js');
19 +const userRouter = require('./routes/user.js');
20 +const postRouter = require('./routes/post.js');
19 21
20 const app = express(); 22 const app = express();
21 app.set('port', process.env.PORT || 3001); 23 app.set('port', process.env.PORT || 3001);
...@@ -53,6 +55,8 @@ app.use('/api/login',loginRouter); //로그인 페이지 ...@@ -53,6 +55,8 @@ app.use('/api/login',loginRouter); //로그인 페이지
53 app.use('/api/main',mainRouter);//메인페이지 55 app.use('/api/main',mainRouter);//메인페이지
54 app.use('/api/auth',authRouter);//가입여부 확인 56 app.use('/api/auth',authRouter);//가입여부 확인
55 app.use('/api/logout',logoutRouter);//로그아웃 57 app.use('/api/logout',logoutRouter);//로그아웃
58 +app.use('/api/user',userRouter);//유저정보 응답
59 +app.use('/api/post',postRouter);//유저정보 응답
56 60
57 61
58 62
......