diff --git a/backend/api/controllers/preset.js b/backend/api/controllers/preset.js index a3b7a7458..824ce2f22 100644 --- a/backend/api/controllers/preset.js +++ b/backend/api/controllers/preset.js @@ -32,12 +32,12 @@ class PresetController { static async editPreset(req, res, next) { try { - const { id, name, config } = req.body; + const { _id, name, config } = req.body; const { uid } = req.decodedToken; if (!isTagPresetNameValid(name)) throw new MonkeyError(400, "Invalid preset name."); if (config) validateConfig(config); - await PresetDAO.editPreset(uid, id, name, config); + await PresetDAO.editPreset(uid, _id, name, config); return res.sendStatus(200); } catch (e) { return next(e); @@ -46,9 +46,9 @@ class PresetController { static async removePreset(req, res, next) { try { - const { id } = req.body; + const { _id } = req.body; const { uid } = req.decodedToken; - await PresetDAO.removePreset(uid, id); + await PresetDAO.removePreset(uid, _id); return res.sendStatus(200); } catch (e) { return next(e); diff --git a/backend/dao/preset.js b/backend/dao/preset.js index e5b672900..37a51e5f0 100644 --- a/backend/dao/preset.js +++ b/backend/dao/preset.js @@ -1,6 +1,6 @@ const MonkeyError = require("../handlers/error"); const { mongoDB } = require("../init/mongodb"); -const uuid = require("uuid"); +const { ObjectID } = require("mongodb"); class PresetDAO { static async getPresets(uid) { @@ -15,36 +15,39 @@ class PresetDAO { static async addPreset(uid, name, config) { const count = await mongoDB().collection("presets").find({ uid }).count(); if (count >= 10) throw new MonkeyError(409, "Too many presets"); - const id = uuid.v4(); - await mongoDB() + let preset = await mongoDB() .collection("presets") - .insertOne({ _id: id, uid, name, config }); + .insertOne({ uid, name, config }); return { - id, - name, + insertedId: preset.insertedId, }; } - static async editPreset(uid, id, name, config) { - const preset = await mongoDB().collection("presets").findOne({ uid, id }); + static async editPreset(uid, _id, name, config) { + console.log(_id); + const preset = await mongoDB() + .collection("presets") + .findOne({ uid, _id: ObjectID(_id) }); if (!preset) throw new MonkeyError(404, "Preset not found"); if (config) { return await mongoDB() .collection("presets") - .updateOne({ uid, _id: id }, { $set: { name, config } }); + .updateOne({ uid, _id: ObjectID(_id) }, { $set: { name, config } }); } else { return await mongoDB() .collection("presets") - .updateOne({ uid, _id: id }, { $set: { name } }); + .updateOne({ uid, _id: ObjectID(_id) }, { $set: { name } }); } } - static async removePreset(uid, id) { + static async removePreset(uid, _id) { const preset = await mongoDB() .collection("presets") - .findOne({ uid, _id: id }); + .findOne({ uid, _id: ObjectID(_id) }); if (!preset) throw new MonkeyError(404, "Preset not found"); - return await mongoDB().collection("presets").remove({ uid, _id: id }); + return await mongoDB() + .collection("presets") + .deleteOne({ uid, _id: ObjectID(_id) }); } } diff --git a/src/js/commandline-lists.js b/src/js/commandline-lists.js index 2b35db653..8294b1a44 100644 --- a/src/js/commandline-lists.js +++ b/src/js/commandline-lists.js @@ -273,10 +273,10 @@ export function updatePresetCommands() { let dis = preset.name; commandsPresets.list.push({ - id: "applyPreset" + preset.id, + id: "applyPreset" + preset._id, display: dis, exec: () => { - PresetController.apply(preset.id); + PresetController.apply(preset._id); TestUI.updateModesNotice(); }, }); diff --git a/src/js/popups/edit-preset-popup.js b/src/js/popups/edit-preset-popup.js index 738ab5832..57fbdbcb4 100644 --- a/src/js/popups/edit-preset-popup.js +++ b/src/js/popups/edit-preset-popup.js @@ -73,7 +73,7 @@ async function apply() { let activeTagIds = []; DB.getSnapshot().tags.forEach((tag) => { if (tag.active) { - activeTagIds.push(tag.id); + activeTagIds.push(tag._id); } }); configChanges.tags = activeTagIds; @@ -100,41 +100,17 @@ async function apply() { DB.getSnapshot().presets.push({ name: inputVal, config: configChanges, - id: response.data.id, + _id: response.data.insertedId, }); Settings.update(); } } else if (action === "edit") { - // Loader.show(); - // axiosInstance - // .post("/editPreset", { - // presetName: inputVal, - // presetid: presetid, - // config: configChanges, - // }) - // .then((e) => { - // Loader.hide(); - // let status = e.data.resultCode; - // if (status === 1) { - // Notifications.add("Preset updated", 1); - // let preset = DB.getSnapshot().presets.filter( - // (preset) => preset._id == presetid - // )[0]; - // preset.name = inputVal; - // preset.config = configChanges; - // Settings.update(); - // } else if (status === -1) { - // Notifications.add("Invalid preset name", 0); - // } else if (status < -1) { - // Notifications.add("Unknown error: " + e.data.message, -1); - // } - // }); Loader.show(); let response; try { response = await axiosInstance.post("/presets/edit", { name: inputVal, - id: presetid, + _id: presetid, config: override === true ? configChanges : null, }); } catch (e) { @@ -149,38 +125,18 @@ async function apply() { } else { Notifications.add("Preset updated", 1); let preset = DB.getSnapshot().presets.filter( - (preset) => preset.id == presetid + (preset) => preset._id == presetid )[0]; preset.name = inputVal; if (override === true) preset.config = configChanges; Settings.update(); } } else if (action === "remove") { - // Loader.show(); - // axiosInstance - // .post("/removePreset", { - // presetid, - // }) - // .then((e) => { - // Loader.hide(); - // let status = e.data.resultCode; - // if (status === 1) { - // Notifications.add("Preset removed", 1); - // DB.getSnapshot().presets.forEach((preset, index) => { - // if (preset.id === presetid) { - // DB.getSnapshot().presets.splice(index, 1); - // } - // }); - // Settings.update(); - // } else if (status < -1) { - // Notifications.add("Unknown error: " + e.data.message, -1); - // } - // }); Loader.show(); let response; try { response = await axiosInstance.post("/presets/remove", { - id: presetid, + _id: presetid, }); } catch (e) { Loader.hide(); @@ -194,7 +150,7 @@ async function apply() { } else { Notifications.add("Preset removed", 1); DB.getSnapshot().presets.forEach((preset, index) => { - if (preset.id === presetid) { + if (preset._id === presetid) { DB.getSnapshot().presets.splice(index, 1); } }); diff --git a/src/js/preset-controller.js b/src/js/preset-controller.js index 4fc4058ba..d973c8c15 100644 --- a/src/js/preset-controller.js +++ b/src/js/preset-controller.js @@ -5,10 +5,10 @@ import * as Settings from "./settings"; import * as TestLogic from "./test-logic"; import * as TagController from "./tag-controller"; -export function apply(id) { +export function apply(_id) { // console.log(DB.getSnapshot().presets); DB.getSnapshot().presets.forEach((preset) => { - if (preset.id == id) { + if (preset._id == _id) { Config.apply(JSON.parse(JSON.stringify(preset.config))); TagController.clear(true); if (preset.config.tags) { diff --git a/src/js/settings.js b/src/js/settings.js index 825479a9b..058f223bc 100644 --- a/src/js/settings.js +++ b/src/js/settings.js @@ -452,7 +452,7 @@ function refreshPresetsSettingsSection() { let presetsEl = $(".pageSettings .section.presets .presetsList").empty(); DB.getSnapshot().presets.forEach((preset) => { presetsEl.append(` -
+
${preset.name}