Skip to content

Commit

Permalink
Complete mod and some modpack API routes.
Browse files Browse the repository at this point in the history
  • Loading branch information
ZandercraftGames committed Sep 28, 2023
1 parent 4a10113 commit eb78295
Show file tree
Hide file tree
Showing 3 changed files with 141 additions and 80 deletions.
Binary file modified README.md
Binary file not shown.
49 changes: 39 additions & 10 deletions database.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ const modpackSchema = new Schema({
})

const modSchema = new Schema({
mod_id: Number,
name: String,
pretty_name: String,
version: String,
Expand All @@ -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,
Expand Down Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -317,16 +322,40 @@ 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}`)
return false
})
}

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
Expand Down
172 changes: 102 additions & 70 deletions routes/api.js
Original file line number Diff line number Diff line change
@@ -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) => {
Expand All @@ -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

// Found mod version
res.json({
id: 0,
md5: '51c1305b56249804926e38fcf3e46640',
filesize: 0,
url: 'https://example.com/file.zip'
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"
})
})

// 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

// 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
}
]
})
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 Not Found
// res.status(404).json({
// "error": "Build does not exist"
// })
// 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
}
]
})

// Modpack Not Found
// res.status(404).json({
// "error": "Modpack does not exist"
// })
// 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"
})
})
})

router.get('/verify/:key?', (req, res) => {
Expand Down

0 comments on commit eb78295

Please sign in to comment.