From 54f1a05eea753212da7b80a95407f1f720d56b9e Mon Sep 17 00:00:00 2001 From: Matilda-Njau Date: Wed, 8 Jan 2025 12:42:55 +0300 Subject: [PATCH 1/3] modify db-2 assignment --- .../controllers/recipes-controllers.js | 150 +++++++++++------- code/db-2-assignment/models/Recipe.js | 6 +- code/db-2-assignment/routes/recipes.js | 23 +-- 3 files changed, 114 insertions(+), 65 deletions(-) diff --git a/code/db-2-assignment/controllers/recipes-controllers.js b/code/db-2-assignment/controllers/recipes-controllers.js index 91defc5c..66e14876 100644 --- a/code/db-2-assignment/controllers/recipes-controllers.js +++ b/code/db-2-assignment/controllers/recipes-controllers.js @@ -1,79 +1,119 @@ import { mongoose } from "../db.js"; -import recipeSchema from "../models/Recipe.js"; +import recipeModel from "../models/Recipe.js"; const getAllRecipes = async (req, res) => { - const getAllRecipes = await recipeSchema.find() - res.json(getAllRecipes) -} + const getAllRecipes = await recipeModel.find(); + res.json(getAllRecipes); +}; const getRecipeById = async (req, res) => { + try { // check if the recipeId is a valid db id if (mongoose.Types.ObjectId.isValid(req.params.recipeId)) { - // if recipeId is a valid db id, check if it matches the id from our db - const findOneRecipe = await recipeSchema.findOne({ _id: req.params.recipeId}) - // if yes, send - if (findOneRecipe) { - res.json(findOneRecipe) - // if no, send a non-existent error message - } else { - res.status(404).send("That recipe ID does not exist in our database") - } - // if recipeId is not a valid db id, send error message + // if recipeId is a valid db id, check if it matches the id from our db + const findOneRecipe = await recipeModel.findOne({ + _id: req.params.recipeId, + }); + // if yes, send + if (findOneRecipe) { + res.json(findOneRecipe); + // if no, send a non-existent error message + } else { + res.status(404).send("That recipe ID does not exist in our database"); + } + // if recipeId is not a valid db id, send error message } else { - res.status(404).send("That is an invalid database ID") + res.status(400).send("That is an invalid database ID"); } -} + } catch (error) { + console.log(error); + res.status(500).json({ message: error.message }); + } +}; const createRecipe = async (req, res) => { - try { - const addRecipe = new recipeSchema(req.body) - await addRecipe.save() - res.status(201).send(`A new recipe titled ${req.body.title} has been added to the database`) - } catch (error) { - console.log(error); - res.status(422).json({message: error.message}) - - } -} + try { + const addRecipe = new recipeModel(req.body); + await addRecipe.save(); + res + .status(201) + .send( + `A new recipe titled ${req.body.title} has been added to the database` + ); + } catch (error) { + console.log(error); + res.status(500).json({ message: error.message }); + } +}; const updateRecipeById = async (req, res) => { + try { // check if the recipeId is a valid db id if (mongoose.Types.ObjectId.isValid(req.params.recipeId)) { - // if recipeId is a valid db id, check if it is an id from our db, change the body of that db ID, then update - const updateRecipe = await recipeSchema.updateOne({ _id: req.params.recipeId}, req.body, {upsert: true}) - // if ID matches, update - if (updateRecipe.modifiedCount === 1) { - res.status(200).send("That recipe has been updated successfully") - // if that id doesn't exist in our db, create a new recipe with that new body---that's what upsert does - } else if (updateRecipe.upsertedCount === 1) { - res.status(201).send("That ID does not exist in our database, so we created a new recipe with that ID") - // THIS CONDITION CREATED MY NEW RECIPE EVEN THOUGH IT DIDN'T MEET MY SCHEMA STANDARD/RATHER WITHOUT THE OTHER 'REQUIRED' ELEMENTS IN MY SCHEMA DEFINITION, HOW COMES? THIS ALSO MEANS MY ELSE WILL NEVER BE EXECUTED BECAUSE A NEW RECIPE WILL ALWAYS BE CREATED EVEN THOUGH IT DOESN'T MEET MY STANDARD + // if recipeId is a valid db id, check if it is an id from our db, change the body of that db ID, then update + const updateRecipe = await recipeModel.updateOne( + { _id: req.params.recipeId }, + req.body, + { upsert: true } + ); + // if ID matches, update + if (updateRecipe.modifiedCount === 1) { + res.status(200).send("That recipe has been updated successfully"); + // if that id doesn't exist in our db, create a new recipe with that new body---that's what upsert does + } else if (updateRecipe.upsertedCount === 1) { + res + .status(201) + .send( + "That ID does not exist in our database, so we created a new recipe with that ID" + ); - // if the previous doesn't match, maybe because it doesn't meet our schema standard - } else { - res.status(500).send("We couldn't update your recipe") - } - // if recipeId is not a valid db id, send error message + // if the previous doesn't match, maybe because it doesn't meet our schema standard + } else { + res.status(422).send("We couldn't update your recipe"); + } + // if recipeId is not a valid db id, send error message } else { - res.status(404).send("That is an invalid database ID") + res.status(400).send("That is an invalid database ID"); } -} + } catch (error) { + console.log(error); + res.status(500).json({ message: error.message }); + } +}; const deleteRecipeById = async (req, res) => { + try { // check if the recipeId is a valid db id if (mongoose.Types.ObjectId.isValid(req.params.recipeId)) { - // if recipeId is a valid db id, check if it matches the id from our db and delete - const deleteRecipe = await recipeSchema.deleteOne({ _id: req.params.recipeId}) - // if the removal was a success, send a success message - if (deleteRecipe.deletedCount === 1) { - res.status(200).send("That recipe has been removed from our database") - // if the id was valid but doesn't exist in our db - } else { - res.status(422).send("We couldn't delete anything, that ID doesn't exist in our database") - } - // if the ID is an invalid db ID, send error message + // if recipeId is a valid db id, check if it matches the id from our db and delete + const deleteRecipe = await recipeModel.deleteOne({ + _id: req.params.recipeId, + }); + // if the removal was a success, send a success message + if (deleteRecipe.deletedCount === 1) { + res.status(200).send("That recipe has been removed from our database"); + // if the id was valid but doesn't exist in our db + } else { + res + .status(422) + .send( + "We couldn't delete anything, that ID doesn't exist in our database" + ); + } + // if the ID is an invalid db ID, send error message } else { - res.status(404).send("That is an invalid database ID") + res.status(400).send("That is an invalid database ID"); } -} -export { getAllRecipes, getRecipeById, createRecipe, updateRecipeById, deleteRecipeById } \ No newline at end of file + } catch (error) { + console.log(error); + res.status(500).json({ message: error.message }); + } +}; + +export { + getAllRecipes, + getRecipeById, + createRecipe, + updateRecipeById, + deleteRecipeById, +}; diff --git a/code/db-2-assignment/models/Recipe.js b/code/db-2-assignment/models/Recipe.js index 9a293ac6..761d7326 100644 --- a/code/db-2-assignment/models/Recipe.js +++ b/code/db-2-assignment/models/Recipe.js @@ -25,4 +25,8 @@ const recipeSchema = new mongoose.Schema({ { timestamps: true } ) -export default mongoose.model("recipe", recipeSchema) \ No newline at end of file +export default mongoose.model("recipe", recipeSchema) +// recipe represents the name of the model in my default +// model export and it can be named anything on import +// it all represents my db collection which is pluralized +// by default \ No newline at end of file diff --git a/code/db-2-assignment/routes/recipes.js b/code/db-2-assignment/routes/recipes.js index 335e0490..4951d1a8 100644 --- a/code/db-2-assignment/routes/recipes.js +++ b/code/db-2-assignment/routes/recipes.js @@ -1,17 +1,22 @@ import express from "express"; -const router = express.Router() +const router = express.Router(); -import { getAllRecipes, getRecipeById, createRecipe, updateRecipeById, deleteRecipeById } from "../controllers/recipes-controllers.js" +import { + getAllRecipes, + getRecipeById, + createRecipe, + updateRecipeById, + deleteRecipeById, +} from "../controllers/recipes-controllers.js"; -router.get("/", getAllRecipes) +router.get("/", getAllRecipes); -router.get("/retrieve/:recipeId", getRecipeById) +router.get("/retrieve/:recipeId", getRecipeById); -router.post("/", createRecipe) +router.post("/", createRecipe); -router.put("/update/:recipeId", updateRecipeById) +router.put("/update/:recipeId", updateRecipeById); -router.delete("/delete/:recipeId", deleteRecipeById) +router.delete("/delete/:recipeId", deleteRecipeById); - -export default router +export default router; From 23c243105278c6caa22b3d8bcd86acfb811376b9 Mon Sep 17 00:00:00 2001 From: Matilda-Njau Date: Wed, 8 Jan 2025 22:16:40 +0300 Subject: [PATCH 2/3] modify db-2 assignment --- code/db-2-assignment/models/Recipe.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/db-2-assignment/models/Recipe.js b/code/db-2-assignment/models/Recipe.js index 761d7326..777f5acd 100644 --- a/code/db-2-assignment/models/Recipe.js +++ b/code/db-2-assignment/models/Recipe.js @@ -29,4 +29,4 @@ export default mongoose.model("recipe", recipeSchema) // recipe represents the name of the model in my default // model export and it can be named anything on import // it all represents my db collection which is pluralized -// by default \ No newline at end of file +// by the db by default \ No newline at end of file From b9991b8d7f61811f2820aeede080a2e2e39598f9 Mon Sep 17 00:00:00 2001 From: Matilda Njau Date: Fri, 10 Jan 2025 14:34:33 +0300 Subject: [PATCH 3/3] make a slight change --- code/db-2-assignment/models/Recipe.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/code/db-2-assignment/models/Recipe.js b/code/db-2-assignment/models/Recipe.js index 777f5acd..ea57df46 100644 --- a/code/db-2-assignment/models/Recipe.js +++ b/code/db-2-assignment/models/Recipe.js @@ -28,5 +28,5 @@ const recipeSchema = new mongoose.Schema({ export default mongoose.model("recipe", recipeSchema) // recipe represents the name of the model in my default // model export and it can be named anything on import -// it all represents my db collection which is pluralized -// by the db by default \ No newline at end of file +// it also represents my db collection which is pluralized +// by the db by default