83 lines
2.1 KiB
JavaScript
83 lines
2.1 KiB
JavaScript
const User = require("../../users/models/user").User;
|
|
const Blogs = require("../models/Blogs");
|
|
const Categories = require("../models/Categories");
|
|
const SavedBlogs = require("../models/SavedBlogs");
|
|
|
|
exports.getBlogs = async function({myUserId}){
|
|
console.log(myUserId, 456);
|
|
return await Blogs.findAll({
|
|
include: [User, Categories, {
|
|
model: SavedBlogs,
|
|
where: {
|
|
userId: myUserId
|
|
},
|
|
required: false
|
|
}],
|
|
order: [["createdAt", "DESC"]]
|
|
});
|
|
};
|
|
|
|
exports.getBlogsWithUsername = async function({username, category, myUserId}){
|
|
return await Blogs.findAll({
|
|
include:[(category) ? {
|
|
model: Categories,
|
|
where: {
|
|
title: category
|
|
}
|
|
} : Categories, {
|
|
model: User,
|
|
where: {
|
|
username: username
|
|
}
|
|
},{
|
|
model: SavedBlogs,
|
|
where: {
|
|
userId: myUserId
|
|
},
|
|
required: false
|
|
}],
|
|
order: [["createdAt", "DESC"]]
|
|
});
|
|
};
|
|
|
|
exports.getBlogsWithCategory = async function({categoryId, categoryTitle, myUserId}){
|
|
return await Blogs.findAll({
|
|
include: [User, Categories, {
|
|
model: Categories,
|
|
where: [(categoryId) && {
|
|
id: categoryId
|
|
}, (categoryTitle) && {
|
|
title: categoryTitle
|
|
}]
|
|
},{
|
|
model: SavedBlogs,
|
|
where: {
|
|
userId: myUserId
|
|
},
|
|
required: false
|
|
}],
|
|
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
|
|
}
|
|
});
|
|
}; |