parent
b044595633
commit
c64137e136
@ -0,0 +1,15 @@ |
|||||||
|
const {Router} = require("express"); |
||||||
|
const fs = require('fs'); |
||||||
|
|
||||||
|
module.exports = (app, parentRouter) => { |
||||||
|
const router = Router() |
||||||
|
.get('/', (req, res) => res.send('/series/:id')); |
||||||
|
|
||||||
|
router.location = '/series'; |
||||||
|
fs.readdirSync('./ani/v1/routes/series').filter(file => file.endsWith('.js')) |
||||||
|
.forEach(route => require(`./series/${route}`)(app, router)); //execute all router functions
|
||||||
|
|
||||||
|
parentRouter.use('/series', router); |
||||||
|
|
||||||
|
//TODO get all series
|
||||||
|
}; |
@ -1,11 +1,11 @@ |
|||||||
const {verify} = require("jsonwebtoken"); |
const {verify} = require("jsonwebtoken"); |
||||||
|
|
||||||
module.exports = app => { |
module.exports = (app, passAuth) => { |
||||||
const Users = app.db.models.ani.users |
const Users = app.db.models.ani.users |
||||||
return (req, res, next) => { |
return (req, res, next) => { |
||||||
if (req.headers && req.headers.authorization && req.headers.authorization.split(" ")[0] === "JWT") { |
if (req.headers && req.headers.authorization && req.headers.authorization.split(" ")[0] === "JWT") { |
||||||
verify(req.headers.authorization.split(' ')[1], app.auth.jwt_secret, (e, d) => { |
verify(req.headers.authorization.split(' ')[1], app.auth.jwt_secret, (e, d) => { |
||||||
if (e) {req.user = undefined; return next();} |
if (e) {req.user = undefined; return passAuth ? next() : res.status(401).send("You are not authenticated!");} |
||||||
Users.findOne({id: d.id}) |
Users.findOne({id: d.id}) |
||||||
.exec((err, user) => { |
.exec((err, user) => { |
||||||
if (err) {res.status(500).send("Something went trying to authorize you!");} |
if (err) {res.status(500).send("Something went trying to authorize you!");} |
@ -0,0 +1,36 @@ |
|||||||
|
const {Router} = require("express"); |
||||||
|
|
||||||
|
module.exports = (parentRouter, path, modifiable, bodyName, save) => { |
||||||
|
const router = Router(); |
||||||
|
|
||||||
|
router.get("/", (req, res) => res.json(req.listData)); |
||||||
|
router.get("/list", (req, res) => res.json(!Array.isArray(req.listData) ? Object.keys(req.listData) : req.listData)); |
||||||
|
router.get('/random', (req, res) => res.json(!Array.isArray(req.listData) ? Object.keys(req.listData)[Math.floor(Math.random() * Object.keys(req.listData).length)] : req.listData[Math.floor(Math.random() * req.listData.length)])); |
||||||
|
//TODO search
|
||||||
|
|
||||||
|
if (modifiable) { //TODO auth
|
||||||
|
const add = async (req, res) => { |
||||||
|
if (!req.body[bodyName]) {return res.status(400).send(`Missing body param "${bodyName}".`);} |
||||||
|
req.listData.push(req.body[bodyName]); |
||||||
|
return await save(req, res, req.listData, req.body[bodyName]) || res.json(req.listData); |
||||||
|
} |
||||||
|
router.post('/add', add); |
||||||
|
router.post('/', add); |
||||||
|
router.post('/clear', async (req, res) => { |
||||||
|
req.listData = []; |
||||||
|
return await save(req, res, req.listData) || res.json(req.listData); |
||||||
|
}); |
||||||
|
router.delete('/remove', async (req, res) => { |
||||||
|
if (!req.body[bodyName]) {return res.status(400).send(`Missing body param "${bodyName}".`);} |
||||||
|
let index = req.listData.indexOf(req.body[bodyName]); |
||||||
|
if (index === -1) {return res.status(400).send("The server was unable to find an item matching the specified criteria.");} |
||||||
|
let length = req.listData.length; |
||||||
|
req.listData = req.listData.length === 1 && req.listData[0] === req.body[bodyName] ? [] : req.listData.splice(req.listData.indexOf(req.body[bodyName]), 1); |
||||||
|
return req.listData.length === length ? res.status(500).send("For some reason, that couldn't be removed.") : await save(req, res, req.listData) || res.json(req.listData); |
||||||
|
}); |
||||||
|
} |
||||||
|
|
||||||
|
router.location = path; |
||||||
|
|
||||||
|
return router; |
||||||
|
}; |
@ -0,0 +1,3 @@ |
|||||||
|
module.exports = (model) => { |
||||||
|
|
||||||
|
}; |
@ -0,0 +1,33 @@ |
|||||||
|
const {Schema} = require("mongoose"); |
||||||
|
|
||||||
|
module.exports = (connection) => connection.model('series', new Schema({ |
||||||
|
id: {type: String, unique: true}, |
||||||
|
meta: { |
||||||
|
locked: {type: Boolean, default: false}, |
||||||
|
creator: String, //uid
|
||||||
|
edits: {type: [{ |
||||||
|
user: String, //uid
|
||||||
|
timestamp: String, //Date.getTime(),
|
||||||
|
action: String |
||||||
|
}], default: []}, |
||||||
|
completed: {type: Boolean, default: false}, |
||||||
|
approved: {type: { |
||||||
|
approved: Boolean, |
||||||
|
by: String |
||||||
|
}, default: false} |
||||||
|
}, |
||||||
|
|
||||||
|
name: String, |
||||||
|
romaji: String, |
||||||
|
kanji: String, |
||||||
|
altNames: [String], |
||||||
|
id: Number, |
||||||
|
|
||||||
|
synopsis: { |
||||||
|
synopsis: String, |
||||||
|
by: String |
||||||
|
}, |
||||||
|
genres: [String], |
||||||
|
tags: [String], |
||||||
|
|
||||||
|
})); |
Loading…
Reference in new issue