Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DELETE /:cookerId/dishId ~ Done! without test #60

Merged
merged 4 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion src/controllers/cooker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,45 @@ const createDish = async (req: Request, res: Response) => {
}
};

const deleteDish = async (req: Request, res: Response) => {
try {
const { cookerId, dishId } = req.params;

const user = await User.findById(cookerId);

if (!user) {
return res.status(404).json({
message: 'User not found',
});
}

if (user.role !== 'cooker') {
return res.status(403).json({
message: 'User is not a cooker',
});
}

const dish = await Food.findOne({ _id: dishId, user_id: cookerId });

if (!dish) {
return res.status(404).json({
message: 'Dish not found',
});
}

await Food.deleteOne({ _id: dishId });

res.status(200).json({
message: 'Dish deleted successfully',
});
} catch (err) {
res.status(500).json({
message: 'An error occurred while deleting the dish',

});
}
};

const getDishes = async (req: Request, res: Response) => {
try {
const { cookerId } = req.params;
Expand All @@ -66,4 +105,5 @@ const getDishes = async (req: Request, res: Response) => {
};

// eslint-disable-next-line node/no-unsupported-features/es-syntax
export { createDish, getDishes };

export { createDish, getDishes, deleteDish };
61 changes: 61 additions & 0 deletions src/docs/cooker.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,64 @@ paths:
example: An error occurred while retrieving the dishes
error:
type: object
/api/cooker/{cookerId}/{dishId}:
delete:
tags:
- Cooker
summary: Delete a specific dish associated with a specific cooker by ID
parameters:
- in: path
name: cookerId
required: true
schema:
type: string
description: The ID of the cooker to delete the dish for
- in: path
name: dishId
required: true
schema:
type: string
description: The ID of the dish to delete
responses:
'200':
description: Dish deleted successfully
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Dish deleted successfully
'403':
description: User is not a cooker
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: User is not a cooker
'404':
description: Dish not found or user not found
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: Dish not found or user not found
'500':
description: An error occurred while deleting the dish.
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: An error occurred while deleting the dish.
error:
type: object
3 changes: 2 additions & 1 deletion src/routes/cookers.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// eslint-disable-next-line node/no-unsupported-features/es-syntax
import express from 'express';
// eslint-disable-next-line node/no-unsupported-features/es-syntax
import { createDish, getDishes } from '../controllers/cooker';
import { createDish, getDishes, deleteDish } from '../controllers/cooker';

const cookerRouter = express.Router();

cookerRouter.post('/:cookerId/dish', createDish);
cookerRouter.delete('/:cookerId/:dishId', deleteDish);
cookerRouter.get('/:cookerId/dishes', getDishes);

// eslint-disable-next-line node/no-unsupported-features/es-syntax
Expand Down
Loading