Complete mod and some modpack API routes.

This commit is contained in:
2023-09-27 22:45:54 -04:00
parent 4a10113e78
commit eb7829570d
3 changed files with 144 additions and 83 deletions

BIN
README.md

Binary file not shown.

View File

@@ -28,6 +28,7 @@ const modpackSchema = new Schema({
}) })
const modSchema = new Schema({ const modSchema = new Schema({
mod_id: Number,
name: String, name: String,
pretty_name: String, pretty_name: String,
version: String, version: String,
@@ -37,10 +38,14 @@ const modSchema = new Schema({
author: String, author: String,
description: String, description: String,
link: String, link: String,
donate: String donate: String,
url: String,
filesize: Number
}) })
const buildSchema = new Schema({ const buildSchema = new Schema({
modpack: { type: SchemaTypes.ObjectId, ref: 'technicflux_modpacks' },
version: String,
minecraft: String, minecraft: String,
java: String, java: String,
memory: Number, memory: Number,
@@ -246,7 +251,7 @@ exports.createModpack = (mSlug, mDisplayName, mOwner) => {
} }
exports.getModpackBySlug = (mSlug) => { exports.getModpackBySlug = (mSlug) => {
return Modpack.findOne({ name: mSlug }).populate('owners').populate('collaborators').exec().then((modpack) => { return Modpack.findOne({ name: mSlug }).populate('owners').populate('contributors').populate('builds').exec().then((modpack) => {
// Modpack found // Modpack found
return modpack return modpack
}).catch((reason) => { }).catch((reason) => {
@@ -257,7 +262,7 @@ exports.getModpackBySlug = (mSlug) => {
} }
exports.getModpacksByOwner = (mOwner) => { exports.getModpacksByOwner = (mOwner) => {
return Modpack.find({ owners: mOwner }).populate('owners').populate('collaborators').exec().then((modpacks) => { return Modpack.find({ owners: mOwner }).populate('owners').populate('contributors').populate('builds').exec().then((modpacks) => {
// Modpacks found. // Modpacks found.
return modpacks return modpacks
}).catch((reason) => { }).catch((reason) => {
@@ -267,20 +272,20 @@ exports.getModpacksByOwner = (mOwner) => {
}) })
} }
exports.getModpacksByCollaborator = (mCollaborator) => { exports.getModpacksByContributor = (mContributor) => {
return Modpack.find({ collaborators: mCollaborator }).populate('owners').populate('collaborators').exec().then((modpacks) => { return Modpack.find({ contributors: mContributor }).populate('owners').populate('contributors').populate('builds').exec().then((modpacks) => {
// Modpacks found. // Modpacks found.
return modpacks return modpacks
}).catch((reason) => { }).catch((reason) => {
// No modpacks found for this collaborator. // No modpacks found for this collaborator.
debug(`ERROR (DB): Failed to find any modpacks with this collaborator because of: ${reason}`) debug(`ERROR (DB): Failed to find any modpacks with this contributor because of: ${reason}`)
return false return false
}) })
} }
exports.getAllModpacks = () => { exports.getAllModpacks = () => {
// Fetch an array of all modpacks // Fetch an array of all modpacks
return Modpack.find({}).populate('owners').populate('collaborators').exec().then((modpacks) => { return Modpack.find({}).populate('owners').populate('contributors').populate('builds').exec().then((modpacks) => {
// Modpacks found // Modpacks found
return modpacks return modpacks
}).catch((reason) => { }).catch((reason) => {
@@ -317,9 +322,22 @@ exports.deleteModpack = (mSlug) => {
// --- Mod-Related Functions --- // --- Mod-Related Functions ---
exports.getModBySlug = (mSlug) => { exports.getModBySlug = (mSlug) => {
return Mod.findOne({ name: mSlug }).exec().then((mod) => { return Mod.find({ name: mSlug }).exec().then((mods) => {
if (mods === null || mods.length === 0) {
// Mod not found
return null
} else {
// Mod found // Mod found
return mod return {
id: mods[0].mod_id,
name: mods[0].name,
pretty_name: mods[0].pretty_name,
author: mods[0].author,
description: mods[0].description,
link: mods[0].link,
versions: mods.map((modVersion) => modVersion['version'])
}
}
}).catch((reason) => { }).catch((reason) => {
// No mod found with this slug // No mod found with this slug
debug(`ERROR (DB): Failed to find a mod with slug '${mSlug}' because of: ${reason}`) debug(`ERROR (DB): Failed to find a mod with slug '${mSlug}' because of: ${reason}`)
@@ -327,6 +345,17 @@ exports.getModBySlug = (mSlug) => {
}) })
} }
exports.getModVersion = (mSlug, mVersion) => {
return Mod.findOne({ name: mSlug, version: mVersion }).exec().then((mod) => {
// Mod found
return mod
}).catch((reason) => {
// No mod found with this slug and version
debug(`ERROR (DB): Failed to find a mod '${mSlug}:${mVersion}' because of: ${reason}`)
return false
})
}
exports.getModsByType = (mType) => { exports.getModsByType = (mType) => {
return Mod.find({ type: mType }).exec().then((mods) => { return Mod.find({ type: mType }).exec().then((mods) => {
// Mods found // Mods found

View File

@@ -1,6 +1,7 @@
const express = require('express') const express = require('express')
const pckg = require('../package.json') const pckg = require('../package.json')
const branchName = require('current-git-branch')() const branchName = require('current-git-branch')()
const database = require('../database')
const router = express.Router() const router = express.Router()
router.get('/', (req, res) => { router.get('/', (req, res) => {
@@ -12,64 +13,93 @@ router.get('/', (req, res) => {
}) })
router.get('/mod/:modname/:modversion?', (req, res) => { router.get('/mod/:modname/:modversion?', (req, res) => {
// Found mod let modName = req.params.modname
res.json({ let modVersion = req.params?.modversion
id: 0,
name: 'example',
pretty_name: 'Example',
author: 'ExampleAuthor',
description: 'This is the description',
link: 'https://example.com/',
versions: [
'1.20.1-0.0.1',
'1.20.1-0.0.2'
]
})
// Found mod version
res.json({
id: 0,
md5: '51c1305b56249804926e38fcf3e46640',
filesize: 0,
url: 'https://example.com/file.zip'
})
// Version Not Found
// res.status(404).json({
// "error": "Mod version does not exist"
// })
return database.getModBySlug(`${modName}`).then((mod) => {
// Check if a mod was found
if (mod === null) {
// Mod Not Found // Mod Not Found
// res.status(404).json({ return res.status(404).json({
// "error": "Mod does not exist" "error": "Mod does not exist"
// }) })
} else if (modVersion === undefined) {
// Request is for the mod's info.
return res.json(mod)
} else {
// Request is for a particular version.
return database.getModVersion(modName, modVersion).then((version) => {
if (version === null) {
// Version Not Found
return res.status(404).json({
"error": "Mod version does not exist"
})
} else {
// Found mod version
return res.json({
id: version.mod_id,
md5: version.md5,
filesize: version.filesize,
url: version.url
})
}
}).catch(() => {
// Issue with db request
return res.status(500).json({
"error": "Internal Server Error"
})
})
}
}).catch(() => {
// Issue with db request
return res.status(500).json({
"error": "Internal Server Error"
})
})
}) })
router.get('/modpack', (req, res) => { router.get('/modpack', (req, res) => {
return database.getAllModpacks().then((modpacks) => {
// Modpack Index // Modpack Index
res.json({ return res.json({
modpacks: { modpacks: (modpacks === null) ? {} : modpacks.reduce((obj, item) => {
slug: 'Example Modpack Pretty Name' obj[item['name']] = String(item['display_name'])
}, return obj
mirror_url: `http://${process.env.HOST}/mods` }, {}),
mirror_url: `http://${process.env.HOST}/mods/`
})
}).catch(() => {
// Issue with db request
return res.status(500).json({
"error": "Internal Server Error"
})
}) })
}) })
router.get('/modpack/:slug/:build?', (req, res) => { router.get('/modpack/:slug/:build?', (req, res) => {
// With Slug let slug = req.params.slug
res.json({ let build = req.params?.build
name: 'examplepack',
display_name: 'Example Pack', return database.getModpackBySlug(`${slug}`).then((modpack) => {
recommended: '0.0.1', if (modpack === null) {
latest: '0.0.2', // Modpack Not Found
builds: [ return res.status(404).json({
'0.0.1', "error": "Modpack does not exist"
'0.0.2'
]
}) })
} else if (build === undefined) {
// Modpack info
return res.json({
name: modpack.name,
display_name: modpack.display_name,
recommended: modpack.recommended,
latest: modpack.latest,
builds: modpack.builds.map((build) => build['version'])
})
} else {
// Modpack build info
// Build found // Build found
res.json({ return res.json({
minecraft: '1.20.1', minecraft: '1.20.1',
forge: null, forge: null,
java: 17, java: 17,
@@ -89,11 +119,13 @@ router.get('/modpack/:slug/:build?', (req, res) => {
// res.status(404).json({ // res.status(404).json({
// "error": "Build does not exist" // "error": "Build does not exist"
// }) // })
}
// Modpack Not Found }).catch(() => {
// res.status(404).json({ // Issue with db request
// "error": "Modpack does not exist" return res.status(500).json({
// }) "error": "Internal Server Error"
})
})
}) })
router.get('/verify/:key?', (req, res) => { router.get('/verify/:key?', (req, res) => {