고병후

Create user information router

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