Implemented most of the modService functions and made various fixes in mod model
This commit is contained in:
parent
10916cc468
commit
ff33af92b7
|
@ -14,23 +14,40 @@ async function getModByName(name) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getFullModInfos(name) {
|
async function getModFullInfos(name) {
|
||||||
|
|
||||||
// Query
|
// Query
|
||||||
const base_infos = db.query(`SELECT * FROM Mods WHERE name = ?`, [name]);
|
const base_infos = db.query(`SELECT * FROM Mods WHERE name = ?`, [name]);
|
||||||
const other_infos = db.query(`SELECT full_description, license, links, creation_date
|
const other_infos = db.query(`SELECT full_description, license, links, creation_date
|
||||||
FROM ModInfos WHERE name = ?`, [name]);
|
FROM ModInfos WHERE name = ?`, [name]);
|
||||||
|
const tags = getModTags(name);
|
||||||
|
|
||||||
// Merge
|
// Merge
|
||||||
const res = {...await base_infos, ...await other_infos};
|
const res = {...await base_infos, ...await other_infos, ...tags};
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function exists(name) {
|
async function getAllVersions(mod_name) {
|
||||||
return db.exists("Mods", "name", name);
|
return await db.query("SELECT * FROM ModVersions WHERE mod = ?", [mod_name]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function getVersionByNumber(mod_name, version_number) {
|
||||||
|
return await db.query(`SELECT * FROM ModVersions
|
||||||
|
WHERE mod = ?
|
||||||
|
AND version_number = ?;`,
|
||||||
|
[mod_name, version_number]);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getVersion(mod_name, version_number, game_version, platform, environment) {
|
||||||
|
return await db.query(`SELECT * FROM ModVersions
|
||||||
|
WHERE mod = ?
|
||||||
|
AND version_number = ?
|
||||||
|
AND game_version = ?
|
||||||
|
AND platform = ?
|
||||||
|
AND environment = ?;`,
|
||||||
|
[mod_name, version_number, game_version, platform, environment]);
|
||||||
|
}
|
||||||
|
|
||||||
// --- Create ---
|
// --- Create ---
|
||||||
|
|
||||||
|
@ -65,20 +82,20 @@ async function createMod(name, display_name, author, description, mod_infos) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addVersion(version_number, channel, changelog, release_date, game_version, platform, environment, url) {
|
async function addVersion(mod, version_number, channel, changelog, release_date, game_version, platform, environment, url) {
|
||||||
|
|
||||||
await db.prepare(`INSERT INTO ModVersions (version_number, channel, changelog, release_date, game_version, platform, url)
|
await db.prepare(`INSERT INTO ModVersions (mod, version_number, channel, changelog, release_date, game_version, environment, platform, url)
|
||||||
VALUES (?, ?, ?, ?, ?, ?, ?);`,
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);`,
|
||||||
[version_number, channel, changelog, release_date, game_version, platform, url]);
|
[mod, version_number, channel, changelog, release_date, game_version, environment, platform, url]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function addTags(tags) {
|
async function addTags(mod, tags) {
|
||||||
// Add asynchronously
|
// Add asynchronously
|
||||||
const promises = tags.map(async (mod) => {
|
const promises = tags.map(async (tag) => {
|
||||||
db.query(`INSERT INTO UserFavoriteMods (username, mod)
|
db.query(`INSERT INTO ModTags (mod, tag)
|
||||||
VALUES (?, ?);`,
|
VALUES (?, ?);`,
|
||||||
[username, mod]);
|
[mod, tag]);
|
||||||
});
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
@ -111,22 +128,21 @@ async function deleteMod(name) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteVersion(name, version_number, channel, game_version, platform, environment) {
|
async function deleteVersion(name, version_number, game_version, platform, environment) {
|
||||||
await db.prepare(`DELETE FROM ModVersions WHERE name = ?
|
await db.prepare(`DELETE FROM ModVersions WHERE mod = ?
|
||||||
AND version_number = ?
|
AND version_number = ?
|
||||||
AND channel = ?
|
|
||||||
AND game_version = ?
|
AND game_version = ?
|
||||||
AND platform = ?
|
AND platform = ?
|
||||||
AND environment = ?;`,
|
AND environment = ?;`,
|
||||||
[name, version_number, channel, game_version, platform, environment]);
|
[name, version_number, game_version, platform, environment]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
async function deleteTags(tags) {
|
async function deleteTags(mod, tags) {
|
||||||
// Remove asynchronously
|
// Remove asynchronously
|
||||||
const promises = tags.map(async (mod) => {
|
const promises = tags.map(async (tag) => {
|
||||||
db.query(`DELETE FROM UserFavoriteMods
|
db.query(`DELETE FROM ModTags
|
||||||
WHERE username = ? AND mod = ?;`, [username, mod]);
|
WHERE mod = ? AND tag = ?;`, [mod, tag]);
|
||||||
});
|
});
|
||||||
await Promise.all(promises);
|
await Promise.all(promises);
|
||||||
|
|
||||||
|
@ -146,10 +162,25 @@ async function updateModInfosAttribute(name, attribute, value) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function ModExists(name) {
|
||||||
|
return db.exists("Mods", "name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function containsVersion(name, version_number, game_version, platform, environment) {
|
||||||
|
throw new AppError(501, "Not implemented");
|
||||||
|
// return db.exists("Mods", "name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function containsTag(name, tag) {
|
||||||
|
throw new AppError(501, "Not implemented");
|
||||||
|
// return db.exists("Mods", "name", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// --- Exports ---
|
// --- Exports ---
|
||||||
|
|
||||||
module.exports = { getAllMods, getModByName, getFullModInfos,
|
module.exports = { getAllMods, getModByName, getFullModInfos,
|
||||||
|
getAllModVersions, getVersionByNumber, getVersion,
|
||||||
createMod, addVersion, addTags,
|
createMod, addVersion, addTags,
|
||||||
updateMod,
|
updateMod,
|
||||||
deleteMod, deleteVersion, deleteTags,
|
deleteMod, deleteVersion, deleteTags,
|
||||||
|
|
|
@ -4,49 +4,149 @@ const { validateModData } = require("../utils/validate_legacy");
|
||||||
const { mdToHtml } = require("../utils/convert");
|
const { mdToHtml } = require("../utils/convert");
|
||||||
const { sanitizeModData } = require("../utils/sanitize");
|
const { sanitizeModData } = require("../utils/sanitize");
|
||||||
|
|
||||||
|
|
||||||
|
// --- Get ---
|
||||||
|
|
||||||
async function getAllMods() {
|
async function getAllMods() {
|
||||||
return model.getAllMods();
|
return model.getAllMods();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getModByName(name) {
|
async function getModByName(name) {
|
||||||
return model.getModByName(name);
|
const res = model.getModByName(name);
|
||||||
|
if (res.length == 0) {
|
||||||
|
throw new AppError(404, "Cannot find mod with this name", "Not found");
|
||||||
|
}
|
||||||
|
return res[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
async function createMod(mod_data) {
|
async function getFullModInfos(name) {
|
||||||
|
const res = model.getFullModInfos(name);
|
||||||
|
if (res.length == 0) {
|
||||||
|
throw new AppError(404, "Cannot find mod with this name", "Not found");
|
||||||
|
}
|
||||||
|
return res[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getModVersion(infos) {
|
||||||
|
const { mod, version_number, game_version, platform, environment} = infos;
|
||||||
|
const res = model.getModVersion(mod, version_number, game_version, platform, environment);
|
||||||
|
if (res.length == 0) {
|
||||||
|
throw new AppError(404, "Cannot find mod with this name", "Not found");
|
||||||
|
}
|
||||||
|
return res[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// --- Create ---
|
||||||
|
|
||||||
|
async function createMod(mod_data, author) {
|
||||||
|
|
||||||
// Check body validity
|
// Check body validity
|
||||||
await validateModData(mod_data);
|
//TODO
|
||||||
|
console.warn("Skipping validity checks for createMod");
|
||||||
|
// await validateModData(mod_data);
|
||||||
|
|
||||||
// Check authenticity
|
// Generate data
|
||||||
//TODO no auth provider
|
const { name, display_name, description, mod_infos } = mod_data;
|
||||||
|
mod_infos.full_description = await mdToHtml(mod_infos.full_description); // Convert
|
||||||
|
await sanitizeModData(mod_data); // Sanitize
|
||||||
|
|
||||||
// Convert
|
// Write changes to database
|
||||||
mod_data.otherInfos.description = await mdToHtml(mod_data.otherInfos.description);
|
model.createMod(name, display_name, author, description, mod_data);
|
||||||
mod_data.otherInfos.changelogs = await mdToHtml(mod_data.otherInfos.changelogs);
|
|
||||||
|
|
||||||
// Sanitize
|
// Return
|
||||||
await sanitizeModData(mod_data);
|
return getModByName(name);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
console.debug("Passed validity checks");
|
|
||||||
model.createMod(mod_data);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function addVersion(version_data) {
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
//TODO
|
||||||
|
console.warn("Skipping validity checks for addVersion");
|
||||||
|
|
||||||
|
// Generate data
|
||||||
|
const { mod_name, version_number, channel, changelog, game_version,
|
||||||
|
platform, environment, url } = version_data; // Split
|
||||||
|
changelog = await mdToHtml(changelog); // Convert
|
||||||
|
await sanitizeModData(mod_data); // Sanitize
|
||||||
|
const release_date = (new Date()).toLocaleDateString();
|
||||||
|
|
||||||
|
// Write changes
|
||||||
|
await model.addVersion(mod_name, version_number, channel, changelog,
|
||||||
|
release_date, game_version, platform, environment, url); // Database
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return await model.getModVersion(mod_name, version_number, game_version, platform, environment );
|
||||||
|
}
|
||||||
|
|
||||||
|
async function addTags(mod, tags) {
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
//TODO
|
||||||
|
console.warn("Skipping validity checks for addTags");
|
||||||
|
|
||||||
|
// Write changes
|
||||||
|
await model.addTags(mod, tags);
|
||||||
|
|
||||||
|
// Return
|
||||||
|
const { tags } = await model.getFullModInfos(mod);
|
||||||
|
return { "mod": mod, "tags": tags};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Update ---
|
||||||
|
|
||||||
|
async function updateMod(diff_data) {
|
||||||
|
//TODO
|
||||||
|
throw new AppError(501, "Not implemented");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// Delete
|
||||||
|
|
||||||
async function deleteMod(name) {
|
async function deleteMod(name) {
|
||||||
|
|
||||||
// Check existence
|
// Check existence
|
||||||
const exists = await model.exists(name);
|
const mod = await model.getModByName(name);
|
||||||
if (!exists) {
|
|
||||||
throw new AppError(404, "Not found: Cannot find mod with this name");
|
// Authorize
|
||||||
|
// TODO move outside of this function
|
||||||
|
if (mod.author != mod.user) {
|
||||||
|
throw new AppError(403, "You don't have the necessary permissions to execute this action", "Forbidden");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check authenticity
|
// Write changes to database
|
||||||
//TODO no auth provider
|
await model.deleteMod(name);
|
||||||
|
|
||||||
model.deleteMod(name);
|
// Return
|
||||||
return;
|
return mod;
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = { getAllMods, getModByName, createMod, deleteMod };
|
async function deleteVersion(version_infos) {
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
// TODO
|
||||||
|
|
||||||
|
// Generate data
|
||||||
|
const res = await getModVersion(version_infos);
|
||||||
|
const { mod, version_number, game_version, platform, environment} = version_infos;
|
||||||
|
|
||||||
|
// Write changes to db
|
||||||
|
await model.deleteVersion(mod, version_number, game_version, platform, environment);
|
||||||
|
|
||||||
|
// Return
|
||||||
|
return res;
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteTags(tags) {
|
||||||
|
|
||||||
|
// Check existence
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { getAllMods, getModByName, getFullModInfos,
|
||||||
|
createMod, addTags, addVersion,
|
||||||
|
updateMod,
|
||||||
|
deleteMod, deleteTags, deleteVersion };
|
Loading…
Reference in a new issue