Implemented custom per-channel !toke commands in back-end.
This commit is contained in:
parent
575244ac53
commit
a1fbbea7b7
10 changed files with 159 additions and 29 deletions
|
|
@ -23,10 +23,17 @@ const channelModel = require('../../../schemas/channel/channelSchema');
|
|||
|
||||
module.exports.get = async function(req, res){
|
||||
try{
|
||||
const tokeList = await tokeCommandModel.getCommandStrings();
|
||||
//Pull validated and sanatized input and yoink the channel DB doc
|
||||
const {chanName} = matchedData(req);
|
||||
const chanDB = await channelModel.findOne({name: chanName});
|
||||
|
||||
res.status(200);
|
||||
return res.send(tokeList);
|
||||
//if we found a channel in the DB
|
||||
if(chanDB != null){
|
||||
res.status(200);
|
||||
return res.send(chanDB.tokeCommands);
|
||||
}else{
|
||||
return errorHandler(res, "Channel not found!", "Bad Query");
|
||||
}
|
||||
}catch(err){
|
||||
return exceptionHandler(res, err);
|
||||
}
|
||||
|
|
@ -39,20 +46,29 @@ module.exports.post = async function(req, res){
|
|||
|
||||
//if they're empty
|
||||
if(validResult.isEmpty()){
|
||||
/*
|
||||
const {command} = matchedData(req);
|
||||
const tokeDB = await tokeCommandModel.findOne({command});
|
||||
//Pull sanatized/validated input and yoink the chan doc
|
||||
const {chanName, command} = matchedData(req);
|
||||
const chanDB = await channelModel.findOne({name: chanName});
|
||||
|
||||
if(tokeDB != null){
|
||||
return errorHandler(res, `Toke command '!${command}' already exists!`);
|
||||
//Check that the channel exists
|
||||
if(chanDB == null){
|
||||
return errorHandler(res, "Channel not found!", "Bad Query");
|
||||
}
|
||||
|
||||
//Check for duplicate toke commands
|
||||
if(chanDB.tokeCommands.indexOf(command) != -1){
|
||||
return errorHandler(res, "Cannot add duplicate toke command.");
|
||||
}
|
||||
|
||||
//Add the toke
|
||||
await tokeCommandModel.create({command});
|
||||
chanDB.tokeCommands.push(command);
|
||||
|
||||
//Save the channel document
|
||||
await chanDB.save();
|
||||
|
||||
//Return the updated command list
|
||||
res.status(200);
|
||||
return res.send(await tokeCommandModel.getCommandStrings());*/
|
||||
return res.send(chanDB.tokeCommands);
|
||||
}else{
|
||||
//otherwise scream
|
||||
res.status(400);
|
||||
|
|
@ -70,19 +86,32 @@ module.exports.delete = async function(req, res){
|
|||
|
||||
//if they're empty
|
||||
if(validResult.isEmpty()){
|
||||
/*
|
||||
const {command} = matchedData(req);
|
||||
const tokeDB = await tokeCommandModel.findOne({command});
|
||||
//Pull sanatized/validated input and yoink the chan doc
|
||||
const {chanName, command} = matchedData(req);
|
||||
const chanDB = await channelModel.findOne({name: chanName});
|
||||
|
||||
if(tokeDB == null){
|
||||
return errorHandler(res, `Cannot delete non-existant toke command '!${command}'!`);
|
||||
//Check that the channel exists
|
||||
if(chanDB == null){
|
||||
return errorHandler(res, "Channel not found!", "Bad Query");
|
||||
}
|
||||
|
||||
await tokeDB.deleteOne();
|
||||
//get index of the requested toke to delete
|
||||
const tokeIndex = chanDB.tokeCommands.indexOf(command);
|
||||
|
||||
//Make sure the toke exists
|
||||
if(tokeIndex == -1){
|
||||
return errorHandler(res, "Cannot delete non-existant toke command.");
|
||||
}
|
||||
|
||||
//splice out the requested toke command
|
||||
chanDB.tokeCommands.splice(tokeIndex,1);
|
||||
|
||||
//Save the channel document
|
||||
await chanDB.save();
|
||||
|
||||
//Return the updated command list
|
||||
res.status(200);
|
||||
return res.send(await tokeCommandModel.getCommandStrings());*/
|
||||
return res.send(chanDB.tokeCommands);
|
||||
}else{
|
||||
//otherwise scream
|
||||
res.status(400);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue