renamed presets to preset

This commit is contained in:
Jack 2021-06-08 18:27:51 +01:00
parent 218ccb3d53
commit f2f9201acb
4 changed files with 46 additions and 43 deletions

View file

@ -1,12 +1,15 @@
import PresetsDAO from "../../dao/presetsDAO";
import { isTagPresetNameValid, validateConfig } from "../../handlers/validation";
import PresetDAO from "../../dao/presetDAO";
import {
isTagPresetNameValid,
validateConfig,
} from "../../handlers/validation";
class PresetsController {
class PresetController {
static async addPreset(req, res, next) {
try {
const { name, config } = req.body;
const { uid } = req.decodedToken;
if(!isTagPresetNameValid(name)) next("Invalid preset name.");
if (!isTagPresetNameValid(name)) next("Invalid preset name.");
validateConfig(config);
const createdInfo = await PresetsDAO.addPreset(uid, name, config);
return res.sendStatus(200).json(createdInfo);
@ -19,7 +22,7 @@ class PresetsController {
try {
const { id, name, config } = req.body;
const { uid } = req.decodedToken;
if(!isTagPresetNameValid(name)) next("Invalid preset name.");
if (!isTagPresetNameValid(name)) next("Invalid preset name.");
validateConfig(config);
await PresetsDAO.editPreset(uid, id, name, config);
return res.sendStatus(200);
@ -40,4 +43,4 @@ class PresetsController {
}
}
module.exports = PresetsController;
module.exports = PresetController;

View file

@ -1,14 +1,18 @@
import { authenticateRequest } from "../../middlewares/auth";
import PresetsController from "../controllers/presets";
import PresetController from "../controllers/preset";
const { Router } = require("express");
const router = Router();
router.post("/presets/add", authenticateRequest, PresetsController.addPreset);
router.post("/presets/add", authenticateRequest, PresetController.addPreset);
router.post("/presets/edit", authenticateRequest, PresetsController.editPreset);
router.post("/presets/edit", authenticateRequest, PresetController.editPreset);
router.get("/presets/remove", authenticateRequest, PresetsController.removePreset);
router.get(
"/presets/remove",
authenticateRequest,
PresetController.removePreset
);
module.exports = router;

29
backend/dao/presetDAO.js Normal file
View file

@ -0,0 +1,29 @@
const MonkeyError = require("../handlers/error");
const { mongoDB } = require("../init/mongodb");
const uuid = require("uuid");
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");
return await mongoDB()
.collection("presets")
.insertOne({ id: uuid.v4(), uid, name, config });
}
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");
return await mongoDB()
.collection("presets")
.updateOne({ uid, id }, { $set: { name, config } });
}
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;

View file

@ -1,33 +0,0 @@
const MonkeyError = require("../handlers/error");
const { mongoDB } = require("../init/mongodb");
const uuid = require("uuid");
class PresetsDAO {
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");
return await mongoDB()
.collection("presets")
.insertOne({ id: uuid.v4(), uid, name, config });
}
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");
return await mongoDB()
.collection("presets")
.updateOne({ uid, id }, { $set: { name , config} });
}
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 = PresetsDAO;