Untitled

 avatar
unknown
plain_text
a year ago
960 B
2
Indexable
const { BlogPost, Category, PostCategory, User } = require('../models');

const noCreativity = [
  {
    model: User,
    as: 'user',
    attributes: { exclude: ['password'] },
  },
  {
    model: Category,
    as: 'categories',
    through: { attributes: [] },
  },
];

const makeNewPost = async ({ content, categoryIds, title }, userId) => {
  const newPost = new Date();
  const category1 = await Category.findAll({ where: { id: categoryIds } });
  const pageBody = { title, content, userId, updated: newPost, published: newPost };
  const post = await BlogPost.create(pageBody);
  const postCategories = category1
    .map((category) => ({ postId: post.id, categoryId: category.id }));
  await PostCategory.bulkCreate(postCategories);
  return post;
};

const findAllPosts = async (userId) => {
  const findPosts = await BlogPost.findAll({ where: { userId }, include: noCreativity });
  return findPosts;
};

module.exports = { makeNewPost, findAllPosts };
Leave a Comment