Skip to content

Commit

Permalink
api additions
Browse files Browse the repository at this point in the history
  • Loading branch information
Dragon1320 committed Jul 3, 2018
1 parent b709036 commit 040f63c
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/db/schemas/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ const ScriptSchema = new Schema({
permissions: { type: Number, required: true },
match: { type: String, required: true },
match_type: { type: String, default: "command" },
code: { type: String, required: false }
code: { type: String, required: false },
featured: { type: Boolean, default: false },
upvotes: { type: Number, default: 0 },
downvotes: { type: Number, default: 0 }
});

module.exports = mongoose.model("Script", ScriptSchema);
2 changes: 2 additions & 0 deletions src/db/schemas/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ const UserSchema = new Schema({
// _id: ObjectId,
discord_id: { type: String, required: true, unique: true, maxlength: 18, minlength: 18 },
admin: { type: Boolean, default: false },
verified: { type: Boolean, default: false },
developer: { type: Boolean, default: false },
scripts: [ { type: Schema.Types.ObjectId, default: [] } ]
});

Expand Down
46 changes: 42 additions & 4 deletions src/web/routes/api/scripts.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ router.route("/").get(authUser, async (req, res) => {
const permissions = req.query.permissions === undefined ? null : req.query.permissions;
const match = req.query.match === undefined ? null : req.query.match;
const match_type = req.query.match_type === undefined ? null : req.query.match_type;
const featured = req.query.featured === undefined ? null : req.query.featured;

let limit = req.query.limit === undefined ? limitDef : parseInt(req.query.limit);
if (isNaN(limit)) {
Expand All @@ -42,7 +43,8 @@ router.route("/").get(authUser, async (req, res) => {
...(name === null ? {} : { name }),
...(type === null ? {} : { type }),
...(match === null ? {} : { match }),
...(match_type === null ? {} : { match_type })
...(match_type === null ? {} : { match_type }),
...(featured === null ? {} : { featured })
})
.skip(page * limit)
.limit(limit)
Expand Down Expand Up @@ -76,6 +78,9 @@ router.route("/").get(authUser, async (req, res) => {
const match = req.body.match === undefined ? null : req.body.match;
const match_type = req.body.match_type === undefined ? null : req.body.match_type;
const code = req.body.code === undefined ? null : req.body.code;
const featured = req.body.featured === undefined ? false : req.body.featured;
const upvotes = req.body.upvotes === undefined ? 0 : req.body.upvotes;
const downvotes = req.body.downvotes === undefined ? 0 : req.body.downvotes;

if (local === null || description === null || name === null || type === null || permissions === null || match === null) {
return res.json({ status: 400, message: "Bad Request", error: "Not enough data specified" });
Expand Down Expand Up @@ -105,6 +110,18 @@ router.route("/").get(authUser, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Match type needs to be either command, startswith, contains or exactmatch" });
}

if (featured !== null && typeof featured !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Featured needs to be either true or false" });
}

if (upvotes !== null && typeof upvotes !== "number") {
return res.json({ status: 400, message: "Bad Request", error: "Upvotes needs to be a number" });
}

if (downvotes !== null && typeof downvotes !== "number") {
return res.json({ status: 400, message: "Bad Request", error: "Downvotes needs to be a number" });
}

const script = new schemas.ScriptSchema({
local,
name,
Expand All @@ -113,7 +130,10 @@ router.route("/").get(authUser, async (req, res) => {
permissions,
match,
match_type,
code
code,
featured,
upvotes,
downvotes
});

try {
Expand Down Expand Up @@ -334,7 +354,7 @@ router.route("/@me/:object_id").get(authUser, async (req, res) => {
...(permissions === null ? {} : { permissions }),
...(match === null ? {} : { match }),
...(match_type === null ? {} : { match_type }),
...(code === null ? {} : { code })
...(code === null ? {} : { code }),
});
} catch(error) {

Expand Down Expand Up @@ -431,6 +451,9 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
const match = req.body.match === undefined ? null : req.body.match;
const match_type = req.body.match_type === undefined ? null : req.body.match_type;
const code = req.body.code === undefined ? null : req.body.code;
const featured = req.body.featured === undefined ? null : req.body.featured;
const upvotes = req.body.upvotes === undefined ? null : req.body.upvotes;
const downvotes = req.body.downvotes === undefined ? null : req.body.downvotes;

if (local !== null && (local !== true && local !== false)) {
return res.json({ status: 400, message: "Bad Request", error: "Local needs to be a boolean" });
Expand All @@ -456,6 +479,18 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Match type needs to be either command, startswith, contains or exactmatch" });
}

if (featured !== null && typeof featured !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Featured needs to be either true or false" });
}

if (upvotes !== null && typeof upvotes !== "number") {
return res.json({ status: 400, message: "Bad Request", error: "Upvotes needs to be a number" });
}

if (downvotes !== null && typeof downvotes !== "number") {
return res.json({ status: 400, message: "Bad Request", error: "Downvotes needs to be a number" });
}

let upd_script;
try {

Expand All @@ -470,7 +505,10 @@ router.route("/:object_id").get(authAdmin, async (req, res) => {
...(permissions === null ? {} : { permissions }),
...(match === null ? {} : { match }),
...(match_type === null ? {} : { match_type }),
...(code === null ? {} : { code })
...(code === null ? {} : { code }),
...(featured === null ? {} : { featured }),
...(upvotes === null ? {} : { upvotes }),
...(downvotes === null ? {} : { downvotes })
});
} catch(error) {

Expand Down
28 changes: 28 additions & 0 deletions src/web/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ router.post("/", authAdmin, async (req, res) => {
// Get input.
const discord_id = req.body.discord_id === undefined ? null : req.body.discord_id;
const admin = req.body.admin === undefined ? false : req.body.admin;
const verified = req.body.verified === undefined ? false : req.body.verified;
const developer = req.body.developer === undefined ? false : req.body.developer;
const scripts = req.body.scripts === undefined ? [] : req.body.scripts;

// Check discord id.
Expand Down Expand Up @@ -44,6 +46,16 @@ router.post("/", authAdmin, async (req, res) => {
return res.json({ status: 400, message: "Bad Request", error: "Admin needs to be either true or false" });
}

// Check verified.
if (verified !== null && typeof verified !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Verified needs to be either true or false" });
}

// Check developer.
if (developer !== null && typeof developer !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Developer needs to be either true or false" });
}

// Check scripts.
if (scripts instanceof Array === false) {
return res.json({ status: 400, message: "Bad Request", error: "Scripts should be an array" });
Expand Down Expand Up @@ -84,6 +96,8 @@ router.post("/", authAdmin, async (req, res) => {
const user = new schemas.UserSchema({
discord_id,
admin,
verified,
developer,
scripts
});

Expand Down Expand Up @@ -159,13 +173,25 @@ router.route("/:discord_id").get(authAdmin, async (req, res) => {
}).patch(authAdmin, async (req, res) => {

const admin = req.body.admin === undefined ? null : req.body.admin;
const verified = req.body.verified === undefined ? null : req.body.verified;
const developer = req.body.developer === undefined ? null : req.body.developer;
const scripts = req.body.scripts === undefined ? [] : req.body.scripts;

// Check admin.
if (admin !== null && typeof admin !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Admin needs to be either true or false" });
}

// Check verified.
if (verified !== null && typeof verified !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Verified needs to be either true or false" });
}

// Check developer.
if (developer !== null && typeof developer !== "boolean") {
return res.json({ status: 400, message: "Bad Request", error: "Developer needs to be either true or false" });
}

// Check scripts.
if (scripts instanceof Array === false) {
return res.json({ status: 400, message: "Bad Request", error: "Scripts should be an array" });
Expand Down Expand Up @@ -210,6 +236,8 @@ router.route("/:discord_id").get(authAdmin, async (req, res) => {
discord_id: req.params.discord_id
}, {
...(admin === null ? {} : { admin }),
...(verified === null ? {} : { verified }),
...(developer === null ? {} : { developer }),
...(scripts.length === 0 ? {} : { scripts })
});
} catch(error) {
Expand Down

0 comments on commit 040f63c

Please sign in to comment.