mirror of
https://github.com/pawelmalak/snippet-box.git
synced 2025-12-23 22:18:06 +01:00
Split snippet controllers into separate files
This commit is contained in:
50
src/controllers/snippets/deleteSnippet.ts
Normal file
50
src/controllers/snippets/deleteSnippet.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { Response, NextFunction } from 'express';
|
||||
import { asyncWrapper } from '../../middleware';
|
||||
import { SnippetModel, Snippet_TagModel } from '../../models';
|
||||
import { Snippet, UserInfoRequest } from '../../typescript/interfaces';
|
||||
import { tagParser, createTags, ErrorResponse } from '../../utils';
|
||||
|
||||
interface Params {
|
||||
id: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Delete snippet
|
||||
* @route /api/snippets/:id
|
||||
* @request DELETE
|
||||
* @access Private
|
||||
*/
|
||||
export const deleteSnippet = asyncWrapper(
|
||||
async (
|
||||
req: UserInfoRequest<{}, Params>,
|
||||
res: Response,
|
||||
next: NextFunction
|
||||
): Promise<void> => {
|
||||
const snippet = await SnippetModel.findOne({
|
||||
where: { id: req.params.id }
|
||||
});
|
||||
|
||||
if (!snippet) {
|
||||
return next(
|
||||
new ErrorResponse(
|
||||
404,
|
||||
`Snippet with the id of ${req.params.id} was not found`
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
if (snippet.createdBy != req.user.id && !req.user.isAdmin) {
|
||||
return next(
|
||||
new ErrorResponse(401, `You are not authorized to modify this resource`)
|
||||
);
|
||||
}
|
||||
|
||||
// Delete all snippet <> tag relations
|
||||
await Snippet_TagModel.destroy({ where: { snippet_id: req.params.id } });
|
||||
await snippet.destroy();
|
||||
|
||||
res.status(200).json({
|
||||
data: {}
|
||||
});
|
||||
}
|
||||
);
|
||||
Reference in New Issue
Block a user