Skip to content
Open
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
150 changes: 95 additions & 55 deletions code/db-2-assignment/controllers/recipes-controllers.js
Original file line number Diff line number Diff line change
@@ -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 }
} catch (error) {
console.log(error);
res.status(500).json({ message: error.message });
}
};

export {
getAllRecipes,
getRecipeById,
createRecipe,
updateRecipeById,
deleteRecipeById,
};
6 changes: 5 additions & 1 deletion code/db-2-assignment/models/Recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,8 @@ const recipeSchema = new mongoose.Schema({
{ timestamps: true }
)

export default mongoose.model("recipe", recipeSchema)
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 also represents my db collection which is pluralized
// by the db by default
23 changes: 14 additions & 9 deletions code/db-2-assignment/routes/recipes.js
Original file line number Diff line number Diff line change
@@ -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;