-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2a3eee6
Showing
6 changed files
with
254 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
**node_modules | ||
**.env | ||
**package-lock.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
const Product = require('../models/product.model'); | ||
|
||
//see all products | ||
const getProducts = async (req, res) => { | ||
try{ | ||
const products = await Product.find({}); | ||
res.status(200).json({products}); | ||
} catch (error) { | ||
res.status(500).json({message: error.message}); | ||
} | ||
}; | ||
|
||
//see one product | ||
const getProduct = async (req, res) => { | ||
try{ | ||
const {id} = req.params; | ||
const product = await Product.findById(id); | ||
res.status(200).json(product); | ||
} catch (error) { | ||
res.status(500).json({message: error.message}); | ||
} | ||
}; | ||
|
||
//create a product | ||
const createProduct = async (req, res) => { | ||
try{ | ||
const product = await Product.create(req.body); | ||
res.status(200).json(product); | ||
} catch (error) { | ||
res.status(500).json({message: error.message}); | ||
} | ||
}; | ||
|
||
//update the product | ||
const updateProduct = async (req, res) => { | ||
try{ | ||
const {id} = req.params; | ||
const product = await Product.findByIdAndUpdate(id, req.body); | ||
|
||
if(!product) { | ||
return res.status(404).json({message: "Product not found"}); | ||
} | ||
|
||
const updatedProduct = await Product.findById(id); | ||
res.status(200).json(updatedProduct); | ||
} catch (error) { | ||
res.status(500).json({message: error.message}); | ||
} | ||
}; | ||
|
||
//delete the product | ||
const deleteProduct = async (req, res) => { | ||
try{ | ||
const {id} = req.params; | ||
const product = await Product.findByIdAndDelete(id); | ||
|
||
if(!product) { | ||
return res.status(404).json({message: "Product not found"}); | ||
} | ||
|
||
res.status(200).json({message: "Product deleted successfully"}); | ||
} catch { | ||
res.status(500).json({message: error.message}); | ||
} | ||
}; | ||
|
||
module.exports = { | ||
getProducts, | ||
getProduct, | ||
createProduct, | ||
updateProduct, | ||
deleteProduct | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
const express = require("express"); | ||
const mongoose = require("mongoose"); | ||
const Product = require("./models/product.model.js"); //fetch schema | ||
const productRoutes = require("./routes/product.route.js"); | ||
import { config } from "dotenv"; | ||
config(); | ||
const app = express(); | ||
|
||
//middleware to use json and form types | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
|
||
//routes | ||
app.use("/api/products", productRoutes); | ||
|
||
//for generalization | ||
app.get("/", (req, res) => { | ||
res.send("Hello from Node API server Updated!"); | ||
}); | ||
|
||
//if in one file i.e. NO MVC | ||
// //read api | ||
// app.get("/api/products", async (req, res) => { | ||
// try { | ||
// const products = await Product.find({}); | ||
// res.status(200).json(products); | ||
// } catch { | ||
// res.status(500).json({ message: error.message }); | ||
// } | ||
// }); | ||
|
||
// //read api of a particular product using its id | ||
// app.get("/api/product/:id", async (req, res) => { | ||
// try { | ||
// const { id } = req.params; | ||
// const product = await Product.findById(id); | ||
// res.status(200).json(product); | ||
// } catch { | ||
// res.status(500).json({ message: error.message }); | ||
// } | ||
// }); | ||
|
||
// //update api | ||
// app.put("/api/product/:id", async (req, res) => { | ||
// try { | ||
// const { id } = req.params; | ||
|
||
// const product = await Product.findByIdAndUpdate(id, req.body); | ||
|
||
// if (!product) { | ||
// return res.status(404).json({ message: "Product not found" }); | ||
// } | ||
|
||
// const updatedProduct = await Product.findById(id); | ||
// res.status(200).json(updatedProduct); | ||
// } catch { | ||
// res.status(500).json({ message: error.message }); | ||
// } | ||
// }); | ||
|
||
// //delete api | ||
// app.delete("/api/product/:id", async (req, res) => { | ||
// try{ | ||
// const {id} = req.params; | ||
|
||
// const product = await Product.findByIdAndDelete(id); | ||
|
||
// if(!product) { | ||
// return res.status(404).json({message: "Product not found"}) | ||
// } | ||
|
||
// res.status(200).json({message: "Product deleted succesfully"}); | ||
// } catch { | ||
// res.status(500).json({message: error.message}); | ||
// } | ||
// }) | ||
|
||
// //create api | ||
// app.post("/api/products", async (req, res) => { | ||
// try { | ||
// const product = await Product.create(req.body); | ||
// res.status(200).json(product); | ||
// } catch (error) { | ||
// res.status(500).json({ message: error.message }); | ||
// } | ||
// }); | ||
|
||
mongoose | ||
.connect(process.env.MONGODB_URL) | ||
.then(() => { | ||
console.log("Connected to database!"), | ||
app.listen(3000, () => { | ||
console.log("Server is running on port 3000"); | ||
}); | ||
}) | ||
.catch(() => { | ||
console.log("Connection failed!"); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const mongoose = require("mongoose"); | ||
|
||
const ProductSchema = mongoose.Schema( | ||
{ | ||
name: { | ||
type: String, | ||
required: [true, "Please enter your name"], | ||
}, | ||
|
||
quantity: { | ||
type: Number, | ||
required: true, | ||
default: 0, | ||
}, | ||
|
||
price: { | ||
type: Number, | ||
required: true, | ||
default: 0, | ||
}, | ||
|
||
image: { | ||
type: String, | ||
required: false, | ||
}, | ||
}, | ||
{ | ||
timestamps: true, | ||
} | ||
); | ||
|
||
const Product = mongoose.model("Product", ProductSchema); | ||
|
||
module.exports = Product; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
{ | ||
"name": "crud", | ||
"version": "1.0.0", | ||
"description": "Crud operations on project using Node, Express, MongoDB", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"serve": "node index.js", | ||
"dev": "nodemon index.js" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git+https://github.com/himanshu-hg-github/Crud.git" | ||
}, | ||
"author": "Himanshu Gupta HG", | ||
"license": "ISC", | ||
"bugs": { | ||
"url": "https://github.com/himanshu-hg-github/Crud/issues" | ||
}, | ||
"homepage": "https://github.com/himanshu-hg-github/Crud#readme", | ||
"dependencies": { | ||
"express": "^4.19.2", | ||
"mongodb": "^6.7.0", | ||
"mongoose": "^8.4.1" | ||
}, | ||
"devDependencies": { | ||
"nodemon": "^3.1.3" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const express = require('express') | ||
const router = express.Router(); | ||
const {getProducts, getProduct, createProduct, updateProduct, deleteProduct} = require('../controllers/product.controller.js') | ||
|
||
|
||
router.get('/', getProducts); //see all products | ||
|
||
router.get('/:id', getProduct); //see one product | ||
|
||
router.post('/', createProduct); //create a product | ||
|
||
router.put('/:id', updateProduct); //update the product | ||
|
||
router.delete('/:id', deleteProduct); //delete the product | ||
|
||
|
||
module.exports = router; |