Skip to content

Commit

Permalink
Refactor: Update API code to ES6 from CommonJS
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrea committed Dec 9, 2023
1 parent edee687 commit d203b60
Show file tree
Hide file tree
Showing 156 changed files with 1,780 additions and 1,539 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"engines": {
"node": ">=18.x"
},
"type": "module",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js"
Expand Down
16 changes: 7 additions & 9 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
const express = require('express');
const cors = require('cors');
import express from 'express';
import cors from 'cors';

// Import custom error handlers and logger
const {
import {
handler404,
errorsLogger,
errorsHandler,
} = require('./handlers/errors/index');
const { ipLogger } = require('./handlers/logger/ip');
const routes = require('./routes');

} from './handlers/errors/index.js';
import { ipLogger } from './handlers/logger/ip.js';
import routes from './routes.js';

// Express APP
const app = express();
Expand All @@ -26,7 +25,6 @@ app.use(express.json());
// Middleware to parse URL-encoded bodies
app.use(express.urlencoded({ extended: true }));


// Logger middleware
// Uncomment the following block if you want to enable IP logging
if (process.env.LOGGER === 'true') {
Expand All @@ -39,4 +37,4 @@ app.use(routes);
// Error handling middleware
app.use(handler404, errorsLogger, errorsHandler);

module.exports = app;
export default app;
60 changes: 37 additions & 23 deletions src/controllers/facts/randomFacts.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,58 @@
const createError = require('http-errors')
const Facts = require('../../models/schemas/Facts')
const tagsFilter = require('../../utils/tagsFilter')
const lengthFilter = require('../../utils/lengthFilter')
const Stats = require('../../models/schemas/Stat')

// Get random Anime Fact
module.exports = async function getRandomFact(req, res, next) {
import createError from 'http-errors';
import Facts from '../../models/schemas/Facts.js';
import tagsFilter from '../../utils/tagsFilter.js';
import lengthFilter from '../../utils/lengthFilter.js';
import Stats from '../../models/schemas/Stat.js';

/**
* Gets a random anime fact with optional length and tags filters and updates system statistics.
*
* @param {Object} req - Express request object.
* @param {Object} res - Express response object.
* @param {Function} next - Express next middleware function.
*/
const getRandomFact = async (req, res, next) => {
try {
const { minLength, maxLength, tags } = req.query
const { minLength, maxLength, tags } = req.query;

const filter = {}
// Create a filter object based on the optional length and tags parameters
const filter = {};

// Apply length filter (if minLength or maxLength is provided)
if (minLength || maxLength) {
filter.length = lengthFilter(minLength, maxLength)
filter.length = lengthFilter(minLength, maxLength);
}

// Apply tags filter (if tags are provided)
if (tags) {
filter.tags = tagsFilter(tags)
filter.tags = tagsFilter(tags);
}

// Aggregate to match the filter, select a random fact, and project excluding version field
const [result] = await Facts.aggregate([
// Apply filters (if any)
{ $match: filter },
// Select a random document from the results
{ $sample: { size: 1 } },
{ $match: filter }, // Apply filters (if any)
{ $sample: { size: 1 } }, // Select a random document from the results
{ $project: { __v: 0 } },
])
]);

// If no fact is found, return a 404 error
if (!result) {
return next(createError(404, 'Could not find any matching fact'))
return next(createError(404, 'Could not find any matching fact'));
}

res.status(200).json(result)
// Respond with the random fact
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } })
// Update system statistics for facts
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { facts: 1 } });
} catch (error) {
// Update system statistics for failed requests
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomFact;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomAngry.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Angry = require('../../models/schemas/Angry')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Angry from '../../models/schemas/Angry.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Angry
module.exports = async function getRandomAngry(req, res, next) {
const getRandomAngry = async (req, res, next) => {
try {
const [result] = await Angry.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Angry Gif'))
return next(createError(404, 'Could not find any Angry Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { angry: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomAngry;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomBaka.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Baka = require('../../models/schemas/Baka')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Baka from '../../models/schemas/Baka.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Baka
module.exports = async function getRandomBaka(req, res, next) {
const getRandomBaka = async (req, res, next) => {
try {
const [result] = await Baka.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Baka Gif'))
return next(createError(404, 'Could not find any Baka Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { baka: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomBaka;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomBite.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Bite = require('../../models/schemas/Bite')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Bite from '../../models/schemas/Bite.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Bite
module.exports = async function getRandomBite(req, res, next) {
const getRandomBite = async (req, res, next) => {
try {
const [result] = await Bite.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Bite Gif'))
return next(createError(404, 'Could not find any Bite Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bite: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomBite;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomBlush.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Blush = require('../../models/schemas/Blush')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Blush from '../../models/schemas/Blush.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Blush
module.exports = async function getRandomBlush(req, res, next) {
const getRandomBlush = async (req, res, next) => {
try {
const [result] = await Blush.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Blush Gif'))
return next(createError(404, 'Could not find any Blush Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { blush: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomBlush;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomBonk.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Bonk = require('../../models/schemas/Bonk')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Bonk from '../../models/schemas/Bonk.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Bonk
module.exports = async function getRandomBonk(req, res, next) {
const getRandomBonk = async (req, res, next) => {
try {
const [result] = await Bonk.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Bonk Gif'))
return next(createError(404, 'Could not find any Bonk Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bonk: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomBonk;
25 changes: 14 additions & 11 deletions src/controllers/gifs/randomBored.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,31 @@
const createError = require('http-errors')
const Bored = require('../../models/schemas/Bored')
const Stats = require('../../models/schemas/Stat')

import createError from 'http-errors';
import Bored from '../../models/schemas/Bored.js';
import Stats from '../../models/schemas/Stat.js';

// Get random Anime Bored
module.exports = async function getRandomBored(req, res, next) {
const getRandomBored = async (req, res, next) => {
try {
const [result] = await Bored.aggregate([
// Select a random document from the results
{ $sample: { size: 1 } },
{ $project: { __v: 0, _id: 0 } },
])
]);

if (!result) {
return next(createError(404, 'Could not find any Bored Gif'))
return next(createError(404, 'Could not find any Bored Gif'));
}

res.status(200).json(result)
res.status(200).json(result);

await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } })
await Stats.findOneAndUpdate({ _id: 'systemstats' }, { $inc: { bored: 1 } });
} catch (error) {
await Stats.findOneAndUpdate(
{ _id: 'systemstats' },
{ $inc: { failed_requests: 1 } }
)
return next(error)
);
return next(error);
}
}
};

export default getRandomBored;
Loading

0 comments on commit d203b60

Please sign in to comment.