Node/modules/blogs/middlewares/query.js

44 lines
1.1 KiB
JavaScript

const User = require("../../users/models/user");
const Blogs = require("../models/Blogs");
const Categories = require("../models/Categories");
exports.getBlogs = async function(){
return await Blogs.findAll({
include: [User, Categories]
});
};
exports.getBlogsWithUsername = async function({username}){
return await Blogs.findAll({
include:[Categories, {
model: User,
where: {
username: username
}
}]
});
};
exports.getBlogsWithCategory = async function({categoryId, categoryTitle}){
return await Blogs.findAll({
include: [User, Categories, {
model: Categories,
where: [(categoryId) && {
id: categoryId
}, (categoryTitle) && {
title: categoryTitle
}]
}]
});
};
// CATE
exports.getCategoryWithParams = async function({categoryId, categoryTitle}){
return await Categories.findOne({
where:[
(categoryId) ? {id: categoryId} : {},
(categoryTitle) ? {title: categoryTitle} : {}
]
});
};