Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ MONGO_DOMAIN=
# MONGO DATABASES (names do not matter)
JSR_DB=
JSRF_DB=
BRC_DB=
CORE_DB=
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "jetsetradio-api",
"version": "1.1.1",
"version": "1.1.2",
"description": "A Data Provider relating to the JSR/JSRF universe",
"type": "module",
"main": "src/app.js",
"scripts": {
"test": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
"build": "npm install",
"prod": "export $(cat ./prod.env | egrep -v '#|^$' | xargs) && node src/utils/swagger.js",
"qa": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && nodemon --inspect --ignore src/utils/ src/utils/swagger.js"
"qa": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && nodemon --inspect --ignore src/utils/ src/utils/swagger.js",
"test": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
"test:file": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js"
},
"keywords": [],
"author": "RazzNBlue",
Expand Down
15 changes: 7 additions & 8 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import express from 'express';
import axios from 'axios';
import dotenv from 'dotenv';
import express from "express";
import axios from "axios";
import dotenv from "dotenv";
dotenv.config();

import LOGGER from './utils/logger.js';
import MiddlewareManager from './managers/MiddlewareManager.js';

import LOGGER from "./utils/logger.js";
import MiddlewareManager from "./managers/MiddlewareManager.js";

const middlewareManager = new MiddlewareManager();

Expand All @@ -18,9 +17,9 @@ middlewareManager.setMiddleware(app);
app.listen(PORT || 8080, () => {
LOGGER.info(`JSR-API Listening on port ${PORT}`);

// Ping App every 10 minutes
// Ping App every 10 minutes
setInterval(async () => {
const res = await axios.get(`${baseUrl}/health`);
console.log(`App Ping - ${baseUrl}. Status: ${res.data.message}`);
}, 600000);
})
});
68 changes: 4 additions & 64 deletions src/config/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ dotenv.config();
import LOGGER from "../utils/logger.js";
import Constants from "../constants/dbConstants.js";

const {CORE_DB, JSR_DB, JSRF_DB, BRC_DB} = Constants;
const {CORE_DB} = Constants;

const buildMongoUri = () => {
const user = process.env.MONGO_USER;
Expand All @@ -33,7 +33,7 @@ const buildMongoUri = () => {
const client = new MongoClient(buildMongoUri());

/* Database Connections */
export const performCoreAdminAction = async (action, username) => {
export const performAdminAction = async (action, username) => {
try {
await client.connect();
return await action(client, CORE_DB, "Admin", username);
Expand All @@ -45,73 +45,13 @@ export const performCoreAdminAction = async (action, username) => {
}
};

export const performCoreAction = async (action, collection, id, qps) => {
export const performDBAction = async (action, dbName, collection, id, qps) => {
try {
await client.connect();
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
return await action(
client,
CORE_DB,
collection,
id,
getQueryObject(qps),
queryActions
);
} catch (err) {
console.error(err);
return err;
} finally {
await client.close();
}
};

export const performJSRAction = async (action, collection, id, qps) => {
try {
await client.connect();
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
return await action(
client,
JSR_DB,
collection,
id,
getQueryObject(qps),
queryActions
);
} catch (err) {
console.error(err);
return err;
} finally {
await client.close();
}
};

export const performJSRFAction = async (action, collection, id, qps) => {
try {
await client.connect();
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
return await action(
client,
JSRF_DB,
collection,
id,
getQueryObject(qps),
queryActions
);
} catch (err) {
console.error(err);
return err;
} finally {
await client.close();
}
};

export const performBRCAction = async (action, collection, id, qps) => {
try {
await client.connect();
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
return await action(
client,
BRC_DB,
dbName,
collection,
id,
getQueryObject(qps),
Expand Down
3 changes: 2 additions & 1 deletion src/config/dbActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ export const Actions = {
fetchAll: async (client, dbName, collectionName, id, qps, sortValue) => { return await client.db(dbName).collection(collectionName).find({}).sort(sortValue).toArray() },
fetchWithQuery: async (client, dbName, collectionName, id, qps, queryActions) => { return await client.db(dbName).collection(collectionName).find(qps).sort(queryActions[0]).limit(queryActions[1]).toArray() },
fetchById: async (client, dbName, collectionName, id) => { return await client.db(dbName).collection(collectionName).findOne({ _id: new ObjectId(id) }) },
fetchAdmin: async (client, dbName, collectionName, username) => { return await client.db(dbName).collection(collectionName).findOne({ username: username }) }
fetchAdmin: async (client, dbName, collectionName, username) => { return await client.db(dbName).collection(collectionName).findOne({ username: username }) },
fetchRandom: async (client, dbName, collectionName) => { return await client.db(dbName).collection(collectionName).aggregate([{ $sample: { size: 1 } }]).toArray(); }
}
5 changes: 5 additions & 0 deletions src/constants/dbConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ const Constants = {
JSR_DB: process.env.JSR_DB,
JSRF_DB: process.env.JSRF_DB,
BRC_DB: process.env.BRC_DB,
gameMap: {
jsr: process.env.JSR_DB,
jsrf: process.env.JSRF_DB,
brc: process.env.BRC_DB,
},
};

export default Constants;
97 changes: 70 additions & 27 deletions src/controllers/artistController.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,103 @@
import { performBRCAction, performCoreAction, performJSRAction, performJSRFAction } from "../config/db.js";
import { Actions } from "../config/dbActions.js";
import { ObjectId } from "mongodb";
import {ObjectId} from "mongodb";
import Constants from "../constants/dbConstants.js";
import {Actions} from "../config/dbActions.js";
import {performDBAction} from "../config/db.js";
import LOGGER from "../utils/logger.js";


const Artist = 'Artist';
const Song = 'Song';
const Artist = "Artist";
const Song = "Song";
const {CORE_DB, JSR_DB, JSRF_DB, BRC_DB} = Constants;

export const getArtists = async (req, res) => {
try {
const artists = await fetchArtists(req);
if (artists) {
return res.send(artists.length === 1 ? artists[0] : artists);
}
}
res.status(404).send();
} catch(err) {
res.status(500).send(`Could not fetch ALL Artists due to error: \n${err}`);
} catch (err) {
LOGGER.error(`Could not fetch ALL Artists`, err);
res.status(500).send(`Could not fetch ALL Artists due to error`, err);
}
}
};

export const getArtistById = async (req, res) => {
try {
const artist = await performCoreAction(Actions.fetchById, Artist, req?.params?.id);
const artist = await performDBAction(
Actions.fetchById,
CORE_DB,
Artist,
req?.params?.id
);
if (artist) {
return res.send(artist);
}
res.status(404).send(`Artist Resource could not be found at requested location`);
} catch(err) {
res.status(500).send(`Could not fetch Artist with ID: ${req.params.id} \n${err}`);
res
.status(404)
.send(`Artist Resource could not be found at requested location`);
} catch (err) {
LOGGER.error(`Could not fetch Artist by Id ${req?.params?.id}`, err);
res
.status(500)
.send(`Could not fetch Artist with ID: ${req.params.id}`, err);
}
}
};

export const getSongsByArtist = async (req, res) => {
try {
const artistId = req?.params?.id;
if (!artistId) {
return res.status(400).send('Invalid ArtistId');
return res.status(400).send("Invalid ArtistId");
}
res.send(await fetchSongsByArtistId(artistId));
} catch(err) {
res.status(500).send(`Could not fetch Songs by Artist with ID: ${req.params.id} \n${err}`);
} catch (err) {
LOGGER.error(`Could not fetch Songs By Artist ${req?.params?.id}`, err);
res
.status(500)
.send(`Could not fetch Songs by Artist with ID: ${req.params.id}`, err);
}
}

};

export const fetchArtists = async (req) => {
if (req?.query) {
return await performCoreAction(Actions.fetchWithQuery, Artist, null, req?.query);
return await performDBAction(
Actions.fetchWithQuery,
CORE_DB,
Artist,
null,
req?.query
);
}
return await performCoreAction(Actions.fetchAll, Artist, null);
}
return await performDBAction(Actions.fetchAll, CORE_DB, Artist, null);
};

export const fetchSongsByArtistId = async (artistId) => {
const songs = [];
const jsrSongs = await performJSRAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
const jsrfSongs = await performJSRFAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
const brcSongs = await performBRCAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
const jsrSongs = await performDBAction(
Actions.fetchWithQuery,
JSR_DB,
Song,
null,
{
artistId: new ObjectId(artistId),
}
);
const jsrfSongs = await performDBAction(
Actions.fetchWithQuery,
JSRF_DB,
Song,
null,
{artistId: new ObjectId(artistId)}
);
const brcSongs = await performDBAction(
Actions.fetchWithQuery,
BRC_DB,
Song,
null,
{
artistId: new ObjectId(artistId),
}
);
if (jsrSongs && jsrSongs.length > 0) {
songs.push(jsrSongs);
}
Expand All @@ -65,4 +108,4 @@ export const fetchSongsByArtistId = async (artistId) => {
songs.push(brcSongs);
}
return songs.flat(1);
}
};
Loading