Updated mod model to work with the new database
This commit is contained in:
parent
4a10e0ff12
commit
072fbbd873
|
@ -2,8 +2,11 @@ const { getDatabase } = require('../database/index');
|
|||
const AppError = require('../utils/appError');
|
||||
const db = getDatabase();
|
||||
|
||||
|
||||
// --- Get ---
|
||||
|
||||
async function getAllMods() {
|
||||
return db.query("SELECT name FROM Mods");
|
||||
return await db.query("SELECT name, display_name, author, description FROM Mods");
|
||||
}
|
||||
|
||||
async function getModByName(name) {
|
||||
|
@ -11,39 +14,136 @@ async function getModByName(name) {
|
|||
|
||||
}
|
||||
|
||||
async function getFullModInfos(name) {
|
||||
|
||||
// Query
|
||||
const base_infos = db.query(`SELECT * FROM Mods WHERE name = ?`, [name]);
|
||||
const other_infos = db.query(`SELECT full_description, license, links, creation_date
|
||||
FROM ModInfos WHERE name = ?`, [name]);
|
||||
|
||||
// Merge
|
||||
const res = {...await base_infos, ...await other_infos};
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
async function exists(name) {
|
||||
return db.exists("Mods", "name", name);
|
||||
}
|
||||
|
||||
async function createMod(mod_data) {
|
||||
|
||||
const { name, displayName, author, versions, otherInfos } = mod_data;
|
||||
const { description, links, tags, screenshots, license, changelogs, counts } = otherInfos;
|
||||
// --- Create ---
|
||||
|
||||
async function createMod(name, display_name, author, description, mod_infos) {
|
||||
|
||||
// Extract infos
|
||||
const { full_description, license, links, creation_date, tags } = mod_infos;
|
||||
|
||||
// Mods table
|
||||
await db.prepare("INSERT INTO Mods (name, display_name, author, description) \
|
||||
VALUES (?, ?, ?, ?)",
|
||||
[name, display_name, author, description]);
|
||||
|
||||
// ModInfos table
|
||||
await db.prepare(`INSERT INTO ModInfos (mod, full_description, license, links, creation_date)
|
||||
VALUES (?, ?, ?, ?, ?)`,
|
||||
[name, full_description, license.type, links.toString(), creation_date]);
|
||||
|
||||
// Tags
|
||||
const tags_proc = updateTags(name, tags, []);
|
||||
|
||||
// License
|
||||
if (license_type == "custom") {
|
||||
await db.prepare(`UPDATE ModInfos SET custom_license = ?
|
||||
WHERE mod = ?`,
|
||||
[license.content, name]);
|
||||
}
|
||||
|
||||
// Await
|
||||
await tags_proc;
|
||||
|
||||
await db.prepare("INSERT INTO mods (name, display_name, author) \
|
||||
VALUES (?, ?, ?, ?)", [name, displayName, author]);
|
||||
// db.prepare("INSERT INTO modsDescription (Name, Description, Links, Tags, Screenshots, License, Changelogs, Counts) \
|
||||
// VALUES (?, ?, ?, ?, ?, ?, ?, ?)", [name, description, links, tags, screenshots, license, changelogs, counts]);
|
||||
return;
|
||||
}
|
||||
|
||||
async function addVersion(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)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?);`,
|
||||
[version_number, channel, changelog, release_date, game_version, platform, url]);
|
||||
return;
|
||||
}
|
||||
|
||||
async function AddTags(tags) {
|
||||
// Add asynchronously
|
||||
const promises = tags.map(async (mod) => {
|
||||
db.query(`INSERT INTO UserFavoriteMods (username, mod)
|
||||
VALUES (?, ?);`,
|
||||
[username, mod]);
|
||||
});
|
||||
await Promise.all(promises);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// --- Update ---
|
||||
|
||||
async function updateMod(name, display_name, author, description) {
|
||||
|
||||
if (display_name) {
|
||||
await updateModAttributes(name, "display_name", display_name);
|
||||
}
|
||||
|
||||
if (author) {
|
||||
await updateModAttribute(nale, "author", author);
|
||||
}
|
||||
|
||||
if (profile_picture) {
|
||||
await updateUserAttribute(name, "description", description)
|
||||
}
|
||||
}
|
||||
|
||||
async function updateModAttribute(name, attribute, value) {
|
||||
await db.prepare(`UPDATE Mods SET ${attribute} = ? WHERE name = ?`, [value, name]);
|
||||
return;
|
||||
}
|
||||
|
||||
async function updateModInfosAttribute(name, attribute, value) {
|
||||
await db.prepare(`UPDATE ModInfos SET ${attribute} = ? WHERE name = ?`, [value, name]);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// --- Delete ---
|
||||
|
||||
async function deleteMod(name) {
|
||||
console.log("WARNING: using a WIP function : deleteMod (models/mods.js)");
|
||||
db.prepare("DELETE FROM Mods WHERE name = ?", [name]);
|
||||
// db.prepare("DELETE FROM modsDescription WHERE Name = ?", [name]);
|
||||
await db.prepare("DELETE FROM Mods WHERE name = ?", [name]);
|
||||
return;
|
||||
}
|
||||
|
||||
// --- WIP ---
|
||||
async function deleteVersion(name, version_number, channel, game_version, platform, environment) {
|
||||
await db.prepare(`DELETE FROM ModVersions WHERE name = ?
|
||||
AND version_number = ?
|
||||
AND channel = ?
|
||||
AND game_version = ?
|
||||
AND platform = ?
|
||||
AND environment = ?;`,
|
||||
[name, version_number, channel, game_version, platform, environment]);
|
||||
return;
|
||||
}
|
||||
|
||||
async function updateMod(mod_data) {
|
||||
console.log("WARNING: using a WIP function : updateMod (models/mods.js)");
|
||||
throw new AppError(501, "Not implemented");
|
||||
// const { name, description } = mod_data;
|
||||
// return db.query("INSERT INTO mods (name, description) VALUES (?, ?)", [name, description]);
|
||||
async function deleteTags(tags) {
|
||||
// Remove asynchronously
|
||||
const promises = tags.map(async (mod) => {
|
||||
db.query(`DELETE FROM UserFavoriteMods
|
||||
WHERE username = ? AND mod = ?;`, [username, mod]);
|
||||
});
|
||||
await Promise.all(promises);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
|
||||
// --- Exports ---
|
||||
|
||||
module.exports = { getAllMods, getModByName, createMod, deleteMod, exists }
|
|
@ -26,7 +26,7 @@ async function getUserPassword(name) {
|
|||
}
|
||||
|
||||
async function exists(name) {
|
||||
return db.exists("Users", "username", name);
|
||||
return await db.exists("Users", "username", name);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue