mirror of
https://github.com/Zandercraft/TechnicFlux.git
synced 2026-10-29 04:39:20 +00:00
API & DB: Finish implementation of builds
This commit is contained in:
@@ -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
|
||||
})
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user