text: class ApiFeatures {
    constructor(query, queryString) {
      this.query = query;
      this.queryString = queryString;
    }
  
    search() {
      let obj = {};
      const { search } = this.queryString;
  
      if (search) {
        obj = {
          $or: [
            { title: { $regex: search, $options: 'i' } },
            { content: { $regex: search, $options: 'i' } }
          ]
        };
      }
  
      this.query = this.query.find(obj);
  
      return this;
    }
  
    filter() {
      const queryObj = { ...this.queryString };
      const excludedFields = ['sort', 'fields', 'page', 'limit'];
      excludedFields.forEach(el => delete queryObj[el]);
  
      let queryString = JSON.stringify(queryObj);
      queryString = queryString.replace(
        /(gte|lte|gt|lt)\b/,
        match => `$${match}`
      );
  
      this.query = this.query.find(JSON.parse(queryString));
  
      return this;
    }
  
    sort() {
      if (this.queryString.sort) {
        const sortBy = this.queryString.sort.split(',').join(' ');
        this.query = this.query.sort(sortBy);
      } else {
        this.query = this.query.sort('-createdAt');
      }
  
      return this;
    }
  
    limitFields() {
      if (this.queryString.fields) {
        const selectedFields = this.queryString.fields.split(',').join(' ');
        this.query = this.query.select(selectedFields);
      } else {
        this.query = this.query.select('-__v');
      }
  
      return this;
    }
  
    paginate() {
      const page = this.queryString.page * 1;
      const limit = this.queryString.limit * 1;
      const skip = (page - 1) * limit;
  
      this.query = this.query.skip(skip).limit(limit);
  
      return this;
    }
  }
  
  module.exports = ApiFeatures;
  