Skip to content

Commit

Permalink
API & DB: Finish implementation of builds
Browse files Browse the repository at this point in the history
  • Loading branch information
ZandercraftGames committed Sep 29, 2023
1 parent 23e93fe commit b6376fb
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 28 deletions.
55 changes: 54 additions & 1 deletion database/build.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
const debug = require('debug')('technicflux:server')

Check failure on line 1 in database/build.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Standard code style

Standard code style: Install the 'eslint' package
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,
Expand All @@ -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) => {

Check warning on line 34 in database/build.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Void function return value used

Void function return value is used
// 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) => {

Check notice on line 46 in database/build.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Signature mismatch

Invalid number of arguments, expected 1
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) => {

Check notice on line 61 in database/build.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Signature mismatch

Invalid number of arguments, expected 1
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
})
}
6 changes: 3 additions & 3 deletions database/counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
4 changes: 2 additions & 2 deletions database/mod.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
14 changes: 14 additions & 0 deletions database/modpack.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const debug = require('debug')('technicflux:server')

Check failure on line 1 in database/modpack.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Standard code style

Standard code style: Install the 'eslint' package
const mongoose = require('mongoose')
const database = require('./database')
const { SchemaTypes } = require('mongoose')
const Schema = mongoose.Schema

Expand Down Expand Up @@ -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) => {

Check notice on line 117 in database/modpack.js

View workflow job for this annotation

GitHub Actions / Qodana for JS

Signature mismatch

Invalid number of arguments, expected 1
exports.Modpack.updateOne(
{ name: mSlug },
{
$push: {
builds: build._id
}
}
)
})
}
47 changes: 25 additions & 22 deletions routes/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b6376fb

Please sign in to comment.