Skip to content

Commit 7fe7ec0

Browse files
authored
v1.1.2 - Refactoring, Tests, & Add /random routes (#37)
* levels * fix tests * cleanup * version
1 parent 1cc5cd6 commit 7fe7ec0

42 files changed

Lines changed: 1785 additions & 1031 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,5 @@ MONGO_DOMAIN=
1313
# MONGO DATABASES (names do not matter)
1414
JSR_DB=
1515
JSRF_DB=
16+
BRC_DB=
1617
CORE_DB=

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
{
22
"name": "jetsetradio-api",
3-
"version": "1.1.1",
3+
"version": "1.1.2",
44
"description": "A Data Provider relating to the JSR/JSRF universe",
55
"type": "module",
66
"main": "src/app.js",
77
"scripts": {
8-
"test": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
98
"build": "npm install",
109
"prod": "export $(cat ./prod.env | egrep -v '#|^$' | xargs) && node src/utils/swagger.js",
11-
"qa": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && nodemon --inspect --ignore src/utils/ src/utils/swagger.js"
10+
"qa": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && nodemon --inspect --ignore src/utils/ src/utils/swagger.js",
11+
"test": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js",
12+
"test:file": "export $(cat ./qa.env | egrep -v '#|^$' | xargs) && node --no-warnings --experimental-vm-modules ./node_modules/jest/bin/jest.js"
1213
},
1314
"keywords": [],
1415
"author": "RazzNBlue",

src/app.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
import express from 'express';
2-
import axios from 'axios';
3-
import dotenv from 'dotenv';
1+
import express from "express";
2+
import axios from "axios";
3+
import dotenv from "dotenv";
44
dotenv.config();
55

6-
import LOGGER from './utils/logger.js';
7-
import MiddlewareManager from './managers/MiddlewareManager.js';
8-
6+
import LOGGER from "./utils/logger.js";
7+
import MiddlewareManager from "./managers/MiddlewareManager.js";
98

109
const middlewareManager = new MiddlewareManager();
1110

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

21-
// Ping App every 10 minutes
20+
// Ping App every 10 minutes
2221
setInterval(async () => {
2322
const res = await axios.get(`${baseUrl}/health`);
2423
console.log(`App Ping - ${baseUrl}. Status: ${res.data.message}`);
2524
}, 600000);
26-
})
25+
});

src/config/db.js

Lines changed: 4 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ dotenv.config();
66
import LOGGER from "../utils/logger.js";
77
import Constants from "../constants/dbConstants.js";
88

9-
const {CORE_DB, JSR_DB, JSRF_DB, BRC_DB} = Constants;
9+
const {CORE_DB} = Constants;
1010

1111
const buildMongoUri = () => {
1212
const user = process.env.MONGO_USER;
@@ -33,7 +33,7 @@ const buildMongoUri = () => {
3333
const client = new MongoClient(buildMongoUri());
3434

3535
/* Database Connections */
36-
export const performCoreAdminAction = async (action, username) => {
36+
export const performAdminAction = async (action, username) => {
3737
try {
3838
await client.connect();
3939
return await action(client, CORE_DB, "Admin", username);
@@ -45,73 +45,13 @@ export const performCoreAdminAction = async (action, username) => {
4545
}
4646
};
4747

48-
export const performCoreAction = async (action, collection, id, qps) => {
48+
export const performDBAction = async (action, dbName, collection, id, qps) => {
4949
try {
5050
await client.connect();
5151
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
5252
return await action(
5353
client,
54-
CORE_DB,
55-
collection,
56-
id,
57-
getQueryObject(qps),
58-
queryActions
59-
);
60-
} catch (err) {
61-
console.error(err);
62-
return err;
63-
} finally {
64-
await client.close();
65-
}
66-
};
67-
68-
export const performJSRAction = async (action, collection, id, qps) => {
69-
try {
70-
await client.connect();
71-
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
72-
return await action(
73-
client,
74-
JSR_DB,
75-
collection,
76-
id,
77-
getQueryObject(qps),
78-
queryActions
79-
);
80-
} catch (err) {
81-
console.error(err);
82-
return err;
83-
} finally {
84-
await client.close();
85-
}
86-
};
87-
88-
export const performJSRFAction = async (action, collection, id, qps) => {
89-
try {
90-
await client.connect();
91-
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
92-
return await action(
93-
client,
94-
JSRF_DB,
95-
collection,
96-
id,
97-
getQueryObject(qps),
98-
queryActions
99-
);
100-
} catch (err) {
101-
console.error(err);
102-
return err;
103-
} finally {
104-
await client.close();
105-
}
106-
};
107-
108-
export const performBRCAction = async (action, collection, id, qps) => {
109-
try {
110-
await client.connect();
111-
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
112-
return await action(
113-
client,
114-
BRC_DB,
54+
dbName,
11555
collection,
11656
id,
11757
getQueryObject(qps),

src/config/dbActions.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ export const Actions = {
55
fetchAll: async (client, dbName, collectionName, id, qps, sortValue) => { return await client.db(dbName).collection(collectionName).find({}).sort(sortValue).toArray() },
66
fetchWithQuery: async (client, dbName, collectionName, id, qps, queryActions) => { return await client.db(dbName).collection(collectionName).find(qps).sort(queryActions[0]).limit(queryActions[1]).toArray() },
77
fetchById: async (client, dbName, collectionName, id) => { return await client.db(dbName).collection(collectionName).findOne({ _id: new ObjectId(id) }) },
8-
fetchAdmin: async (client, dbName, collectionName, username) => { return await client.db(dbName).collection(collectionName).findOne({ username: username }) }
8+
fetchAdmin: async (client, dbName, collectionName, username) => { return await client.db(dbName).collection(collectionName).findOne({ username: username }) },
9+
fetchRandom: async (client, dbName, collectionName) => { return await client.db(dbName).collection(collectionName).aggregate([{ $sample: { size: 1 } }]).toArray(); }
910
}

src/constants/dbConstants.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ const Constants = {
66
JSR_DB: process.env.JSR_DB,
77
JSRF_DB: process.env.JSRF_DB,
88
BRC_DB: process.env.BRC_DB,
9+
gameMap: {
10+
jsr: process.env.JSR_DB,
11+
jsrf: process.env.JSRF_DB,
12+
brc: process.env.BRC_DB,
13+
},
914
};
1015

1116
export default Constants;
Lines changed: 70 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,103 @@
1-
import { performBRCAction, performCoreAction, performJSRAction, performJSRFAction } from "../config/db.js";
2-
import { Actions } from "../config/dbActions.js";
3-
import { ObjectId } from "mongodb";
1+
import {ObjectId} from "mongodb";
2+
import Constants from "../constants/dbConstants.js";
3+
import {Actions} from "../config/dbActions.js";
4+
import {performDBAction} from "../config/db.js";
5+
import LOGGER from "../utils/logger.js";
46

5-
6-
const Artist = 'Artist';
7-
const Song = 'Song';
7+
const Artist = "Artist";
8+
const Song = "Song";
9+
const {CORE_DB, JSR_DB, JSRF_DB, BRC_DB} = Constants;
810

911
export const getArtists = async (req, res) => {
1012
try {
1113
const artists = await fetchArtists(req);
1214
if (artists) {
1315
return res.send(artists.length === 1 ? artists[0] : artists);
14-
}
16+
}
1517
res.status(404).send();
16-
} catch(err) {
17-
res.status(500).send(`Could not fetch ALL Artists due to error: \n${err}`);
18+
} catch (err) {
19+
LOGGER.error(`Could not fetch ALL Artists`, err);
20+
res.status(500).send(`Could not fetch ALL Artists due to error`, err);
1821
}
19-
}
22+
};
2023

2124
export const getArtistById = async (req, res) => {
2225
try {
23-
const artist = await performCoreAction(Actions.fetchById, Artist, req?.params?.id);
26+
const artist = await performDBAction(
27+
Actions.fetchById,
28+
CORE_DB,
29+
Artist,
30+
req?.params?.id
31+
);
2432
if (artist) {
2533
return res.send(artist);
2634
}
27-
res.status(404).send(`Artist Resource could not be found at requested location`);
28-
} catch(err) {
29-
res.status(500).send(`Could not fetch Artist with ID: ${req.params.id} \n${err}`);
35+
res
36+
.status(404)
37+
.send(`Artist Resource could not be found at requested location`);
38+
} catch (err) {
39+
LOGGER.error(`Could not fetch Artist by Id ${req?.params?.id}`, err);
40+
res
41+
.status(500)
42+
.send(`Could not fetch Artist with ID: ${req.params.id}`, err);
3043
}
31-
}
44+
};
3245

3346
export const getSongsByArtist = async (req, res) => {
3447
try {
3548
const artistId = req?.params?.id;
3649
if (!artistId) {
37-
return res.status(400).send('Invalid ArtistId');
50+
return res.status(400).send("Invalid ArtistId");
3851
}
3952
res.send(await fetchSongsByArtistId(artistId));
40-
} catch(err) {
41-
res.status(500).send(`Could not fetch Songs by Artist with ID: ${req.params.id} \n${err}`);
53+
} catch (err) {
54+
LOGGER.error(`Could not fetch Songs By Artist ${req?.params?.id}`, err);
55+
res
56+
.status(500)
57+
.send(`Could not fetch Songs by Artist with ID: ${req.params.id}`, err);
4258
}
43-
}
44-
59+
};
4560

4661
export const fetchArtists = async (req) => {
4762
if (req?.query) {
48-
return await performCoreAction(Actions.fetchWithQuery, Artist, null, req?.query);
63+
return await performDBAction(
64+
Actions.fetchWithQuery,
65+
CORE_DB,
66+
Artist,
67+
null,
68+
req?.query
69+
);
4970
}
50-
return await performCoreAction(Actions.fetchAll, Artist, null);
51-
}
71+
return await performDBAction(Actions.fetchAll, CORE_DB, Artist, null);
72+
};
5273

5374
export const fetchSongsByArtistId = async (artistId) => {
5475
const songs = [];
55-
const jsrSongs = await performJSRAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
56-
const jsrfSongs = await performJSRFAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
57-
const brcSongs = await performBRCAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
76+
const jsrSongs = await performDBAction(
77+
Actions.fetchWithQuery,
78+
JSR_DB,
79+
Song,
80+
null,
81+
{
82+
artistId: new ObjectId(artistId),
83+
}
84+
);
85+
const jsrfSongs = await performDBAction(
86+
Actions.fetchWithQuery,
87+
JSRF_DB,
88+
Song,
89+
null,
90+
{artistId: new ObjectId(artistId)}
91+
);
92+
const brcSongs = await performDBAction(
93+
Actions.fetchWithQuery,
94+
BRC_DB,
95+
Song,
96+
null,
97+
{
98+
artistId: new ObjectId(artistId),
99+
}
100+
);
58101
if (jsrSongs && jsrSongs.length > 0) {
59102
songs.push(jsrSongs);
60103
}
@@ -65,4 +108,4 @@ export const fetchSongsByArtistId = async (artistId) => {
65108
songs.push(brcSongs);
66109
}
67110
return songs.flat(1);
68-
}
111+
};

0 commit comments

Comments
 (0)