added users and groups
This commit is contained in:
51
src/middlewares/existance.js
Normal file
51
src/middlewares/existance.js
Normal file
@@ -0,0 +1,51 @@
|
||||
import UserService from '../services/user.js';
|
||||
import GroupService from '../services/group.js';
|
||||
import log from '../utils/log.js';
|
||||
import statuses from '../utils/status.js';
|
||||
|
||||
const TAG = "/middlewares/existance.js"
|
||||
|
||||
const usernameExists = async (req, res, next) => {
|
||||
try {
|
||||
let { username } = req.body;
|
||||
|
||||
let user = await UserService.getByUsername(username);
|
||||
if (!user || user == statuses.not_found) return res.status(404).send("User not found");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameExists: ${e}`)); }
|
||||
};
|
||||
|
||||
const usernameDoesntExist = async (req, res, next) => {
|
||||
try {
|
||||
let { username } = req.body;
|
||||
|
||||
let user = await UserService.getByUsername(username);
|
||||
if (user || user != statuses.not_found) return res.status(400).send("Such username already taken");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/usernameDoesntExist: ${e}`)); }
|
||||
}
|
||||
|
||||
const groupExists = async (req, res, next) => {
|
||||
try {
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
let group = await GroupService.getById(id);
|
||||
|
||||
if (!group || group == statuses.not_found) return res.status(404).send("Group not found");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupExists: ${e}`)) }
|
||||
}
|
||||
|
||||
const groupDoesntExist = async (req, res, next) => {
|
||||
try {
|
||||
|
||||
const { id } = req.params;
|
||||
|
||||
let group = await GroupService.getById(id);
|
||||
|
||||
if (group || group != statuses.not_found) return res.status(400).send("Such group already exists");
|
||||
next();
|
||||
} catch (e) { return res.status(500).send(log.unknownError(`${TAG}/groupDoesntExist: ${e}`)) }
|
||||
}
|
||||
export default { usernameExists, usernameDoesntExist, groupExists, groupDoesntExist }
|
||||
Reference in New Issue
Block a user