forked from DebaucheryLibrarian/traxxx
Added tags and entities to REST API..
This commit is contained in:
29
src/web/entities.js
Normal file
29
src/web/entities.js
Normal file
@@ -0,0 +1,29 @@
|
||||
'use strict';
|
||||
|
||||
const { fetchEntity, fetchEntities, searchEntities } = require('../entities');
|
||||
|
||||
async function fetchEntityApi(req, res, type) {
|
||||
const entity = await fetchEntity(req.params.entityId, type || req.query.type);
|
||||
|
||||
if (entity) {
|
||||
res.send({ entity });
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(404).send({ entity: null });
|
||||
}
|
||||
|
||||
async function fetchEntitiesApi(req, res, type) {
|
||||
const query = req.query.query || req.query.q;
|
||||
|
||||
const entities = query
|
||||
? await searchEntities(query, type || req.query.type, req.query.limit)
|
||||
: await fetchEntities(type || req.query.type, req.query.limit);
|
||||
|
||||
res.send({ entities });
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchEntity: fetchEntityApi,
|
||||
fetchEntities: fetchEntitiesApi,
|
||||
};
|
||||
@@ -1,26 +0,0 @@
|
||||
'use strict';
|
||||
|
||||
const { fetchNetworks, fetchNetworksFromReleases } = require('../networks');
|
||||
|
||||
async function fetchNetworksApi(req, res) {
|
||||
const networkId = typeof req.params.networkId === 'number' ? req.params.networkId : undefined; // null will literally include NULL results
|
||||
const networkSlug = typeof req.params.networkId === 'string' ? req.params.networkId : undefined;
|
||||
|
||||
const networks = await fetchNetworks({
|
||||
id: networkId,
|
||||
slug: networkSlug,
|
||||
});
|
||||
|
||||
res.send(networks);
|
||||
}
|
||||
|
||||
async function fetchNetworksFromReleasesApi(req, res) {
|
||||
const networks = await fetchNetworksFromReleases();
|
||||
|
||||
res.send(networks);
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
fetchNetworks: fetchNetworksApi,
|
||||
fetchNetworksFromReleases: fetchNetworksFromReleasesApi,
|
||||
};
|
||||
@@ -26,6 +26,16 @@ const {
|
||||
fetchActors,
|
||||
} = require('./actors');
|
||||
|
||||
const {
|
||||
fetchEntity,
|
||||
fetchEntities,
|
||||
} = require('./entities');
|
||||
|
||||
const {
|
||||
fetchTag,
|
||||
fetchTags,
|
||||
} = require('./tags');
|
||||
|
||||
async function initServer() {
|
||||
const app = express();
|
||||
const router = Router();
|
||||
@@ -78,6 +88,21 @@ async function initServer() {
|
||||
router.get('/api/actors', fetchActors);
|
||||
router.get('/api/actors/:actorId', fetchActor);
|
||||
|
||||
router.get('/api/entities', async (req, res) => fetchEntities(req, res, null));
|
||||
router.get('/api/entities/:entityId', async (req, res) => fetchEntity(req, res, null));
|
||||
|
||||
router.get('/api/channels', async (req, res) => fetchEntities(req, res, 'channel'));
|
||||
router.get('/api/channels/:entityId', async (req, res) => fetchEntity(req, res, 'channel'));
|
||||
|
||||
router.get('/api/networks', async (req, res) => fetchEntities(req, res, 'network'));
|
||||
router.get('/api/networks/:entityId', async (req, res) => fetchEntity(req, res, 'network'));
|
||||
|
||||
router.get('/api/studios', async (req, res) => fetchEntities(req, res, 'studio'));
|
||||
router.get('/api/studios/:entityId', async (req, res) => fetchEntity(req, res, 'studio'));
|
||||
|
||||
router.get('/api/tags', fetchTags);
|
||||
router.get('/api/tags/:tagId', fetchTag);
|
||||
|
||||
router.get('*', (req, res) => {
|
||||
res.render(path.join(__dirname, '../../assets/index.ejs'), {
|
||||
env: JSON.stringify({
|
||||
|
||||
@@ -1,40 +1,26 @@
|
||||
'use strict';
|
||||
|
||||
const { fetchTags } = require('../tags');
|
||||
const { fetchTag, fetchTags } = require('../tags');
|
||||
|
||||
async function fetchTagsApi(req, res) {
|
||||
const tagId = typeof req.params.tagId === 'number' ? req.params.tagId : undefined; // null will literally include NULL results
|
||||
const tagSlug = typeof req.params.tagId === 'string' ? req.params.tagId : undefined;
|
||||
async function fetchTagApi(req, res) {
|
||||
const tag = await fetchTag(req.params.tagId);
|
||||
|
||||
if (tagId || tagSlug) {
|
||||
const tags = await fetchTags({
|
||||
id: tagId,
|
||||
slug: tagSlug,
|
||||
}, null, req.query.limit);
|
||||
|
||||
if (tags.length > 0) {
|
||||
res.send(tags[0]);
|
||||
return;
|
||||
}
|
||||
|
||||
res.status(404).send();
|
||||
if (tag) {
|
||||
res.send({ tag });
|
||||
return;
|
||||
}
|
||||
|
||||
const query = {};
|
||||
const groupsQuery = {};
|
||||
|
||||
if (req.query.priority) query.priority = req.query.priority.split(',');
|
||||
if (req.query.slug) query.slug = req.query.slug.split(',');
|
||||
if (req.query.group) {
|
||||
groupsQuery.slug = req.query.group.split(',');
|
||||
}
|
||||
|
||||
const tags = await fetchTags(query, groupsQuery, req.query.limit);
|
||||
|
||||
res.send(tags);
|
||||
res.status(404).send({ tag: null });
|
||||
}
|
||||
|
||||
async function fetchTagsApi(req, res) {
|
||||
const tags = await fetchTags(req.query.limit);
|
||||
|
||||
res.send({ tags });
|
||||
}
|
||||
|
||||
|
||||
module.exports = {
|
||||
fetchTag: fetchTagApi,
|
||||
fetchTags: fetchTagsApi,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user