Skip to content

Commit 41c4021

Browse files
razznbluek-wilmeth
andauthored
v1.1.0 - BRC Integration (#35)
* all characters * character routes * songs * locations * collectibles * add docs * fix * adjust text * node version * modify readme * v1.1.0 --------- Co-authored-by: Kaipo Wilmeth <kwilmeth@fanthreesixty.com>
1 parent ddbb326 commit 41c4021

22 files changed

Lines changed: 741 additions & 7131 deletions

.env.example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# Filename: <env>.env
2+
13
PORT=
24
BASE_URL=http://localhost:<PORT>
35
LOG_LEVEL=

.nvmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v14.16.0

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11

22
# <img src="https://media-library-swgu.netlify.app/jetsetradio-api-core/jsr-logo.png" width=6% /> JetSetRadio-API
33

4-
Easily consume Jet Set Radio data into your applications! This public API includes data from both JSR and JSRF.
4+
Easily consume Jet Set Radio data into your applications! This public API includes data from:
5+
- Jet Set Radio
6+
- Jet Set Radio Future
7+
- Bomb Rush Cyberfunk
58

69

710
## Purpose

package-lock.json

Lines changed: 93 additions & 6950 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jetsetradio-api",
3-
"version": "1.0.4",
3+
"version": "1.1.0",
44
"description": "A Data Provider relating to the JSR/JSRF universe",
55
"type": "module",
66
"main": "src/app.js",

src/config/db.js

Lines changed: 100 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { MongoClient } from 'mongodb';
2-
import { ObjectId } from 'mongodb';
3-
import dotenv from 'dotenv';
1+
import {MongoClient} from "mongodb";
2+
import {ObjectId} from "mongodb";
3+
import dotenv from "dotenv";
44
dotenv.config();
55

6-
import LOGGER from '../utils/logger.js';
7-
import Constants from '../constants/dbConstants.js';
6+
import LOGGER from "../utils/logger.js";
7+
import Constants from "../constants/dbConstants.js";
88

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

1211
const buildMongoUri = () => {
1312
const user = process.env.MONGO_USER;
@@ -18,7 +17,9 @@ const buildMongoUri = () => {
1817
return LOGGER.error(`Invalid admin user found while building mongo uri`);
1918
}
2019
if (!password) {
21-
return LOGGER.error(`Invalid admin password found while building mongo uri`);
20+
return LOGGER.error(
21+
`Invalid admin password found while building mongo uri`
22+
);
2223
}
2324
if (!clusterName) {
2425
return LOGGER.error(`Invalid cluster name found while building mongo uri`);
@@ -27,97 +28,153 @@ const buildMongoUri = () => {
2728
return LOGGER.error(`Invalid domain name found while building mongo uri`);
2829
}
2930
return `mongodb+srv://${user}:${password}@${clusterName}.${domainName}?retryWrites=true&w=majority`;
30-
}
31+
};
3132

3233
const client = new MongoClient(buildMongoUri());
3334

3435
/* Database Connections */
3536
export const performCoreAdminAction = async (action, username) => {
3637
try {
3738
await client.connect();
38-
return await action(client, CORE_DB, 'Admin', username);
39-
} catch(err) {
39+
return await action(client, CORE_DB, "Admin", username);
40+
} catch (err) {
4041
console.error(err);
4142
return err;
4243
} finally {
4344
await client.close();
4445
}
45-
}
46+
};
4647

4748
export const performCoreAction = async (action, collection, id, qps) => {
4849
try {
4950
await client.connect();
5051
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
51-
return await action(client, CORE_DB, collection, id, getQueryObject(qps), queryActions);
52-
} catch(err) {
52+
return await action(
53+
client,
54+
CORE_DB,
55+
collection,
56+
id,
57+
getQueryObject(qps),
58+
queryActions
59+
);
60+
} catch (err) {
5361
console.error(err);
5462
return err;
5563
} finally {
5664
await client.close();
5765
}
58-
}
66+
};
5967

6068
export const performJSRAction = async (action, collection, id, qps) => {
6169
try {
6270
await client.connect();
6371
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
64-
return await action(client, JSR_DB, collection, id, getQueryObject(qps), queryActions);
65-
} catch(err) {
72+
return await action(
73+
client,
74+
JSR_DB,
75+
collection,
76+
id,
77+
getQueryObject(qps),
78+
queryActions
79+
);
80+
} catch (err) {
6681
console.error(err);
6782
return err;
6883
} finally {
6984
await client.close();
7085
}
71-
}
86+
};
7287

7388
export const performJSRFAction = async (action, collection, id, qps) => {
7489
try {
7590
await client.connect();
7691
const queryActions = [getSortQuery(qps), getLimitSize(qps)];
77-
return await action(client, JSRF_DB, collection, id, getQueryObject(qps), queryActions);
78-
} catch(err) {
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,
115+
collection,
116+
id,
117+
getQueryObject(qps),
118+
queryActions
119+
);
120+
} catch (err) {
79121
console.error(err);
80122
return err;
81123
} finally {
82124
await client.close();
83125
}
84-
}
126+
};
85127

86128
export const listCollections = async (dbName) => {
87129
try {
88130
await client.connect();
89131
return await client.db(dbName).listCollections().toArray();
90-
} catch(err) {
132+
} catch (err) {
91133
console.error(err);
92134
return err;
93135
} finally {
94136
await client.close();
95137
}
96-
}
97-
98-
const excludedKeys = ['sortBy', 'orderBy', 'filter', 'page', 'limit'];
99-
const objectIdKeys = ['gameId', 'locationId', 'levelId', 'graffitiTagId', 'characterId', 'artistId', 'songId',
100-
'game.id', 'location.id', 'adjacentLocations.id', 'level.id', 'graffitiTag.id', 'character.id', 'artist.id', 'song.id'];
138+
};
139+
140+
const excludedKeys = ["sortBy", "orderBy", "filter", "page", "limit"];
141+
const objectIdKeys = [
142+
"gameId",
143+
"locationId",
144+
"levelId",
145+
"graffitiTagId",
146+
"characterId",
147+
"artistId",
148+
"songId",
149+
"game.id",
150+
"location.id",
151+
"adjacentLocations.id",
152+
"level.id",
153+
"graffitiTag.id",
154+
"character.id",
155+
"artist.id",
156+
"song.id",
157+
];
101158

102159
// Prepare the queryParameters as one single object for mongoDB query
103160
const getQueryObject = (qps) => {
104161
if (qps) {
105162
const queryMap = {};
106163

107164
/* Remove special keys that will be used for query suffix */
108-
excludedKeys.forEach(key => delete qps[key]);
165+
excludedKeys.forEach((key) => delete qps[key]);
109166

110167
/* Transform the user query object into a format MongoDB can understand */
111168
for (let [key, value] of Object.entries(qps)) {
112-
if (typeof value === String && value.includes(',')) {
113-
value = value.split(',')
169+
if (typeof value === String && value.includes(",")) {
170+
value = value.split(",");
114171
queryMap[key] = {$all: value}; // Uses AND currently...
115-
} else if (value === 'true' || value === 'false') {
116-
queryMap[key] = value === 'true' ? true : false;
117-
} else if (typeof value === String && value.toLowerCase() === 'female') {
118-
queryMap[key] = 'Female';
119-
} else if (typeof value === String && value.toLowerCase() === 'male') {
120-
queryMap[key] = 'Male';
172+
} else if (value === "true" || value === "false") {
173+
queryMap[key] = value === "true" ? true : false;
174+
} else if (typeof value === String && value.toLowerCase() === "female") {
175+
queryMap[key] = "Female";
176+
} else if (typeof value === String && value.toLowerCase() === "male") {
177+
queryMap[key] = "Male";
121178
} else if (objectIdKeys.includes(key) && ObjectId.isValid(value)) {
122179
queryMap[key] = new ObjectId(value);
123180
} else {
@@ -127,17 +184,16 @@ const getQueryObject = (qps) => {
127184
return queryMap;
128185
}
129186
return {};
130-
}
187+
};
131188

132189
const getSortQuery = (query) => {
133190
const field = query?.sortBy;
134-
const order = query?.orderBy || 'asc';
135-
const orderMap = { asc: 1, desc: -1 };
191+
const order = query?.orderBy || "asc";
192+
const orderMap = {asc: 1, desc: -1};
136193
return field ? {[field]: orderMap[order]} : {};
137-
}
194+
};
138195

139196
const getLimitSize = (query) => {
140-
const limit = query?.limit || '0';
141-
return !isNaN(Number(limit)) && limit !== '0' ? Number(limit) : 0;
142-
}
143-
197+
const limit = query?.limit || "0";
198+
return !isNaN(Number(limit)) && limit !== "0" ? Number(limit) : 0;
199+
};

src/constants/dbConstants.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import dotenv from 'dotenv';
1+
import dotenv from "dotenv";
22
dotenv.config();
33

44
const Constants = {
55
CORE_DB: process.env.CORE_DB,
66
JSR_DB: process.env.JSR_DB,
77
JSRF_DB: process.env.JSRF_DB,
8-
}
8+
BRC_DB: process.env.BRC_DB,
9+
};
910

10-
export default Constants;
11+
export default Constants;

src/controllers/artistController.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { performCoreAction, performJSRAction, performJSRFAction } from "../config/db.js";
1+
import { performBRCAction, performCoreAction, performJSRAction, performJSRFAction } from "../config/db.js";
22
import { Actions } from "../config/dbActions.js";
33
import { ObjectId } from "mongodb";
44

@@ -40,7 +40,6 @@ export const getSongsByArtist = async (req, res) => {
4040
} catch(err) {
4141
res.status(500).send(`Could not fetch Songs by Artist with ID: ${req.params.id} \n${err}`);
4242
}
43-
4443
}
4544

4645

@@ -55,11 +54,15 @@ export const fetchSongsByArtistId = async (artistId) => {
5554
const songs = [];
5655
const jsrSongs = await performJSRAction(Actions.fetchWithQuery, Song, null, { artistId: new ObjectId(artistId) });
5756
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) });
5858
if (jsrSongs && jsrSongs.length > 0) {
5959
songs.push(jsrSongs);
6060
}
6161
if (jsrfSongs && jsrfSongs.length > 0) {
6262
songs.push(jsrfSongs);
6363
}
64+
if (brcSongs && brcSongs.length > 0) {
65+
songs.push(brcSongs);
66+
}
6467
return songs.flat(1);
6568
}

0 commit comments

Comments
 (0)