From b6376fb4a61b42d22aa503e5cfdd9bc654929667 Mon Sep 17 00:00:00 2001 From: "Zander M." Date: Fri, 29 Sep 2023 18:51:49 -0400 Subject: [PATCH] API & DB: Finish implementation of builds --- database/build.js | 55 ++++++++++++++++++++++++++++++++++++++++++++- database/counter.js | 6 ++--- database/mod.js | 4 ++-- database/modpack.js | 14 ++++++++++++ routes/api.js | 47 ++++++++++++++++++++------------------ 5 files changed, 98 insertions(+), 28 deletions(-) diff --git a/database/build.js b/database/build.js index eef8251..8c9d518 100644 --- a/database/build.js +++ b/database/build.js @@ -1,11 +1,11 @@ const debug = require('debug')('technicflux:server') +const database = require('./database') const mongoose = require('mongoose') const { SchemaTypes } = require('mongoose') const Schema = mongoose.Schema // --- Schema --- const buildSchema = new Schema({ - modpack: { type: SchemaTypes.ObjectId, ref: 'technicflux_modpacks' }, version: String, minecraft: String, java: String, @@ -18,3 +18,56 @@ const buildSchema = new Schema({ exports.Build = mongoose.model('technicflux_builds', buildSchema) // --- Related Functions --- + +exports.createBuild = (bVersion, bMinecraft, bJava, bMemory, bForge) => { + // Create a build object + const newBuild = new exports.Build({ + version: bVersion, + minecraft: bMinecraft, + java: bJava, + memory: bMemory, + forge: bForge, + mods: [] + }) + + // Commit it to the database + return newBuild.save().then((build) => { + // Build successfully created + debug(`Successfully added new build ${build.version}.\n`) + return build + }).catch((reason) => { + // Build failed to create + debug(`ERROR (DB: when saving new build): ${reason}\n`) + return false + }) +} + +exports.getAllModpackBuilds = (mSlug) => { + return database.modpack.getModpackBySlug(`${mSlug}`).populate('builds').exec().then((modpack) => { + return modpack.builds + }).catch((reason) => { + // Failed to fetch the modpack's builds + debug(`ERROR (DB): Couldn't fetch modpack because of: ${reason}\n`) + return undefined + }) +} + +exports.getModpackBuild = (mSlug, bVersion) => { + return database.modpack.getModpackBySlug(`${mSlug}`).then((modpack) => { + // Find the build. + const mBuild = modpack.builds.find(build => build.version === bVersion) + + // The reason why we search separately is to prevent populating the mods of every modpack build. + return exports.Build.findOne({ _id: mBuild._id }).populate('mods').exec().then((build) => { + return build + }).catch((reason) => { + // Failed to fetch the build + debug(`ERROR (DB): Couldn't fetch the build (${mSlug}: ${bVersion}) because of: ${reason}\n`) + return undefined + }) + }).catch((reason) => { + // Failed to fetch the modpack's builds + debug(`ERROR (DB): Couldn't fetch modpack because of: ${reason}\n`) + return undefined + }) +} diff --git a/database/counter.js b/database/counter.js index 88384a6..ed1c1f7 100644 --- a/database/counter.js +++ b/database/counter.js @@ -4,8 +4,8 @@ const Schema = mongoose.Schema // --- Incremental Counter --- // Used for tracking the value of fields const CounterSchema = Schema({ - _id: {type: String, required: true}, + _id: { type: String, required: true }, seq: { type: Number, default: 0 } -}); +}) -exports.Counter = mongoose.model('technicflux_modcounter', CounterSchema); +exports.Counter = mongoose.model('technicflux_modcounter', CounterSchema) diff --git a/database/mod.js b/database/mod.js index a45fb80..156c0c4 100644 --- a/database/mod.js +++ b/database/mod.js @@ -25,8 +25,8 @@ const modSchema = new Schema({ // Handle incremental updates of the mod_id modSchema.pre('save', (next) => { - let doc = this; - database.counter.Counter.findByIdAndUpdate({_id: 'modId'}, {$inc: { seq: 1 }}, (error, counter) => { + const doc = this + database.counter.Counter.findByIdAndUpdate({ _id: 'modId' }, { $inc: { seq: 1 } }, (error, counter) => { if (error) { return next(error) } diff --git a/database/modpack.js b/database/modpack.js index 9e9faa9..46cfc8f 100644 --- a/database/modpack.js +++ b/database/modpack.js @@ -1,5 +1,6 @@ const debug = require('debug')('technicflux:server') const mongoose = require('mongoose') +const database = require('./database') const { SchemaTypes } = require('mongoose') const Schema = mongoose.Schema @@ -111,3 +112,16 @@ exports.deleteModpack = (mSlug) => { return false }) } + +exports.addModpackBuild = (mSlug, bVersion, bMinecraft, bJava, bMemory, bForge) => { + return database.build.createBuild(bVersion, bMinecraft, bJava, bMemory, bForge).exec().then((build) => { + exports.Modpack.updateOne( + { name: mSlug }, + { + $push: { + builds: build._id + } + } + ) + }) +} diff --git a/routes/api.js b/routes/api.js index c5e04d2..3f6a752 100644 --- a/routes/api.js +++ b/routes/api.js @@ -150,32 +150,35 @@ router.get('/modpack/:slug/:build?', (req, res) => { display_name: modpack.display_name, recommended: modpack.recommended, latest: modpack.latest, - builds: modpack.builds.map((build) => build.version) + builds: modpack.builds.map((mBuild) => mBuild.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 - } - ] + return database.build.getModpackBuild(`${slug}`, `${build}`).then((mBuild) => { + if (mBuild !== undefined) { + // Build found + return res.json({ + minecraft: mBuild.minecraft, + forge: null, + java: mBuild.java, + memory: mBuild.memory, + mods: mBuild.mods.map((mod) => { + return { + name: mod.name, + version: mod.version, + md5: mod.md5, + url: mod.link, + filesize: 0 + } + }) + }) + } else { + // Build Not Found + res.status(404).json({ + error: 'Build does not exist' + }) + } }) - - // Build Not Found - // res.status(404).json({ - // "error": "Build does not exist" - // }) } }).catch(() => { // Issue with db request