2021-06-09 01:27:51 +08:00
|
|
|
const MonkeyError = require("../handlers/error");
|
|
|
|
const { mongoDB } = require("../init/mongodb");
|
|
|
|
const uuid = require("uuid");
|
|
|
|
|
|
|
|
class PresetDAO {
|
2021-07-09 06:28:00 +08:00
|
|
|
static async getPresets(uid) {
|
|
|
|
const preset = await mongoDB()
|
|
|
|
.collection("presets")
|
|
|
|
.find({ uid })
|
|
|
|
.sort({ timestamp: -1 })
|
|
|
|
.toArray(); // this needs to be changed to later take patreon into consideration
|
|
|
|
return preset;
|
|
|
|
}
|
|
|
|
|
2021-06-09 01:27:51 +08:00
|
|
|
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");
|
2021-07-09 06:28:00 +08:00
|
|
|
const id = uuid.v4();
|
|
|
|
await mongoDB().collection("presets").insertOne({ id, uid, name, config });
|
|
|
|
return {
|
|
|
|
id,
|
|
|
|
name,
|
|
|
|
};
|
2021-06-09 01:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static async editPreset(uid, id, name, config) {
|
|
|
|
const preset = await mongoDB().collection("presets").findOne({ uid, id });
|
|
|
|
if (!preset) throw new MonkeyError(404, "Preset not found");
|
2021-07-09 06:28:00 +08:00
|
|
|
if (config) {
|
|
|
|
return await mongoDB()
|
|
|
|
.collection("presets")
|
|
|
|
.updateOne({ uid, id }, { $set: { name, config } });
|
|
|
|
} else {
|
|
|
|
return await mongoDB()
|
|
|
|
.collection("presets")
|
|
|
|
.updateOne({ uid, id }, { $set: { name } });
|
|
|
|
}
|
2021-06-09 01:27:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static async removePreset(uid, id) {
|
|
|
|
const preset = await mongoDB().collection("presets").findOne({ uid, id });
|
|
|
|
if (!preset) throw new MonkeyError(404, "Preset not found");
|
|
|
|
return await mongoDB().collection("presets").remove({ uid, id });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = PresetDAO;
|