Update: Ad.findAll
This commit is contained in:
parent
d2044c2b28
commit
61813b0644
1 changed files with 26 additions and 14 deletions
|
|
@ -51,21 +51,33 @@ exports.findAll = (req, res) => {
|
||||||
let query = {};
|
let query = {};
|
||||||
let condition;
|
let condition;
|
||||||
|
|
||||||
const playlistId = req.query.playlistId;
|
const adId = req.query.adId;
|
||||||
condition = playlistId ? playlistId : undefined;
|
condition = adId ? adId : undefined;
|
||||||
query._id = condition;
|
query._id = condition;
|
||||||
|
|
||||||
const userId = req.query.userId;
|
const userId = req.query.userId;
|
||||||
condition = userId ? userId : undefined;
|
condition = userId ? userId : undefined;
|
||||||
query.userId = condition;
|
query.userId = condition;
|
||||||
|
|
||||||
const videoIds = req.query.videoIds;
|
const title = req.query.title;
|
||||||
condition = videoIds ? {$in: videoIds} : undefined;
|
condition = title ? { $regex: new RegExp(title), $options: "i" } : undefined;
|
||||||
query.videoIds = condition;
|
query.title = condition;
|
||||||
|
|
||||||
const name = req.query.name;
|
const url = req.query.url;
|
||||||
condition = name ? { $regex: new RegExp(name), $options: "i" } : undefined;
|
condition = url ? { $regex: new RegExp(url), $options: "i" } : undefined;
|
||||||
query.name = condition;
|
query.url = condition;
|
||||||
|
|
||||||
|
const interests = req.query.interests;
|
||||||
|
condition = interests ? {$in: interests} : undefined;
|
||||||
|
query["interests.interest"] = condition
|
||||||
|
|
||||||
|
const comment = req.query.comment;
|
||||||
|
condition = comment ? { $regex: new RegExp(comment), $options: "i" } : undefined;
|
||||||
|
query.comment = condition;
|
||||||
|
|
||||||
|
const isVisible = req.query.isVisible;
|
||||||
|
condition = isVisible ? isVisible : undefined;
|
||||||
|
query.isVisible = condition;
|
||||||
|
|
||||||
const isActive = req.query.isActive;
|
const isActive = req.query.isActive;
|
||||||
condition = isActive ? isActive : undefined;
|
condition = isActive ? isActive : undefined;
|
||||||
|
|
@ -75,10 +87,10 @@ exports.findAll = (req, res) => {
|
||||||
if(sort !== 'undefined'){
|
if(sort !== 'undefined'){
|
||||||
switch (sort){
|
switch (sort){
|
||||||
case 'asc':
|
case 'asc':
|
||||||
condition = {name: 1};
|
condition = {title: 1};
|
||||||
break;
|
break;
|
||||||
case 'desc':
|
case 'desc':
|
||||||
condition = {name: -1};
|
condition = {title: -1};
|
||||||
break;
|
break;
|
||||||
case 'createdAtAsc':
|
case 'createdAtAsc':
|
||||||
condition = {createdAt: 1};
|
condition = {createdAt: 1};
|
||||||
|
|
@ -93,7 +105,7 @@ exports.findAll = (req, res) => {
|
||||||
condition = {updatedAt: -1};
|
condition = {updatedAt: -1};
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
condition = {name: 1};
|
condition = {title: 1};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
const query_sort = {sort: condition};
|
const query_sort = {sort: condition};
|
||||||
|
|
@ -102,12 +114,12 @@ exports.findAll = (req, res) => {
|
||||||
Object.keys(query).forEach(key => query[key] === undefined ? delete query[key] : {});
|
Object.keys(query).forEach(key => query[key] === undefined ? delete query[key] : {});
|
||||||
console.log(query);
|
console.log(query);
|
||||||
|
|
||||||
Playlist.find(query, {}, query_sort)
|
Ad.find(query, {}, query_sort)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
return sendMessage(res, 22, data, token);
|
return sendMessage(res, 42, data, token);
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
return sendError(res,500,100,err.message || "Some error occurred while finding the Playlists.", token);
|
return sendError(res,500,100,err.message || "Some error occurred while finding the Ads.", token);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Reference in a new issue