mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-03-12 23:20:25 +08:00
renamed presets to preset
This commit is contained in:
parent
218ccb3d53
commit
f2f9201acb
4 changed files with 46 additions and 43 deletions
|
@ -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;
|
|
@ -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
29
backend/dao/presetDAO.js
Normal 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;
|
|
@ -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;
|
Loading…
Reference in a new issue