Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added new endpoint to get filtered courses according to price and category id #74

Open
wants to merge 1 commit into
base: DevG_Contribute
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions server/controllers/Courses.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,3 +436,44 @@ exports.deleteCourse = async (req, res) => {
});
}
};
//Get filtered courses accoring to price and category
exports.getfilteredCourses = async (req, res) => {
try {
const { price, category } = req.body;
// const courses=await Course.find({price:{$lte:price}});
if (!price) {
price = 0;
}
let courses;
if (!category) {
console.log("category not found");
courses = await Course.find({ price: { $lte: price } })
.populate([
{ path: "instructor", select: "firstName lastName", model: User },
])
.select("courseName price thumbnail instructor");
} else {
console.log("category found");
courses = await Course.find({
price: { $lte: price },
category: category,
})
.populate([
{ path: "instructor", select: "firstName lastName", model: User },
])
.select("courseName price thumbnail instructor");
}

res.status(200).json({
success: true,
data: courses,
});
} catch (error) {
console.error(error);
res.status(500).json({
success: false,
message: "Server error",
error: error.message,
});
}
};
2 changes: 2 additions & 0 deletions server/routes/Course.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const {
getFullCourseDetails,
getInstructorCourses,
deleteCourse,
getfilteredCourses,
} = require("../controllers/Courses");

//Import Category controllers
Expand Down Expand Up @@ -63,6 +64,7 @@ router.post("/deleteSection", auth, isInstructor, deleteSection);
router.post("/addSubSection", auth, isInstructor, createSubSection);
router.post("/updateSubSection", auth, isInstructor, updateSubSection);
router.post("/deleteSubSection", auth, isInstructor, deleteSubSection);
router.post("/getFilteredCourses", getfilteredCourses);

router.get("/getAllCourses", getAllCourses);
router.post("/getCourseDetails", getCourseDetails);
Expand Down
Loading