In this blog we will be making the product controllers for our Product API's
Step-1 Open the Controllers
folder and make a new file named productControllers.js
. Here we will be writing all the codes for the manipulation of the Products
Step-2 Import the productModel
that we made in the previous part by writing the following code
Our Aim now is to make the following product controllers, if needed we will add more in the later parts
The product controllers will be
- creating new product
- getting all the products
- getting product details
- updating product details
- deleting a product
1. Creating New Product
After importing the productModel
we will make an async arrow function
named createProduct
with parameters req
, res
& next
.
Here the req
is the request we will be making, res
is the response we will be getting back & next
is another parameter which we will be using for error handling
Now we will be creating a const
variable named product
which will create the the product
using the mongoose
function create()
which takes the and object
or array
in which the data will be stored in the database.
We take the data from the the body by writing req.body
.
After that we send a success through res.status(200)
and also a json
object with two messages success: true
& the product
.
The final function shall looks like the below
2. Getting All Products
Now to get all the products we can use the find()
function. Find
will list all the items in the database.
The final code of this function is
3. Get Product Detail
In this function we will be finding the product through it's id
with the help of the function findById()
which will take the id
of the product will be auto generated by mongodb
from the params
of the url.
After getting the product we will check if the product exist
or not. If not we will return the function with a 500
response code and a json
message with success: false
and Product Not Found
.
But if the product exist we will send a response with status code 200
& a json
object with status code 200
and the product
variable which contains all the details.
The final function shall look like this
4. Updating Product Details
Now for updating product details we make a normal variable
named product
which will store the details of the product which we got by using findById()
.
After that we check if the product exits and if not we return a status code of 500
with json message as success: false
and Product Not Found
.
If the product exits then we update the product by using findByIdAndUpdate()
which takes two parameters, Id
and the Updated Properties
.
After updating we just send a success response
5. Deleting Product
Its the same function as getting product details, just here after checking if the product exist
we call an await function remove()
which will remove the product of which we gave the Id
After removing we again write the success response code of it
The Full code of this section is
const Product = require("../models/productModel");
// create product --- Admin
exports.createProduct = async (req, res, next) => {
const product = await Product.create(req.body);
res.status(201).json({
success: true,
product,
});
};
// Get all products
exports.getAllProducts = async (req, res, next) => {
const products = await Product.find();
res.status(200).json({
success: true,
products,
});
};
// get product details
exports.getProductDetails = async (req, res, next) => {
const product = await Product.findById(req.params.id);
if (!product) {
return res
.status(500)
.json({ success: false, message: "Product Not Found" });
}
res.status(200).json({
success: true,
product,
});
};
// Update product ---Admin
exports.updateProduct = async (req, res, next) => {
let product = await Product.findById(req.params.id);
if (!product) {
return res.status(500).json({
success: false,
message: "Product not Found",
});
}
product = await Product.findByIdAndUpdate(req.params.id, req.body);
res.status(200).json({
success: true,
product,
});
};
// Delete Product ---Admin
exports.deleteProduct = async (req, res, next) => {
const product = await Product.findById(req.params.id);
if (!product) {
return res
.status(500)
.json({ success: false, message: "Product Not Found" });
}
await product.remove();
res.status(200).json({
success: true,
message: "Product Deleted Successfully",
});
};
*Note : * We will be adding Admin & Developer Routes
in the later parts.
We have successfully completed making the controllers for the products.
Now lets go ahead and make the Product Routes through which we can use these functions
Part-3.3 Product Routes
Top comments (0)