Update: Video.findAll
This commit is contained in:
parent
0cf76acdfe
commit
e52d756d3b
1 changed files with 61 additions and 1 deletions
|
|
@ -288,7 +288,67 @@ exports.create = (req, res) => {
|
|||
exports.findAll = (req, res) => {
|
||||
const token = checkLogin(req, res);
|
||||
if(token){
|
||||
return sendError(res, 501, -1, "Video.findAll not Implemented", token);
|
||||
let query = {};
|
||||
let condition;
|
||||
|
||||
const userId = req.query.userId;
|
||||
condition = userId ? userId : undefined;
|
||||
query.userId = condition;
|
||||
|
||||
const videoId = req.query.videoId;
|
||||
condition = videoId ? videoId : undefined;
|
||||
query.videoId = condition;
|
||||
|
||||
const source = req.query.source;
|
||||
condition = source ? source : undefined;
|
||||
query.source = condition;
|
||||
|
||||
const interests = req.query.interests;
|
||||
condition = interests ? {$in: interests} : undefined;
|
||||
query["interests.interest"] = condition
|
||||
|
||||
const isActive = req.query.isActive;
|
||||
condition = isActive ? isActive : undefined;
|
||||
query.isActive = condition;
|
||||
|
||||
const sort = req.query.sort;
|
||||
if(sort !== 'undefined'){
|
||||
switch (sort){
|
||||
case 'asc':
|
||||
condition = {videoId: 1};
|
||||
break;
|
||||
case 'desc':
|
||||
condition = {videoId: -1};
|
||||
break;
|
||||
case 'createdAtAsc':
|
||||
condition = {createdAt: 1};
|
||||
break;
|
||||
case 'createdAtDesc':
|
||||
condition = {createdAt: -1};
|
||||
break;
|
||||
case 'updatedAtAsc':
|
||||
condition = {updatedAt: 1};
|
||||
break;
|
||||
case 'updatedAtDesc':
|
||||
condition = {updatedAt: -1};
|
||||
break;
|
||||
default:
|
||||
condition = {createdAt: -1};
|
||||
}
|
||||
}
|
||||
const query_sort = {sort: condition};
|
||||
|
||||
// Remove undefined key
|
||||
Object.keys(query).forEach(key => query[key] === undefined ? delete query[key] : {});
|
||||
console.log(query);
|
||||
|
||||
Video.find(query, {}, query_sort)
|
||||
.then(data => {
|
||||
return sendMessage(res, 34, data, token);
|
||||
})
|
||||
.catch(err => {
|
||||
return sendError(res,500,100,err.message || "Some error occurred while finding the Videos.", token);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
Reference in a new issue