63 lines
1.8 KiB
JavaScript
63 lines
1.8 KiB
JavaScript
const ApiError = require('../errorClass');
|
|
const config = require('../../../config-handler');
|
|
|
|
async function isAuthorExists (authorId) {
|
|
if (!authorId) return false;
|
|
return (await global.database.authors.promiseMode().get({ where : { id : authorId } })).length !== 0;
|
|
}
|
|
|
|
async function checkSyntaxArgs (req, res) {
|
|
return !!(
|
|
( // Проверка поля type.
|
|
['author', 'muzic'].indexOf(req.json?.type) !== -1
|
|
) &&
|
|
( // Проверка поля id.
|
|
Number.isInteger(req.json.id) && req.json.type === 'author' ? await isAuthorExists(req.json.id) : true
|
|
)
|
|
)
|
|
}
|
|
|
|
module.exports = async (req, res) => {
|
|
if (req.json === undefined) {
|
|
// console.log(req.headers);
|
|
throw new ApiError("METHOD_MUST_BE_POST_JSON", {
|
|
request_method : req.method,
|
|
'content-type' : !req.headers['content-type'] ? null : req.headers['content-type']
|
|
});
|
|
}
|
|
if (!await checkSyntaxArgs(req, res)) {
|
|
throw new ApiError("INVALID_OR_UNSYNTAX_PARAMS", {
|
|
request_method : req.method,
|
|
params : {
|
|
type : req.json?.type === undefined ? null : req.json.type,
|
|
name : req.json?.name === undefined ? null : req.json.name,
|
|
...(req.json?.type === 'muzic' ? ({
|
|
author_id : req.json?.author_id === undefined ? null : req.json?.author_id,
|
|
data : req.json?.data === undefined ? null : req.json.data,
|
|
}) : ({}))
|
|
}
|
|
});
|
|
}
|
|
if (req.json.type === 'author') {
|
|
// Удаляем всю музыку этого исполнителя
|
|
await global.database.muzic.promiseMode().remove({
|
|
where: {
|
|
author_id: req.json.id
|
|
}
|
|
});
|
|
// Удаляем исполнителя
|
|
await global.database.authors.promiseMode().remove({
|
|
where: {
|
|
id: req.json.id
|
|
}
|
|
});
|
|
}
|
|
else {
|
|
await global.database.muzic.promiseMode().remove({
|
|
where: {
|
|
id: req.json.id
|
|
}
|
|
});
|
|
}
|
|
return 'ok';
|
|
} |