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

Add POST/PUT/DELETE endpoints for Recipes data #95

Merged
merged 1 commit into from
Feb 16, 2025
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
102 changes: 102 additions & 0 deletions src/controllers/recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,106 @@ controller.getRecipesByMealType = ({ mealType, ..._options }) => {
return result;
};

controller.addNewRecipe = ({ ...data }) => {
const {
name,
ingredients,
instructions,
prepTimeMinutes,
cookTimeMinutes,
servings,
difficulty,
cuisine,
caloriesPerServing,
tags,
userId,
image,
rating,
reviewCount,
mealType,
} = data;

const newRecipe = {
id: frozenData.recipes.length + 1, // Assuming `frozenData.recipes` holds the recipe data
name,
ingredients,
instructions,
prepTimeMinutes,
cookTimeMinutes,
servings,
difficulty,
cuisine,
caloriesPerServing,
tags,
userId,
image,
rating,
reviewCount,
mealType,
};

return newRecipe;
};

controller.updateRecipeById = ({ id, ...data }) => {
const {
name,
ingredients,
instructions,
prepTimeMinutes,
cookTimeMinutes,
servings,
difficulty,
cuisine,
caloriesPerServing,
tags,
userId,
image,
rating,
reviewCount,
mealType,
} = data;

const recipeFrozen = frozenData.recipes.find(r => r.id.toString() === id);

if (!recipeFrozen) {
throw new APIError(`Recipe with id '${id}' not found`, 404);
}

const updatedRecipe = {
id: +id, // converting id to number
name: name || recipeFrozen.name,
ingredients: ingredients || recipeFrozen.ingredients,
instructions: instructions || recipeFrozen.instructions,
prepTimeMinutes: prepTimeMinutes || recipeFrozen.prepTimeMinutes,
cookTimeMinutes: cookTimeMinutes || recipeFrozen.cookTimeMinutes,
servings: servings || recipeFrozen.servings,
difficulty: difficulty || recipeFrozen.difficulty,
cuisine: cuisine || recipeFrozen.cuisine,
caloriesPerServing: caloriesPerServing || recipeFrozen.caloriesPerServing,
tags: tags || recipeFrozen.tags,
userId: userId || recipeFrozen.userId,
image: image || recipeFrozen.image,
rating: rating || recipeFrozen.rating,
reviewCount: reviewCount || recipeFrozen.reviewCount,
mealType: mealType || recipeFrozen.mealType,
};

return updatedRecipe;
};

controller.deleteRecipeById = ({ id }) => {
const recipeFrozen = frozenData.recipes.find(p => p.id.toString() === id);

if (!recipeFrozen) {
throw new APIError(`Recipe with id '${id}' not found`, 404);
}

return {
...recipeFrozen,
isDeleted: true,
deletedOn: new Date().toISOString(),
};
};

module.exports = controller;
25 changes: 25 additions & 0 deletions src/routes/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ const {
getRecipeTags,
getRecipesByTag,
getRecipesByMealType,
addNewRecipe,
updateRecipeById,
deleteRecipeById,
} = require('../controllers/recipes');

// get all recipes
Expand Down Expand Up @@ -45,4 +48,26 @@ router.get('/meal-type/:mealType', (req, res) => {
res.send(getRecipesByMealType({ mealType, ...req._options }));
});

router.post('/add', (req, res) => {
res.send(addNewRecipe({ ...req.body }));
});

router.put('/:id', (req, res) => {
const { id } = req.params;

res.send(updateRecipeById({ id, ...req.body }));
});

router.patch('/:id', (req, res) => {
const { id } = req.params;

res.send(updateRecipeById({ id, ...req.body }));
});

router.delete('/:id', (req, res) => {
const { id } = req.params;

res.send(deleteRecipeById({ id }));
});

module.exports = router;
2 changes: 1 addition & 1 deletion views/docs-products.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -489,4 +489,4 @@
</script>
</body>

</html>
</html>
94 changes: 93 additions & 1 deletion views/docs-recipes.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,98 @@
}
</code></pre>
</div>

<div class="resource" id="recipes-add">
<a href="#recipes-add" class="res-title">Add Recipe</a>
<p class="res-tip">
Adding a recipe will not add it into the server.
<br />
It will simulate a POST request and will return a new created recipe with a new id
</p>
<pre><code class="language-js">
fetch('https://dummyjson.com/recipes/add', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Tasty Pizza'
/* other recipe data */
})
})
.then(res => res.json())
.then(console.log);
</code></pre>

<button class="show-output btn">Show Output</button>

<pre class="output"><code class="language-json">
{
"id": 51,
"name": "Tasty Pizza",
{...}
}
</code></pre>
</div>

<div class="resource" id="recipes-update">
<a href="#recipes-update" class="res-title">Update Recipe</a>
<p class="res-tip">
Updating a recipe will not update it into the server.
<br />
It will simulate a PUT/PATCH request and will return updated recipe with modified data
</p>
<pre><code class="language-js">
/* updating name of recipe with id 1 */
fetch('https://dummyjson.com/recipes/1', {
method: 'PUT', /* or PATCH */
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
name: 'Tasty Pizza'
})
})
.then(res => res.json())
.then(console.log);
</code></pre>

<button class="show-output btn">Show Output</button>

<pre class="output"><code class="language-json">
{
"id": 1,
"name": "Tasty Pizza",
{...}
}
</code></pre>
</div>

<div class="resource" id="recipes-delete">
<a href="#recipes-delete" class="res-title">Delete Recipe</a>
<p class="res-tip">
Deleting a recipe will not delete it into the server.
<br />
It will simulate a DELETE request and will return deleted recipe with `isDeleted` & `deletedOn` keys
</p>
<pre><code class="language-js">
/* deleting recipe with id 1 */
fetch('https://dummyjson.com/recipes/1', {
method: 'DELETE'
})
.then(res => res.json())
.then(console.log);
</code></pre>

<button class="show-output btn">Show Output</button>

<pre class="output"><code class="language-json">
{
"id": 1,
{...}
"isDeleted": true,
"deletedOn": /* ISOTime */
}
</code></pre>
</div>


</div>
</section>

Expand All @@ -400,4 +492,4 @@
</script>
</body>

</html>
</html>
20 changes: 19 additions & 1 deletion views/partials/docs-side-nav.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,24 @@
Get recipes by meal
</a>
</li>

<li>
<a href="/docs/recipes/#recipes-add">
Add a recipe
</a>
</li>

<li>
<a href="/docs/recipes/#recipes-update">
Update a recipes
</a>
</li>

<li>
<a href="/docs/recipes/#recipes-delete">
Delete a recipe
</a>
</li>
</ul>
</li>

Expand Down Expand Up @@ -662,4 +680,4 @@
</ul>
</li>
</ul>
</nav>
</nav>