Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
himanshugupta-hg committed Dec 11, 2024
0 parents commit 2a3eee6
Show file tree
Hide file tree
Showing 6 changed files with 254 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
**node_modules
**.env
**package-lock.json
73 changes: 73 additions & 0 deletions controllers/product.controller.js
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
};
98 changes: 98 additions & 0 deletions index.js
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!");
});
34 changes: 34 additions & 0 deletions models/product.model.js
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;
29 changes: 29 additions & 0 deletions package.json
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"
}
}
17 changes: 17 additions & 0 deletions routes/product.route.js
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;

0 comments on commit 2a3eee6

Please sign in to comment.