diff --git a/api/ani/v1/router.js b/api/ani/v1/router.js index 4b15daf..e371e40 100644 --- a/api/ani/v1/router.js +++ b/api/ani/v1/router.js @@ -1,11 +1,14 @@ const fs = require('fs'); const {Router} = require("express"); +const chalk = require("chalk"); + +const reqTypeColors = require('../../util/misc/rtcolors'); const router = Router(); module.exports = app => { router - .use((req, res, next) => {console.log(`[ANI/v1] ${req.path} | [${req.method}]`); next()}) + .use((req, res, next) => {console.log(`${chalk.gray(`[ANI/v1]`)} ${req.path} ${chalk.gray(`|`)} [${chalk[reqTypeColors[req.method]](req.method)}]`); next()}) .get('/', (req, res) => res.send("This is the Natsuki Anime DB API v1 head.")) fs.readdirSync('./ani/v1/routes').filter(file => file.endsWith('.js')) diff --git a/api/ani/v1/routes/series.js b/api/ani/v1/routes/series.js index dbd85ca..7613f0c 100644 --- a/api/ani/v1/routes/series.js +++ b/api/ani/v1/routes/series.js @@ -6,6 +6,7 @@ module.exports = (app, parentRouter) => { router.use('/', (req, res, next) => { req.listData = Object.keys(app.cache.series).map(series => { return { + id: series, name: app.cache.series[series].name, romaji: app.cache.series[series].romaji, kanji: app.cache.series[series].kanji diff --git a/api/ani/v1/routes/series/add.js b/api/ani/v1/routes/series/add.js index 72727c0..cac8092 100644 --- a/api/ani/v1/routes/series/add.js +++ b/api/ani/v1/routes/series/add.js @@ -105,7 +105,7 @@ module.exports = (app, router) => { //TODO remove console error }) .get(app.auth.tokenPass, app.auth.permsPass('series-approve'), async (req, res, next) => { - const series = await Anime.findOne(/*{$or: [*/{id: req.params.id.toLowerCase()}/*, {numericalId: req.params.id}]}*/); //TODO make sure all ID calls are lowercased + const series = req.params.id.match(/^\d+$/) ? await Anime.findOne({numericalId: req.params.id.toLowerCase()}) : await Anime.findOne({id: req.params.id.toLowerCase()}); //TODO make sure all ID calls are lowercased if (!series || (series && !series.meta.completed && req.unauthorized)) { if (req.params.id.toLowerCase() === 'queue') {return next();} return res.status(400).send("A series with that ID doesn't exist!"); diff --git a/api/ani/v1/routes/series/edits.js b/api/ani/v1/routes/series/edits.js index 3cffde4..c57959a 100644 --- a/api/ani/v1/routes/series/edits.js +++ b/api/ani/v1/routes/series/edits.js @@ -53,53 +53,6 @@ module.exports = (app, router) => { ].forEach(field => edits.stringWrap(field[0], field[1])); - /*router.patch('/:id/name', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { - if (!req.params.id) {return;} - const series = await Anime.findOne({id: req.params.id.toLowerCase()}); - if (!editCheck(series, req, res)) {return;} - if (!req.body.name) {return res.status(400).send("You did not provide a new name in your body.");} - if (!req.body.name.match(/^[\w_\-!?.:; ]+$/gm) || req.body.name.length > 150) {return res.status(400).send("Your new name is too long or contains illegal characters.");} - try { - series.name = req.body.name.trim(); - pushEdit("Updated name", req, series); - return series.save() - .then(() => res.send("Name updated.")) - .catch(() => res.status(500).send("There was an error trying to update your name. Please try again.")); - } catch {return res.status(500).send("There was an error trying to update your name. Please try again.");} - }); - - router.patch('/:id/romaji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { - if (!req.params.id) {return;} - const series = await Anime.findOne({id: req.params.id.toLowerCase()}); - if (!editCheck(series, req, res)) {return;} - if (!req.body.romaji) {return res.status(400).send("You did not provide a new romanized name (romaji) in your body.");} - if (req.body.romaji.length > 150) {return res.status(400).send("Your new romanized name is too long or contains illegal characters.");} - try { - series.romaji = req.body.romaji.trim(); - pushEdit("Updated romaji", req, series); - return series.save() - .then(() => res.send("Romanized name updated.")) - .catch(() => res.status(500).send("There was an error trying to update your romaji. Please try again.")); - } catch {return res.status(500).send("There was an error trying to update your romaji. Please try again.");} - }); - - router.patch('/:id/kanji', app.auth.token, app.auth.perms('series-submit'), async (req, res) => { - if (!req.params.id) {return;} - const series = await Anime.findOne({id: req.params.id.toLowerCase()}); - if (!editCheck(series, req, res)) {return;} - if (!req.body.kanji) {return res.status(400).send("You did not provide a new kanji in your body.");} - if (req.body.kanji.length > 150) {return res.status(400).send("Your new kanji is too long or contains illegal characters.");} - try { - series.kanji = req.body.kanji.trim(); - pushEdit("Updated kanji", req, series); - return series.save() - .then(() => res.send("Kanji updated.")) - .catch(() => res.status(500).send("There was an error trying to update your kanji. Please try again.")); - } catch {return res.status(500).send("There was an error trying to update your kanji. Please try again.");} - });*/ - - - //router.use('/:id/altnames', app.auth.tokenPass, app.auth.permsPass('series-approve')) }; \ No newline at end of file diff --git a/api/index.js b/api/index.js index 690c014..3656e05 100644 --- a/api/index.js +++ b/api/index.js @@ -5,6 +5,8 @@ const helmet = require('helmet'); const {set, createConnection} = require('mongoose'); const chalk = require('chalk'); +const reqTypeColors = require('./util/misc/rtcolors'); + const app = express(); set('strictQuery', false); @@ -49,14 +51,6 @@ server = app.listen(4062, async () => { require('./v1/index')(app); //initialize bot API branch require('./ani/index')(app); //initialize ani API branch - const reqTypeColors = { - GET: 'greenBright', - POST: 'blueBright', - PATCH: 'yellowBright', - PUT: 'yellow', - DELETE: 'redBright' - }; - const checkMiddleware = (middleware, string) => { if (middleware.route) {Object.keys(middleware.route.methods).forEach(method => { if (middleware.route.methods[method]) {console.log(`${chalk.gray(`[`)}${chalk[reqTypeColors[method.toUpperCase()]](method.toUpperCase())}${chalk.gray(`]`)} ${string}${middleware.route.path}`);} diff --git a/api/util/misc/rtcolors.js b/api/util/misc/rtcolors.js new file mode 100644 index 0000000..7929b72 --- /dev/null +++ b/api/util/misc/rtcolors.js @@ -0,0 +1,7 @@ +module.exports = { + GET: 'greenBright', + POST: 'blueBright', + PATCH: 'yellowBright', + PUT: 'yellow', + DELETE: 'redBright' +}; \ No newline at end of file