Node/modules/blogs/middlewares/query.js

59 lines
1.5 KiB
JavaScript

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