Skip to main content

Create/Launch new Product API +Update an Delete Product by Admin, Get Product Details by customer API Nodejs , MongoDB MERN Stack

 //  Create Required following files mentioned in previous post = https://vasubirla.blogspot.com/2023/02/register-new-user-login-user-delete.html 

config.env    

Server.js 

//app.js 

/utilsfolder/errorhander.js

/utils/jwtToken.js file

3. /utils/sendEmail.js  file



//Create Product Model - and import it in ProductController 


//ProductModel.js
const mongoose = require('mongoose');

const ProductSchema = new mongoose.Schema({

    name: {
        type:String,
        required:[true,"please enter produt name"],
        trim:true
    },
    description: {
        type:String,
        required:[true,"please add product description"]
    },
    price: {
        type:Number,
        required:[true,"Product Price is required"],
        maxLength:[8,"price cannot be exceed 8 character"]
    },
    ratings: {
        type:Number,
        default:0
    },
    image: [
        {
            public_id: {
                type:String,
                required:true
            },
            url: {
                type:String,
                required:true
            }
        }        
    ],
    category: {
        type:String,
        required:[true,"Please enter Product Category"]
    },
    stock:{
        type:Number,
        required:[true,"Product stoke limit required"],
        maxLength:[4,"stoke cannot be exceed 4 char"],
        default:1
    },
    numOfReviews:{
        type:Number,
        default:0
    },
    reviews: [
        {
            user:{
                type: mongoose.Schema.ObjectId,
                ref:"user",
                required:true
            },
            name:{
                type:String,
                required:true
            },
            rating:{
                type:Number,
                required:true
            },
            comment:{
                type:String,
                required:true
            }
        }
    ],
    createdAt:{
        type:Date,
        defualt:Date.now
    }
 })

 
 module.exports = mongoose.model("Product",ProductSchema)



//Create ProductController.js and import it in route 

//ProductController.js
const Product = require('../models/productModel');
const ErrorHander = require('../utils/errorhander');
const catchAsyncError = require('../middleware/catchAsyncError')
const ApiFeatures = require('../utils/apifeatures')

//create/add or Launch New Products to sell on website --by Admin
 
exports.createProduct = catchAsyncError(
    async (req,res,next)=>{

        const product = await Product.create(req.body);
   
        res.status(201).json({
   
            success:true,
            product
       
        })
   
   
    }

)
//==create Product API Testing on Postman





//Get Products Details on HomePage by different Customers or Visiters
exports.getAllProduct =catchAsyncError( async (req,res)=>{

        console.log("Hited from React End")
    const resultPerPage = 5; // show 5 product per page

    const productCount = await Product.countDocuments()

    const apiFeatures = new ApiFeatures(Product.find(),req.query).search().filter().pagination(resultPerPage)
   
    const products = await apiFeatures.query //await Product.find()

    res.status(200).json({

        success:true,
        products,
        productCount
    });

}
)

//== Get products API Testing on Postman =




//get perticular Product Details


    exports.getProductDetails =catchAsyncError( async (req,res,next)=>{

        const product = await Product.findById(req.params.id)

        if(!product){
            return next(new ErrorHander("Product Not Found",404))
        }
        else{
            res.status(200).json({
                success:true,
                product
            })
        }

       

    }

    )
//=====Open Perticular Product DEtails API Testing - Postman (with wrong ID)





//Update Product -- Admin

exports.updateProduct = catchAsyncError( async (req,res,next)=>{

    let product = await Product.findById(req.params.id)

    if(!product){
        res.status(500).json({
            success:false,
            message:"Product not found"
        })
    }
    product = await Product.findByIdAndUpdate(req.params.id,req.body,{

        new:true,
        runValidators:true,
        useFindAndModify:false
    })
    res.status(200).json({
        success:true,
        product
    })
}
)

//========Update Product API testing - only Admin can update - Postman







//=======delete Products=============================

exports.deleteProduct =catchAsyncError(  async(req,res,next)=>{

    const product = await Product.findById(req.params.id)

    if(!product){
        res.status(500).json({
            success:false,
            message:"Product not found"
        }) // or we can use this line  return next(new ErrorHander("Product Not Found",404))
    }
    else {
        await Product.findByIdAndDelete(req.params.id,req.body);

        res.status(200).json({
            success:true,
            message:"Product Deleted Successfully"
        })
    }
   


}
)


//Give Product Review or Update it if already exists =========================================

exports.createProductReview = catchAsyncError(async(req,res,next)=>{

        const { rating,comment, productId} = req.body;

    const review = {
        user:req.user.id,
        name:req.user.name,
        rating,
        comment
    }

    const product = await Product.findById(productId);

        const isReviewed = product.reviews.find(
           (rev)=>rev.user.toString()=== req.user._id.toString()
            )
        //here rev.user who already made reviewed and req.user._id is also your Id - If Both ID is same and Review already exists by you then it will be updated

        if(isReviewed)
        {
           product.reviews.forEach(rev=>{

            if(rev.user.toString()=== req.user._id.toString())
            {                
             rev.rating = rating,
             rev.comment = comment
            }
           })
        }
        else
        {
            product.reviews.push(review)
            product.numOfReviews = product.reviews.length
        }

        //Overall Rating = Total Rating divided by length of Rating
        let avg = 0
        product.reviews.forEach( rev=>{
            avg=avg+rev.rating
        })

        product.ratings = avg/product.reviews.length

        await product.save({validateBeforeSave:false})

        res.status(200).json({
            success:true,
            message:"You have rated this Product Successfully !!"
        })

})


//Get All Reviews of Product ========
exports.getProductReviews = catchAsyncError(async (req,res,next)=>{

    const product = await Product.findById(req.query.id)

    if(!product){
        return next(new ErrorHander("Product or Review Found ",404))
    }

    else{
        res.status(200).json({
            success:true,
            reviews:product.reviews
        })
    }
   

   
})

//Delete Review of Product
exports.deleteReview = catchAsyncError(async (req,res,next)=>{
    const product = await Product.findById(req.query.productId)
    if(!product){
        console.log("No Product found")
        return next(new ErrorHander("Product Not Found",404))
    }
    else{
           
        //now in review that all reviews are assigned which we dont want to delete  using filter
        const reviews = product.reviews.filter(rev=> rev._id.toString() !== req.query.id.toString());
       

        // now after deleteing review Overall Averaging Rating, Num of rating also should be updated
        let avg = 0
            reviews.forEach( rev=>{
            avg=avg+rev.rating
        })
        const ratings = avg/reviews.length        
        const numOfReviews = reviews.length
        await Product.findByIdAndUpdate(req.query.productId,{
            reviews ,ratings, numOfReviews
        },
        {
            new:true,
            runValidators:true,
            useFindAndModify:false
        })

        res.status(200).json({
            success:true,
            message:"Your Review has Been deleted "
        })
    }

})  





//Create ProductRoute.js 

//===== ProductRoute.js
const express = require('express');
const {getAllProduct,
    createProduct,
     updateProduct,
      deleteProduct,
       getProductDetails, createProductReview, getProductReviews, deleteReview} = require('../controllers/productController.js');
const { isAuthenticatedUser, authorizeRoles } = require('../middleware/auth.js');
const router = express.Router();

router.route('/products').get(getAllProduct);
router.route('/products/new').post(isAuthenticatedUser, authorizeRoles("admin"),createProduct);

router.route('/products/:id').put(isAuthenticatedUser, authorizeRoles("admin"),updateProduct);

router.route('/products/:id').delete(isAuthenticatedUser, authorizeRoles("admin"),deleteProduct);

router.route('/products/:id').get(getProductDetails );

router.route('/review').put(isAuthenticatedUser, createProductReview )

router.route('/reviews').get(getProductReviews)

router.route('/reviews').delete(isAuthenticatedUser,deleteReview)

module.exports = router

=================== End===============



Comments

Popular posts from this blog

Part 15- What is Repeater (Networking Devices)- Computer Networking- CCNA

Repeater  hello friends i am Vasu Birla and today i am starting new segment of CCNA computer networking  its a Networking Devices. we will discuss every important Devices used in networks. Repeater is a first networking devices. What is Repeater  Repeater is a device that receives signals and re-transmits by amplifying or regenerating signals. Repeater has two port one is for receive signals from previous network and second port is for retransmit signals to the next extended network. Two Port Repeater Repeater is a device which is used to Regenerate or replicate signal (Pichhe se aa rahe signal ko regenerate karke ya amplify karke aage strong signals bhej sakte he) Analog and digital both type of signals can be retransmitted by repeater. Here there is difference between regeneration and amplifying.  Amplification means , received signals will be amplified as it is , whether there are impurities in signals  and Regeneration means repeate...

GitHub Repo Collaboration Work on single project

 =============================================== To collaborate effectively with your friend on the same project, you should use Git branches to manage different lines of development. Here's a step-by-step procedure you can follow to streamline collaboration: 1. Create Separate Branches for Each Developer Create a New Branch for Your Friend: On your local repository, create a new branch for your friend. For example if your friend name is kilvish , if you want to create a branch named kilvish , you would run: make sure you would be on main branch already   command->  git checkout -b kilvish git push origin kilvish 2. Set Up Your Friend’s Environment( On your Friend's System )  at kilvish side  run ->  Clone the Repository (if not already done): If your friend hasn’t cloned the repository yet, they should do so: command ->  git clone https://github.com/Vasu-Birla/your-repo.git   // your your main clone line  Fetch All Bran...

Part 19- Router (Networking Devices)- Computer Networking- CCNA

Hello friends...i am Vasu Birla and today will discuss about the most important Networking Device ..Router.  so let's start... ROUTER Router is a device which connect two or more networks together, which is why router is also known is Inter-networking device also. Inter-networking means two or more networks are connected together with the help of router. one more thing router is just like a computer  but it is designed for routing only, our computer can be router also but that are software router while hardware router which are specialize for routing is more efficient and fast than software router.  There is a Operating System installed on router which get moves data from one network to another network with the help of routing table.  Router does work on Network layer or Layer 3 of the OSI model.  Cisco Router There many companies which manufacture Router but main companies are - Cisco , Juniper , HP, 3com and Nortel  ...