mirror of
https://github.com/Zandercraft/TechnicFlux.git
synced 2026-10-31 04:59: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 debug = require('debug')('technicflux:server')
|
||||||
|
const database = require('./database')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
const { SchemaTypes } = require('mongoose')
|
const { SchemaTypes } = require('mongoose')
|
||||||
const Schema = mongoose.Schema
|
const Schema = mongoose.Schema
|
||||||
|
|
||||||
// --- Schema ---
|
// --- Schema ---
|
||||||
const buildSchema = new Schema({
|
const buildSchema = new Schema({
|
||||||
modpack: { type: SchemaTypes.ObjectId, ref: 'technicflux_modpacks' },
|
|
||||||
version: String,
|
version: String,
|
||||||
minecraft: String,
|
minecraft: String,
|
||||||
java: String,
|
java: String,
|
||||||
@@ -18,3 +18,56 @@ const buildSchema = new Schema({
|
|||||||
exports.Build = mongoose.model('technicflux_builds', buildSchema)
|
exports.Build = mongoose.model('technicflux_builds', buildSchema)
|
||||||
|
|
||||||
// --- Related Functions ---
|
// --- 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 ---
|
// --- Incremental Counter ---
|
||||||
// Used for tracking the value of fields
|
// Used for tracking the value of fields
|
||||||
const CounterSchema = Schema({
|
const CounterSchema = Schema({
|
||||||
_id: {type: String, required: true},
|
_id: { type: String, required: true },
|
||||||
seq: { type: Number, default: 0 }
|
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
|
// Handle incremental updates of the mod_id
|
||||||
modSchema.pre('save', (next) => {
|
modSchema.pre('save', (next) => {
|
||||||
let doc = this;
|
const doc = this
|
||||||
database.counter.Counter.findByIdAndUpdate({_id: 'modId'}, {$inc: { seq: 1 }}, (error, counter) => {
|
database.counter.Counter.findByIdAndUpdate({ _id: 'modId' }, { $inc: { seq: 1 } }, (error, counter) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return next(error)
|
return next(error)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
const debug = require('debug')('technicflux:server')
|
const debug = require('debug')('technicflux:server')
|
||||||
const mongoose = require('mongoose')
|
const mongoose = require('mongoose')
|
||||||
|
const database = require('./database')
|
||||||
const { SchemaTypes } = require('mongoose')
|
const { SchemaTypes } = require('mongoose')
|
||||||
const Schema = mongoose.Schema
|
const Schema = mongoose.Schema
|
||||||
|
|
||||||
@@ -111,3 +112,16 @@ exports.deleteModpack = (mSlug) => {
|
|||||||
return false
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
@@ -150,32 +150,35 @@ router.get('/modpack/:slug/:build?', (req, res) => {
|
|||||||
display_name: modpack.display_name,
|
display_name: modpack.display_name,
|
||||||
recommended: modpack.recommended,
|
recommended: modpack.recommended,
|
||||||
latest: modpack.latest,
|
latest: modpack.latest,
|
||||||
builds: modpack.builds.map((build) => build.version)
|
builds: modpack.builds.map((mBuild) => mBuild.version)
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
// Modpack build info
|
// Modpack build info
|
||||||
|
return database.build.getModpackBuild(`${slug}`, `${build}`).then((mBuild) => {
|
||||||
// Build found
|
if (mBuild !== undefined) {
|
||||||
return res.json({
|
// Build found
|
||||||
minecraft: '1.20.1',
|
return res.json({
|
||||||
forge: null,
|
minecraft: mBuild.minecraft,
|
||||||
java: 17,
|
forge: null,
|
||||||
memory: 2048,
|
java: mBuild.java,
|
||||||
mods: [
|
memory: mBuild.memory,
|
||||||
{
|
mods: mBuild.mods.map((mod) => {
|
||||||
name: 'examplemod',
|
return {
|
||||||
version: '0.0.1',
|
name: mod.name,
|
||||||
md5: '51c1305b56249804926e38fcf3e46640',
|
version: mod.version,
|
||||||
url: 'https://example.com/file.zip',
|
md5: mod.md5,
|
||||||
filesize: 0
|
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(() => {
|
}).catch(() => {
|
||||||
// Issue with db request
|
// Issue with db request
|
||||||
|
|||||||
Reference in New Issue
Block a user