canopy/src/controllers/api/channel/tokeCommandController.js

123 lines
4.1 KiB
JavaScript

/*Canopy - The next generation of stoner streaming software
Copyright (C) 2024-2025 Rainbownapkin and the TTN Community
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.*/
//npm imports
const {validationResult, matchedData} = require('express-validator');
//local imports
const {exceptionHandler, errorHandler} = require('../../../utils/loggerUtils');
const channelModel = require('../../../schemas/channel/channelSchema');
module.exports.get = async function(req, res){
try{
//Pull validated and sanatized input and yoink the channel DB doc
const {chanName} = matchedData(req);
const chanDB = await channelModel.findOne({name: chanName});
//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);
}
}
module.exports.post = async function(req, res){
try{
//get validation error results
const validResult = validationResult(req);
//if they're empty
if(validResult.isEmpty()){
//Pull sanatized/validated input and yoink the chan doc
const {chanName, command} = matchedData(req);
const chanDB = await channelModel.findOne({name: chanName});
//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
chanDB.tokeCommands.push(command);
//Save the channel document
await chanDB.save();
//Return the updated command list
res.status(200);
return res.send(chanDB.tokeCommands);
}else{
//otherwise scream
res.status(400);
return res.send({errors: validResult.array()})
}
}catch(err){
return exceptionHandler(res, err);
}
}
module.exports.delete = async function(req, res){
try{
//get validation error results
const validResult = validationResult(req);
//if they're empty
if(validResult.isEmpty()){
//Pull sanatized/validated input and yoink the chan doc
const {chanName, command} = matchedData(req);
const chanDB = await channelModel.findOne({name: chanName});
//Check that the channel exists
if(chanDB == null){
return errorHandler(res, "Channel not found!", "Bad Query");
}
//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(chanDB.tokeCommands);
}else{
//otherwise scream
res.status(400);
return res.send({errors: validResult.array()})
}
}catch(err){
return exceptionHandler(res, err);
}
}