FP8: controllers/comments
38772 단어 ControllersquelizeJWTController
- Json Web Token
- Sequelize
// 'index.js' from 'controllers/comments' Directory
module.exports = {
create: require('./create'),
read: require('./read'),
update: require('./update'),
remove: require('./remove'),
};
// 'create.js' from 'controllers/comments' Directory
const { Comment } = require('../../models');
const jwt = require('jsonwebtoken');
require('dotenv').config();
module.exports = {
post: async (req, res) => {
if (!req.body.contents || !req.body.postId) {
res.status(400).json({
data: null,
message: "insufficient parameters supplied"
});
}
if (!req.headers['authorization']) {
res.status(403).json({ data: null, message: "invalid access token" });
}
const accessToken = req.headers['authorization'].split(' ')[1];
const payload = await jwt.verify(accessToken, process.env.ACCESS_SECRET);
const newComment = await Comment.create({
userId: payload.id,
contents: req.body.contents,
postId: req.body.postId,
username: req.body.username,
profileUrl: req.body.profileUrl
});
const { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl } = newComment;
res.status(201).json({
commentData: { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl },
message: "created ok"
});
}
};
// 'read.js' from 'controllers/comments' Directory
const { Comment } = require('../../models');
module.exports = {
getComments: async (req, res) => {
if (!req.params.postId) {
res.status(400).json({
data: null,
message: "insufficient parameters supplied"
});
}
const foundComments = await Comment.findAll({
where: {
postId: req.params.postId
}
});
const results = foundComments.map(comment => comment.dataValues);
res.status(200).json({
commentsData: results,
message: "ok"
});
},
getComment: async (req, res) => {
if (!req.params.id) {
res.status(400).json({
data: null,
message: "insufficient parameters supplied"
});
}
const foundComment = await Comment.findOne({
where: {
id: req.params.id
}
});
const { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl } = foundComment;
res.status(200).json({
commentData: { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl },
message: "ok"
});
}
};
// 'update.js' from 'controllers/comments' Directory
const { Comment } = require('../../models');
const jwt = require('jsonwebtoken');
require('dotenv').config();
module.exports = {
put: async (req, res) => {
if (!req.params.id || !req.body.contents || !req.body.postId) {
res.status(400).json({
data: null,
message: "insufficient parameters supplied"
});
}
if (!req.headers['authorization']) {
res.status(403).json({
data: null,
message: "invalid access token"
});
}
const accessToken = req.headers['authorization'].split(' ')[1];
const payload = await jwt.verify(accessToken, process.env.ACCESS_SECRET);
const foundComment = await Comment.findOne({
where: {
id: req.params.id
}
});
if (!foundComment) {
res.status(404).json({
data: null,
message: "not found comment"
});
} else {
await Comment.update(
{
contents: req.body.contents,
userId: payload.id,
postId: req.body.postId,
username: req.body.username,
profileUrl: req.body.profileUrl
},
{
where: {
id: req.params.id
}
}
);
const updatedComment = await Comment.findOne({
where: {id: req.params.id}
})
const { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl } = updatedComment;
res.status(200).json({
commentData: { id, contents, userId, postId, createdAt, updatedAt, username, profileUrl },
message: "updated ok"
});
}
}
};
// 'remove.js' from 'controllers/comments' Directory
const { Comment } = require('../../models');
const jwt = require('jsonwebtoken');
require('dotenv').config();
module.exports = {
delete: async (req, res) => {
if (!req.params.id) {
res.status(400).json({
data: null,
message: "insufficient parameters supplied"
});
}
if (!req.headers['authorization']) {
res.status(403).json({
data: null,
message: "invalid access token"
});
}
const accessToken = req.headers['authorization'].split(' ')[1];
const payload = await jwt.verify(accessToken, process.env.ACCESS_SECRET);
const foundComment = await Comment.findOne({
where: {
id: req.params.id,
userId: payload.id
}
});
if (!foundComment) {
res.status(404).json({
data: null,
message: "not found comment"
});
} else {
await Comment.destroy({
where: {
id: req.params.id
}
});
res.status(200).json({
data: null,
message: "removed ok"
});
}
}
};
Author And Source
이 문제에 관하여(FP8: controllers/comments), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://velog.io/@charlie-lyc/FP8-controllerscomments저자 귀속: 원작자 정보가 원작자 URL에 포함되어 있으며 저작권은 원작자 소유입니다.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)