FP8: controllers/comments

Charlie·2021년 1월 11일
0

First Project

목록 보기
8/8
post-thumbnail
  • 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"
            });
        }
    }

};

1개의 댓글

comment-user-thumbnail
2023년 10월 23일

Lots of data and metrics to consider. I find this interesting and easy to access, expanding my knowledge while learning free games command functions.

답글 달기