mirror of
https://github.com/monkeytypegame/monkeytype.git
synced 2025-10-27 09:16:13 +08:00
added delete user option, playing around with the return codes and formats
This commit is contained in:
parent
978bb753cd
commit
32a2a73915
3 changed files with 23 additions and 5 deletions
|
|
@ -18,10 +18,20 @@ class UserController {
|
|||
}
|
||||
}
|
||||
|
||||
static async deleteUser(req, res, next) {
|
||||
try {
|
||||
const { uid } = req.body;
|
||||
await UsersDAO.deleteUser(uid);
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
}
|
||||
}
|
||||
|
||||
static async updateName(req, res, next) {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
if (!isUsernameValid(name)) return next("Username invalid!");
|
||||
if (!isUsernameValid(name)) return res.status(400).json({message:"Username invalid. Name cannot contain special characters or contain more than 14 characters. Can include _ . and -"});
|
||||
await UsersDAO.updateName();
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
|
|
@ -32,9 +42,9 @@ class UserController {
|
|||
static async checkName(req, res, next) {
|
||||
try {
|
||||
const { name } = req.body;
|
||||
if (!isUsernameValid(name)) return next("Username invalid");
|
||||
if (!isUsernameValid(name)) return next({status: 400, message: "Username invalid. Name cannot contain special characters or contain more than 14 characters. Can include _ . and -"});
|
||||
const available = await UsersDAO.isNameAvailable(name);
|
||||
if(!available) return next("Username unavailable");
|
||||
if(!available) return res.status(400).json({message:"Username unavailable"});
|
||||
return res.sendStatus(200);
|
||||
} catch (e) {
|
||||
return next(e);
|
||||
|
|
|
|||
|
|
@ -5,9 +5,11 @@ const UserController = require("../controllers/user");
|
|||
|
||||
const router = Router();
|
||||
|
||||
router.post("/user/signup", UserController.createNewUser);
|
||||
router.post("/signup", UserController.createNewUser);
|
||||
|
||||
router.post("/user/checkName", authenticateRequest, UserController.checkName);
|
||||
router.post("/checkName", UserController.checkName);
|
||||
|
||||
router.post("/delete", UserController.deleteUser);
|
||||
|
||||
router.post("/user/updateName", authenticateRequest, UserController.updateName);
|
||||
|
||||
|
|
|
|||
|
|
@ -10,6 +10,12 @@ class UsersDAO {
|
|||
.insertOne({ name, email, uid, addedAt: Date.now() });
|
||||
}
|
||||
|
||||
static async deleteUser(uid) {
|
||||
return await mongoDB()
|
||||
.collection("users")
|
||||
.deleteOne({uid});
|
||||
}
|
||||
|
||||
static async updateName(uid, name) {
|
||||
const nameDoc = await mongoDB()
|
||||
.collection("users")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue