From e03de96466380f7808b57b434808f08aa8d03528 Mon Sep 17 00:00:00 2001 From: Sibunnayak Date: Fri, 13 Sep 2024 13:00:48 +0530 Subject: [PATCH] fix get all brand , get all categories and add user type for single user leave --- resources/Brands/BrandsController.js | 34 ++++++++++++++++++++++-- resources/Category/categoryController.js | 33 +++++++++++++++++++++-- resources/Leaves/LeaveController.js | 1 + 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/resources/Brands/BrandsController.js b/resources/Brands/BrandsController.js index 554b066..f225f01 100644 --- a/resources/Brands/BrandsController.js +++ b/resources/Brands/BrandsController.js @@ -35,13 +35,43 @@ export const addBrand = async (req, res) => { // Get all Brands export const getBrands = async (req, res) => { try { - const brands = await BrandModel.find().sort({ createdAt: -1 }); + const PAGE_SIZE = parseInt(req.query.show) || 10; + const page = parseInt(req.query.page) || 1; + const skip = (page - 1) * PAGE_SIZE; + let filter = {}; + + // Search by brandName if provided + if (req.query.brandName) { + filter.brandName = { + $regex: new RegExp(req.query.brandName, "i"), // Case-insensitive search + }; + } + + // Get total number of brands matching the filter + const total = await BrandModel.countDocuments(filter); + + // Fetch brands with pagination and filtering + const brands = await BrandModel.find(filter) + .limit(PAGE_SIZE) + .skip(skip) + .sort({ createdAt: -1 }) + .exec(); + + // If no brands are found, return 404 error if (!brands.length) { return res.status(404).json({ message: "No brands found" }); } - res.status(200).json({ success: true, brands }); + // Return the paginated and filtered brands list + res.status(200).json({ + success: true, + total_data: total, + total_pages: Math.ceil(total / PAGE_SIZE), + current_page: page, + brands, + }); } catch (error) { + // Handle server error res.status(500).json({ success: false, message: error.message || "Something went wrong", diff --git a/resources/Category/categoryController.js b/resources/Category/categoryController.js index e3eaebd..a9b2d76 100644 --- a/resources/Category/categoryController.js +++ b/resources/Category/categoryController.js @@ -35,14 +35,43 @@ export const addCategory = async (req, res) => { // Get all Categories export const getCategories = async (req, res) => { try { - const categories = await CategoryModel.find().sort({ createdAt: -1 }); + const PAGE_SIZE = parseInt(req.query.show) || 10; + const page = parseInt(req.query.page) || 1; + const skip = (page - 1) * PAGE_SIZE; + let filter = {}; + // Handle filtering by categoryName + if (req.query.categoryName) { + filter.categoryName = { + $regex: new RegExp(req.query.categoryName, "i"), + }; + } + + // Count the total number of documents matching the filter + const total = await CategoryModel.countDocuments(filter); + + // Fetch the categories with pagination + const categories = await CategoryModel.find(filter) + .limit(PAGE_SIZE) + .skip(skip) + .sort({ createdAt: -1 }) + .exec(); + + // If no categories are found, return a 404 error if (!categories.length) { return res.status(404).json({ message: "No categories found" }); } - res.status(200).json({ success: true, categories }); + // Return success response with total data and total pages + res.status(200).json({ + success: true, + total_data: total, + total_pages: Math.ceil(total / PAGE_SIZE), + current_page: page, + categories, + }); } catch (error) { + // Handle server error res.status(500).json({ success: false, message: error.message || "Something went wrong", diff --git a/resources/Leaves/LeaveController.js b/resources/Leaves/LeaveController.js index 2cea40e..5e239c6 100644 --- a/resources/Leaves/LeaveController.js +++ b/resources/Leaves/LeaveController.js @@ -140,6 +140,7 @@ export const AdmingetLeaveByUser = async (req, res) => { success: true, user: leaveDoc.userId, leave: paginatedRecords, + userType: leaveDoc.userType, total_data: totalData, }); } catch (error) {