mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 07:25:21 +02:00
remove deeply
This commit is contained in:
parent
d6209f8578
commit
97077b1b30
6 changed files with 89 additions and 3 deletions
|
@ -21,6 +21,14 @@ class Aliases {
|
|||
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param id
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async removeById(id) {
|
||||
return Alias.destroyByEntityId(id);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Aliases;
|
||||
|
|
|
@ -90,6 +90,20 @@ class PagesOrder {
|
|||
pageOrder.putAbove(currentPageId, putAbovePageId);
|
||||
await pageOrder.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param parentId
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async remove(parentId) {
|
||||
const order = await Model.get(parentId);
|
||||
|
||||
if (!order._id) {
|
||||
throw new Error('Page with given id does not contain order');
|
||||
}
|
||||
|
||||
return order.destroy();
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = PagesOrder;
|
||||
|
|
|
@ -47,6 +47,18 @@ class Alias {
|
|||
return new Alias(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} id
|
||||
* @returns {Promise<void>}
|
||||
*/
|
||||
static async destroyByEntityId(id) {
|
||||
await aliasesDb.remove({id});
|
||||
|
||||
delete this._id;
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @constructor
|
||||
*
|
||||
|
|
|
@ -18,6 +18,7 @@ class PageOrder {
|
|||
* Returns current Page's children order
|
||||
*
|
||||
* @param {string} pageId - page's id
|
||||
* @returns {PageOrder}
|
||||
*/
|
||||
static async get(pageId) {
|
||||
const order = await db.findOne({page: pageId});
|
||||
|
|
|
@ -3,7 +3,7 @@ const router = express.Router();
|
|||
const multer = require('multer')();
|
||||
const Pages = require('../../controllers/pages');
|
||||
const PagesOrder = require('../../controllers/pagesOrder');
|
||||
|
||||
const Aliases = require("../../controllers/aliases");
|
||||
/**
|
||||
* GET /page/:id
|
||||
*
|
||||
|
@ -126,15 +126,39 @@ router.delete('/page/:id', async (req, res) => {
|
|||
responsePage = page._parent !== "0" ? await Pages.get(page._parent) : null;
|
||||
}
|
||||
|
||||
// remove current page and return previous from order
|
||||
// remove current page and go deeper to remove children with orders
|
||||
async function deleteRecursively(startFrom) {
|
||||
let order = [];
|
||||
try {
|
||||
const children = await PagesOrder.get(startFrom);
|
||||
order = children.order;
|
||||
} catch (e) {}
|
||||
|
||||
order.forEach(async id => {
|
||||
await deleteRecursively(id);
|
||||
});
|
||||
|
||||
try {
|
||||
await Pages.remove(startFrom);
|
||||
await PagesOrder.remove(startFrom);
|
||||
await Aliases.removeById(startFrom);
|
||||
} catch (e) {}
|
||||
|
||||
}
|
||||
|
||||
await deleteRecursively(req.params.id);
|
||||
|
||||
// remove also from parent's order
|
||||
parentPageOrder.remove(req.params.id);
|
||||
await [Pages.remove(req.params.id), parentPageOrder.save()];
|
||||
await Aliases.removeById(req.params.id);
|
||||
await parentPageOrder.save();
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
result: responsePage
|
||||
});
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
error: err.message
|
||||
|
|
|
@ -352,4 +352,31 @@ describe('Page model', () => {
|
|||
|
||||
expect(page.title).to.equal(pageData.body.blocks[0].data.text);
|
||||
});
|
||||
|
||||
it('test deletion', async () => {
|
||||
|
||||
const pages = ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9'];
|
||||
const orders = {
|
||||
'0' : ['1', '2', '3'],
|
||||
'1' : ['4', '5'],
|
||||
'5' : ['6', '7', '8'],
|
||||
'3' : ['9']
|
||||
};
|
||||
|
||||
function deleteRecursively(startFrom) {
|
||||
const order = orders[startFrom];
|
||||
if (!order) {
|
||||
const found = pages.indexOf(startFrom);
|
||||
pages.splice(found, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
order.forEach(id => {
|
||||
deleteRecursively(id);
|
||||
});
|
||||
|
||||
const found = pages.indexOf(startFrom);
|
||||
pages.splice(found, 1);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue