mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-10 05:35:05 +08:00
Migrate presets to TS (#2662) by bruce
This commit is contained in:
parent
ef69178abf
commit
79a6cb86ed
3 changed files with 76 additions and 55 deletions
|
@ -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;
|
75
backend/dao/preset.ts
Normal file
75
backend/dao/preset.ts
Normal file
|
@ -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<any> {
|
||||
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<any[]> {
|
||||
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<PresetCreationResult> {
|
||||
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<void> {
|
||||
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<void> {
|
||||
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;
|
|
@ -101,7 +101,7 @@ async function apply(): Promise<void> {
|
|||
snapshotPresets.push({
|
||||
name: presetName,
|
||||
config: configChanges,
|
||||
_id: response.data.insertedId,
|
||||
_id: response.data.presetId,
|
||||
});
|
||||
}
|
||||
} else if (action === "edit") {
|
||||
|
|
Loading…
Reference in a new issue