ZettelKasten part 7
Deletion
This is a followup to a previous article, ZettelKasten Part 6
Delet this
Look who forgot to add a DELETE
action to their API. Me.
Let's do this.
I'm going to make it a POST
to /:id/delete
.
.post(['/', '/:id', '/:id/delete'], writeTransactionMiddleware, koaBody())
It needs the write transaction middleware and koaBody() like the others that make modifications
And then it also needs to get the zettel:
.post('/:id/delete', getZettelMiddleware, async (ctx: Context) => {
And then we delete it.
const transaction = ctx.graknTransaction;
const zettel = ctx.zettel.asRemote(transaction);
const serializedZettel = await serializeZettel(zettel, transaction)
await zettel.delete();
const success = await zettel.isDeleted();
if (success) {
ctx.body = {
deleted: serializedZettel
}
} else {
ctx.body = {
error: "Could not delete zettel",
zettel: serializeZettel(zettel, transaction)
}
ctx.status = 500;
}
Make sure you capture what it was before you delete it!
The adventure continues in ZettelKasten Part 8