mirror of
https://github.com/Zandercraft/TechnicFlux.git
synced 2026-10-27 04:19:21 +00:00
Complete mod and some modpack API routes.
This commit is contained in:
49
database.js
49
database.js
@@ -28,6 +28,7 @@ const modpackSchema = new Schema({
|
||||
})
|
||||
|
||||
const modSchema = new Schema({
|
||||
mod_id: Number,
|
||||
name: String,
|
||||
pretty_name: String,
|
||||
version: String,
|
||||
@@ -37,10 +38,14 @@ const modSchema = new Schema({
|
||||
author: String,
|
||||
description: String,
|
||||
link: String,
|
||||
donate: String
|
||||
donate: String,
|
||||
url: String,
|
||||
filesize: Number
|
||||
})
|
||||
|
||||
const buildSchema = new Schema({
|
||||
modpack: { type: SchemaTypes.ObjectId, ref: 'technicflux_modpacks' },
|
||||
version: String,
|
||||
minecraft: String,
|
||||
java: String,
|
||||
memory: Number,
|
||||
@@ -246,7 +251,7 @@ exports.createModpack = (mSlug, mDisplayName, mOwner) => {
|
||||
}
|
||||
|
||||
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
|
||||
return modpack
|
||||
}).catch((reason) => {
|
||||
@@ -257,7 +262,7 @@ exports.getModpackBySlug = (mSlug) => {
|
||||
}
|
||||
|
||||
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.
|
||||
return modpacks
|
||||
}).catch((reason) => {
|
||||
@@ -267,20 +272,20 @@ exports.getModpacksByOwner = (mOwner) => {
|
||||
})
|
||||
}
|
||||
|
||||
exports.getModpacksByCollaborator = (mCollaborator) => {
|
||||
return Modpack.find({ collaborators: mCollaborator }).populate('owners').populate('collaborators').exec().then((modpacks) => {
|
||||
exports.getModpacksByContributor = (mContributor) => {
|
||||
return Modpack.find({ contributors: mContributor }).populate('owners').populate('contributors').populate('builds').exec().then((modpacks) => {
|
||||
// Modpacks found.
|
||||
return modpacks
|
||||
}).catch((reason) => {
|
||||
// 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
|
||||
})
|
||||
}
|
||||
|
||||
exports.getAllModpacks = () => {
|
||||
// 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
|
||||
return modpacks
|
||||
}).catch((reason) => {
|
||||
@@ -317,9 +322,22 @@ exports.deleteModpack = (mSlug) => {
|
||||
|
||||
// --- Mod-Related Functions ---
|
||||
exports.getModBySlug = (mSlug) => {
|
||||
return Mod.findOne({ name: mSlug }).exec().then((mod) => {
|
||||
// Mod found
|
||||
return mod
|
||||
return Mod.find({ name: mSlug }).exec().then((mods) => {
|
||||
if (mods === null || mods.length === 0) {
|
||||
// Mod not found
|
||||
return null
|
||||
} else {
|
||||
// Mod found
|
||||
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) => {
|
||||
// No mod found with this slug
|
||||
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) => {
|
||||
return Mod.find({ type: mType }).exec().then((mods) => {
|
||||
// Mods found
|
||||
|
||||
178
routes/api.js
178
routes/api.js
@@ -1,6 +1,7 @@
|
||||
const express = require('express')
|
||||
const pckg = require('../package.json')
|
||||
const branchName = require('current-git-branch')()
|
||||
const database = require('../database')
|
||||
const router = express.Router()
|
||||
|
||||
router.get('/', (req, res) => {
|
||||
@@ -12,88 +13,119 @@ router.get('/', (req, res) => {
|
||||
})
|
||||
|
||||
router.get('/mod/:modname/:modversion?', (req, res) => {
|
||||
// Found mod
|
||||
res.json({
|
||||
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'
|
||||
]
|
||||
let modName = req.params.modname
|
||||
let modVersion = req.params?.modversion
|
||||
|
||||
return database.getModBySlug(`${modName}`).then((mod) => {
|
||||
// Check if a mod was found
|
||||
if (mod === null) {
|
||||
// Mod Not Found
|
||||
return res.status(404).json({
|
||||
"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"
|
||||
})
|
||||
})
|
||||
|
||||
// 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"
|
||||
// })
|
||||
|
||||
// Mod Not Found
|
||||
// res.status(404).json({
|
||||
// "error": "Mod does not exist"
|
||||
// })
|
||||
})
|
||||
|
||||
router.get('/modpack', (req, res) => {
|
||||
// Modpack Index
|
||||
res.json({
|
||||
modpacks: {
|
||||
slug: 'Example Modpack Pretty Name'
|
||||
},
|
||||
mirror_url: `http://${process.env.HOST}/mods`
|
||||
return database.getAllModpacks().then((modpacks) => {
|
||||
// Modpack Index
|
||||
return res.json({
|
||||
modpacks: (modpacks === null) ? {} : modpacks.reduce((obj, item) => {
|
||||
obj[item['name']] = String(item['display_name'])
|
||||
return obj
|
||||
}, {}),
|
||||
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) => {
|
||||
// With Slug
|
||||
res.json({
|
||||
name: 'examplepack',
|
||||
display_name: 'Example Pack',
|
||||
recommended: '0.0.1',
|
||||
latest: '0.0.2',
|
||||
builds: [
|
||||
'0.0.1',
|
||||
'0.0.2'
|
||||
]
|
||||
let slug = req.params.slug
|
||||
let build = req.params?.build
|
||||
|
||||
return database.getModpackBySlug(`${slug}`).then((modpack) => {
|
||||
if (modpack === null) {
|
||||
// Modpack Not Found
|
||||
return res.status(404).json({
|
||||
"error": "Modpack does not exist"
|
||||
})
|
||||
} 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
|
||||
return res.json({
|
||||
minecraft: '1.20.1',
|
||||
forge: null,
|
||||
java: 17,
|
||||
memory: 2048,
|
||||
mods: [
|
||||
{
|
||||
name: 'examplemod',
|
||||
version: '0.0.1',
|
||||
md5: '51c1305b56249804926e38fcf3e46640',
|
||||
url: 'https://example.com/file.zip',
|
||||
filesize: 0
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// Build Not Found
|
||||
// res.status(404).json({
|
||||
// "error": "Build does not exist"
|
||||
// })
|
||||
}
|
||||
}).catch(() => {
|
||||
// Issue with db request
|
||||
return res.status(500).json({
|
||||
"error": "Internal Server Error"
|
||||
})
|
||||
})
|
||||
|
||||
// Build found
|
||||
res.json({
|
||||
minecraft: '1.20.1',
|
||||
forge: null,
|
||||
java: 17,
|
||||
memory: 2048,
|
||||
mods: [
|
||||
{
|
||||
name: 'examplemod',
|
||||
version: '0.0.1',
|
||||
md5: '51c1305b56249804926e38fcf3e46640',
|
||||
url: 'https://example.com/file.zip',
|
||||
filesize: 0
|
||||
}
|
||||
]
|
||||
})
|
||||
|
||||
// Build Not Found
|
||||
// res.status(404).json({
|
||||
// "error": "Build does not exist"
|
||||
// })
|
||||
|
||||
// Modpack Not Found
|
||||
// res.status(404).json({
|
||||
// "error": "Modpack does not exist"
|
||||
// })
|
||||
})
|
||||
|
||||
router.get('/verify/:key?', (req, res) => {
|
||||
|
||||
Reference in New Issue
Block a user