Skip to content

Commit

Permalink
api script route fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Sep 4, 2018
1 parent 0805136 commit e2a04c9
Showing 1 changed file with 47 additions and 53 deletions.
100 changes: 47 additions & 53 deletions src/web/routes/api/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,17 @@ router.route("/").get(authUser, (req, res) => {
// Parse the current page of scripts that the api will return.
const page = defaultValue(req.query.page, defaultSearchPage);

// Parse the name seperately as it will be a 'contains' filter.
const name = req.query.name;

// Parse other search parameters. Add only the allowed parameters into a new search object.
const search = {};
search.local = req.query.local;
search.verified = req.query.verified;
search.featured = req.query.featured;

// Only return marketplace enabled scripts.
search.marketplace_enabled = req.user.admin === false ? true : req.query.marketplace_enabled;
const search = {
...( req.query.local === undefined ? {} : { local: req.query.local } ),
...( req.query.verified === undefined ? {} : { verified: req.query.verified } ),
...( req.query.featured === undefined ? {} : { featured: req.query.featured } ),
...( req.query.marketplace_enabled === undefined ? { marketplace_enabled: true } : { marketplace_enabled: req.user.admin === false ? true : req.query.marketplace_enabled === "true" ? true : false } ),
...( req.query.name === undefined ? {} : { name: { $regex: `.*${req.query.name}.*` } } )
};

schemas.ScriptSchema
.count({
...search,
...(name === undefined ? {} : {
name: {
$regex: `.*${name}.*`
}
})
})
.count(search)
.skip(page * limit)
.limit(limit)
.then(total => {
Expand All @@ -61,14 +51,7 @@ router.route("/").get(authUser, (req, res) => {
}

schemas.ScriptSchema
.find({
...search,
...(name === undefined ? {} : {
name: {
$regex: `.*${name}.*`
}
})
})
.find(search)
.skip(page * limit)
.limit(limit)
.select({ __v: 0 })
Expand All @@ -91,6 +74,7 @@ router.route("/").get(authUser, (req, res) => {
}).post(authUser, (req, res) => {

const params = {};
params.author_id = req.user._id;
params.name = req.body.name;
params.description = req.body.description;
params.thumbnail = req.body.thumbnail;
Expand Down Expand Up @@ -143,24 +127,19 @@ router.route("/@me").get(authUser, (req, res) => {
// Parse the current page of scripts that the api will return.
const page = defaultValue(req.query.page, defaultSearchPage);

// Parse the name seperately as it will be a 'contains' filter.
const name = req.query.name;

// Parse other search parameters. Add only the allowed parameters into a new search object.
const search = {};
search.local = req.query.local;
search.verified = req.query.verified;
search.featured = req.query.featured;
const search = {
...( req.query.local === undefined ? {} : { local: req.query.local } ),
...( req.query.verified === undefined ? {} : { verified: req.query.verified } ),
...( req.query.featured === undefined ? {} : { featured: req.query.featured } ),
...( req.query.marketplace_enabled === undefined ? {} : { marketplace_enabled: req.query.marketplace_enabled } ),
...( req.query.name === undefined ? {} : { name: { $regex: `.*${req.query.name}.*` } } )
};

schemas.ScriptSchema
.count({
_id: req.user._id,
...search,
...(name === undefined ? {} : {
name: {
$regex: `.*${name}.*`
}
})
author_id: req.user._id,
...search
})
.skip(page * limit)
.limit(limit)
Expand All @@ -172,13 +151,8 @@ router.route("/@me").get(authUser, (req, res) => {

schemas.ScriptSchema
.find({
_id: req.user._id,
...search,
...(name === undefined ? {} : {
name: {
$regex: `.*${name}.*`
}
})
author_id: req.user._id,
...search
})
.skip(page * limit)
.limit(limit)
Expand Down Expand Up @@ -273,7 +247,7 @@ router.route("/:object_id").get(authUser, (req, res) => {
params.updated_at = Date.now();

schemas.ScriptSchema
.findOneById(object_id)
.findById(object_id)
.then(doc => {
if (doc === null) {

Expand Down Expand Up @@ -315,7 +289,7 @@ router.route("/:object_id").get(authUser, (req, res) => {
}

schemas.ScriptSchema
.findOneById(object_id)
.findById(object_id)
.then(doc => {
if (doc === null) {

Expand All @@ -328,7 +302,7 @@ router.route("/:object_id").get(authUser, (req, res) => {
}

doc
.delete()
.remove()
.then(() => {

return res.json({ status: 200 });
Expand Down Expand Up @@ -370,7 +344,17 @@ router.route("/:object_id/likes").post(authUser, (req, res) => {
return res.json({ status: 403 });
}

if (req.user.likes.includes(object_id) === true) {
let found = false;
for (let id of req.user.likes) {

if (id.equals(object_id)) {

found = true;
break;
}
}

if (found === true) {

return res.json({ status: 400 });
}
Expand Down Expand Up @@ -429,7 +413,17 @@ router.route("/:object_id/likes").post(authUser, (req, res) => {
return res.json({ status: 403 });
}

if (req.user.likes.includes(object_id) === false) {
let found = false;
for (let id of req.user.likes) {

if (id.equals(object_id)) {

found = true;
break;
}
}

if (found === false) {

return res.json({ status: 404 });
}
Expand All @@ -443,7 +437,7 @@ router.route("/:object_id/likes").post(authUser, (req, res) => {
if (req.user.likes[i].equals(object_id)) {

req.user.likes.splice(i, 1);
return;
break;
}
}
req.user
Expand Down

0 comments on commit e2a04c9

Please sign in to comment.