From 79a6cb86ed7d6ac242c8d79fb80dfd13b060dece Mon Sep 17 00:00:00 2001 From: Bruce Berrios <58147810+Bruception@users.noreply.github.com> Date: Wed, 9 Mar 2022 06:51:20 -0500 Subject: [PATCH] Migrate presets to TS (#2662) by bruce --- backend/dao/preset.js | 54 ------------- backend/dao/preset.ts | 75 +++++++++++++++++++ .../src/scripts/popups/edit-preset-popup.ts | 2 +- 3 files changed, 76 insertions(+), 55 deletions(-) delete mode 100644 backend/dao/preset.js create mode 100644 backend/dao/preset.ts diff --git a/backend/dao/preset.js b/backend/dao/preset.js deleted file mode 100644 index 95af8d46f..000000000 --- a/backend/dao/preset.js +++ /dev/null @@ -1,54 +0,0 @@ -import MonkeyError from "../utils/error"; -import db from "../init/db"; -import { ObjectId } from "mongodb"; - -class PresetDAO { - static async getPresets(uid) { - const preset = await db - .collection("presets") - .find({ uid }) - .sort({ timestamp: -1 }) - .toArray(); // this needs to be changed to later take patreon into consideration - return preset; - } - - static async addPreset(uid, name, config) { - const count = await db.collection("presets").find({ uid }).count(); - if (count >= 10) throw new MonkeyError(409, "Too many presets"); - let preset = await db - .collection("presets") - .insertOne({ uid, name, config }); - return { - insertedId: preset.insertedId, - }; - } - - static async editPreset(uid, _id, name, config) { - console.log(_id); - const preset = await db - .collection("presets") - .findOne({ uid, _id: new ObjectId(_id) }); - if (!preset) throw new MonkeyError(404, "Preset not found"); - if (config) { - return await db - .collection("presets") - .updateOne({ uid, _id: new ObjectId(_id) }, { $set: { name, config } }); - } else { - return await db - .collection("presets") - .updateOne({ uid, _id: new ObjectId(_id) }, { $set: { name } }); - } - } - - static async removePreset(uid, _id) { - const preset = await db - .collection("presets") - .findOne({ uid, _id: new ObjectId(_id) }); - if (!preset) throw new MonkeyError(404, "Preset not found"); - return await db - .collection("presets") - .deleteOne({ uid, _id: new ObjectId(_id) }); - } -} - -export default PresetDAO; diff --git a/backend/dao/preset.ts b/backend/dao/preset.ts new file mode 100644 index 000000000..f4f2b0b86 --- /dev/null +++ b/backend/dao/preset.ts @@ -0,0 +1,75 @@ +import MonkeyError from "../utils/error"; +import db from "../init/db"; +import { ObjectId, Filter } from "mongodb"; + +const MAX_PRESETS = 10; +const COLLECTION_NAME = "presets"; + +function getPresetKeyFilter(uid: string, keyId: string): Filter { + return { + _id: new ObjectId(keyId), + uid, + }; +} + +interface PresetCreationResult { + presetId: string; +} + +class PresetDAO { + // TODO: Add typings for presets/configs, must look into shared type declarations. + static async getPresets(uid: string): Promise { + const presets = await db + .collection(COLLECTION_NAME) + .find({ uid }) + .sort({ timestamp: -1 }) + .toArray(); // this needs to be changed to later take patreon into consideration + return presets; + } + + static async addPreset( + uid: string, + name: string, + config: any + ): Promise { + const presets = await this.getPresets(uid); + if (presets.length >= MAX_PRESETS) { + throw new MonkeyError(409, "Too many presets"); + } + + const preset = await db + .collection(COLLECTION_NAME) + .insertOne({ uid, name, config } as any); + return { + presetId: preset.insertedId.toHexString(), + }; + } + + static async editPreset( + uid: string, + presetId: string, + name: string, + config: any + ): Promise { + const presetUpdates = config ? { name, config } : { name }; + const updateResult = await db + .collection(COLLECTION_NAME) + .updateOne(getPresetKeyFilter(uid, presetId), { $set: presetUpdates }); + + if (updateResult.modifiedCount === 0) { + throw new MonkeyError(404, "Preset not found"); + } + } + + static async removePreset(uid: string, presetId: string): Promise { + const deleteResult = await db + .collection(COLLECTION_NAME) + .deleteOne(getPresetKeyFilter(uid, presetId)); + + if (deleteResult.deletedCount === 0) { + throw new MonkeyError(404, "Preset not found"); + } + } +} + +export default PresetDAO; diff --git a/frontend/src/scripts/popups/edit-preset-popup.ts b/frontend/src/scripts/popups/edit-preset-popup.ts index 0bc95e1ed..4b6968e3d 100644 --- a/frontend/src/scripts/popups/edit-preset-popup.ts +++ b/frontend/src/scripts/popups/edit-preset-popup.ts @@ -101,7 +101,7 @@ async function apply(): Promise { snapshotPresets.push({ name: presetName, config: configChanges, - _id: response.data.insertedId, + _id: response.data.presetId, }); } } else if (action === "edit") {