-
Notifications
You must be signed in to change notification settings - Fork 1
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
himanshusachan112
committed
Oct 23, 2024
1 parent
2211054
commit 5fd5f8b
Showing
9 changed files
with
249 additions
and
4 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
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
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,180 @@ | ||
const Category = require("../models/Category"); | ||
const Product = require("../models/Product"); | ||
const User=require("../models/User"); | ||
const Ratingandreviews=require("../models/Ratingandreviews"); | ||
|
||
|
||
require("dotenv").config() | ||
|
||
exports.createreview=async (req,res)=>{ | ||
try{ | ||
const {id,email}=req.user; | ||
const {rating,comment,productid}=req.body; | ||
if(!id || !email || !rating || !comment || !productid ){ | ||
return res.json({ | ||
success:false, | ||
message:"All fields are required" | ||
}) | ||
} | ||
|
||
const user=await User.findById(id); | ||
if(!user){ | ||
return res.json({ | ||
success:false, | ||
message:"User Not Registered", | ||
}) | ||
} | ||
|
||
const ratedetails=await Ratingandreviews.create({ | ||
rating, | ||
comment, | ||
}) | ||
|
||
const userupdate=await User.findByIdAndUpdate(id, | ||
{$push:{ratingandreviews:ratedetails._id}} | ||
) | ||
|
||
const productupdate=await Product.findByIdAndUpdate(productid, | ||
{$push:{ratingandreviews:ratedetails._id}} | ||
) | ||
console.log("This is product update ",productupdate) | ||
|
||
return res.json({ | ||
success:true, | ||
message: "Review Created Successfully" | ||
}) | ||
|
||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
success:false, | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
|
||
|
||
exports.deletereview=async (req,res)=>{ | ||
try{ | ||
|
||
|
||
const {id,email}=req.user; | ||
const {reviewid, productid}=req.body; | ||
if(!id || !email || !productid){ | ||
return res.json({ | ||
success:false, | ||
message:"All fields are required" | ||
}) | ||
} | ||
|
||
const user=await User.findById(id); | ||
if(!user){ | ||
return res.json({ | ||
success:false, | ||
message:"User Not Registered", | ||
}) | ||
} | ||
|
||
const prodel=await Ratingandreviews.findByIdAndDelete(reviewid); | ||
|
||
await User.findByIdAndUpdate(id, | ||
{$pull:{ratingandreviews:prodel._id}} | ||
) | ||
|
||
await Product.findByIdAndUpdate(productid, | ||
{$pull:{ratingandreviews:prodel._id}} | ||
) | ||
|
||
return res.json({ | ||
success:true, | ||
message:"Deleted Review Successfully" | ||
}) | ||
|
||
|
||
|
||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
success:false, | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
|
||
|
||
|
||
|
||
|
||
exports.getproductreviews=async (req,res)=>{ | ||
try{ | ||
|
||
const {productid}=req.body; | ||
const data=await Product.findById(productid).populate("ratingandreviews"); | ||
return res.json({ | ||
success:true, | ||
message:"Product reivews fetched successfully", | ||
data:data | ||
}) | ||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
success:false, | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
|
||
|
||
|
||
exports.getcategoryreviews=async (req,res)=>{ | ||
try{ | ||
const {cateid}=req.body; | ||
const data=await Category.findById(cateid).populate({ | ||
path: 'products', | ||
populate: { | ||
path: 'ratingandreviews', | ||
model: 'Ratingandreviews' | ||
} | ||
}); | ||
|
||
return res.json({ | ||
success:true, | ||
message:"fetched category wise rating successfully", | ||
data:data | ||
}) | ||
|
||
|
||
|
||
|
||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
success:false, | ||
message:err.message, | ||
}) | ||
} | ||
} | ||
|
||
exports.getuserreviews=async (req,res)=>{ | ||
try{ | ||
console.log('comming') | ||
const {email}=req.body; | ||
const data=await User.findOne({email:email}).populate("ratingandreviews"); | ||
return res.json({ | ||
success:true, | ||
message:"User reviews fetched successfully", | ||
data:data | ||
}) | ||
|
||
} | ||
catch(err){ | ||
return res.json({ | ||
success:false, | ||
message:err.message, | ||
}) | ||
} | ||
} |
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
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
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,16 @@ | ||
const mongoose=require("mongoose"); | ||
|
||
|
||
const ratingandreivewsschema=new mongoose.Schema({ | ||
rating:{ | ||
type:Number, | ||
required:true | ||
}, | ||
comment:{ | ||
type:String, | ||
required:true | ||
}, | ||
|
||
}); | ||
|
||
module.exports=mongoose.model("Ratingandreviews",ratingandreivewsschema); |
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
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
const express=require("express"); | ||
const { createcategory,deletecategory} = require("../controllers/category"); | ||
const { createcategory,deletecategory,getcategories} = require("../controllers/category"); | ||
const {auth}=require("../middlewares/auth"); | ||
|
||
const router=express.Router(); | ||
|
||
router.post("/createcategory",auth,createcategory); | ||
router.post("/deletecategory",auth,deletecategory); | ||
router.post("/getcategories",getcategories); | ||
|
||
|
||
module.exports = router |
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 {auth}=require("../middlewares/auth"); | ||
const { createreview, getuserreviews,deletereview, getproductreviews, getcategoryreviews} = require("../controllers/ratingandreviews"); | ||
|
||
|
||
|
||
router.post("/createreview",auth,createreview); | ||
router.post("/deletereview",auth,deletereview); | ||
router.post("/getuserreviews",getuserreviews); | ||
router.post("/getproductreviews",getproductreviews); | ||
router.post("/getcategoryreviews",getcategoryreviews ); | ||
|
||
|
||
module.exports=router; |