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
14 changes: 14 additions & 0 deletions Code/database/mongoose-schemas/app.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import express from "express"
import { dbConnect } from "./db.js"
const app = express()
const PORT = 4000

app.use(express.json())

import router from "./controllers/recipe.js"
app.use("/recipe", router)

app.listen(PORT, () => {
dbConnect()
console.log(`Server: Listening on port ${PORT}`)
})
55 changes: 55 additions & 0 deletions Code/database/mongoose-schemas/controllers/recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import e from "express";
const router = e.Router()
import { mongoose } from "../db.js";

import Recipe from "../models/Recipe.js";

//Get all recipes
router.get("/", async (req, res) => {
try{
const recipe = await Recipe.find()
res.send(recipe)
} catch(err) {
res.status(500).send({message: err.message})
}
})

// Get a single recipe by id
router.get("/find/:id", async (req, res) => {
if (mongoose.Types.ObjectId.isValid(req.params.id)) {
const recipe= await Recipe.findOne({ _id: req.params.id })
recipe ? res.json(recipe) : res.status(404).send("recipe not found")
} else {
res.status(404).send("invalid recipe id")
}
})

//Create a new recipe
router.post("/", async (req, res) => {
try {
const newRecipe = new Recipe(req.body)
await newRecipe.save()
res.send(`added ${req.body.title} recipe`)
} catch (err) {
res.status(422).send(err)
}
})

//Update an existing recipe
router.put("/update/:id", async (req, res) => {
await Recipe.findByIdAndUpdate({_id: req.params.id}, req.body)
res.send(`Updated successfully`)
})

//delete a recipe
router.delete("/delete/:id", async (req, res) => {
if (mongoose.Types.ObjectId.isValid(req.params.id)) {
await Recipe.deleteOne()
res.send("Recipe successfully deleted")
} else {
res.status(404).send("invalid recipe id")
}
})

export default router

13 changes: 13 additions & 0 deletions Code/database/mongoose-schemas/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import mongoose from "mongoose";

const dbConnect = async () => {
try{
await mongoose.connect("mongodb://localhost:27017/Recipe")
console.log(`Database: MongoDB Connected`);

} catch(err) {
console.warn(`[database error] : ${err} `)
}
}

export { dbConnect, mongoose }
24 changes: 24 additions & 0 deletions Code/database/mongoose-schemas/models/Recipe.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { mongoose } from "../db.js";

const Recipe = new mongoose.Schema({
title: {
type: String,
maxlength: 100
},
author: {
type: String,
maxlength: 50
},
ingredients: [String],

instructions: {
type: String,
maxlength: 2000
},

})



export default mongoose.model("recipe", Recipe)

Loading