Added deletion of documents

This commit is contained in:
Jori Lallo
2017-06-05 00:36:50 -07:00
parent a4dca58ae7
commit 1be0ff8105
3 changed files with 34 additions and 26 deletions

View File

@ -136,20 +136,15 @@ router.post('documents.create', auth(), async ctx => {
text,
});
// TODO: Move to afterSave hook if possible with imports
if (parentDocument && ownerCollection.type === 'atlas') {
ownerCollection.addDocument(
newDocument,
newDocument.parentDocumentId,
index || -1
);
await ownerCollection.save();
if (ownerCollection.type === 'atlas') {
await ownerCollection.addDocumentToStructure(newDocument, index);
}
ctx.body = {
data: await presentDocument(ctx, newDocument, {
includeCollection: true,
includeCollaborators: true,
collection: ownerCollection,
}),
};
});
@ -197,14 +192,15 @@ router.post('documents.delete', auth(), async ctx => {
if (collection.type === 'atlas') {
// Don't allow deletion of root docs
if (!document.parentDocumentId) {
throw httpErrors.BadRequest("Unable to delete atlas's root document");
if (collection.documentStructure.length === 1) {
throw httpErrors.BadRequest(
"Unable to delete collection's only document"
);
}
// Delete all chilren
try {
await collection.deleteDocument(document);
await collection.save();
} catch (e) {
throw httpErrors.BadRequest('Error while deleting');
}