# All files / controllers BaseController.js

0% Statements 0/26
0% Branches 0/22
0% Functions 0/6
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103

     /**
     * Base Controller
     * Provides common functionality for all controllers
     *
     * @class BaseController
     */

    const { pool } = require('../config/database');
    const { DatabaseError } = require('../middleware/errorHandler');

    class BaseController {
      /**
       * Get database connection from pool
       */
      static async getConnection() {
        try {
          return await pool.getConnection();
        } catch (error) {
          throw new DatabaseError('Failed to get database connection');
        }
      }

      /**
       * Execute database query with automatic error handling
       */
      static async executeQuery(query, params = [], connection = null) {
        const shouldRelease = !connection;

        try {
          if (!connection) {
            connection = await this.getConnection();
          }

          const [result] = await connection.execute(query, params);
          return result;
        } catch (error) {
          throw new DatabaseError(`Database query failed: ${error.message}`);
        } finally {
          if (shouldRelease && connection) {
            connection.release();
          }
        }
      }

      /**
       * Send standardized success response
       */
      static sendSuccess(res, data = null, statusCode = 200, message = null) {
        const response = {
          success: true,
          ...(message && { message }),
          ...(data !== null && { data })
        };
        return res.status(statusCode).json(response);
      }

      /**
       * Send error response
       */
      static sendError(res, message, statusCode = 400) {
        return res.status(statusCode).json({
          success: false,
          error: message
        });
      }

      /**
       * Paginate results
       */
      static paginate(data, page = 1, limit = 10) {
        const offset = (page - 1) * limit;
        const total = data.length;
        const totalPages = Math.ceil(total / limit);
        const paginatedData = data.slice(offset, offset + limit);

        return {
          data: paginatedData,
          pagination: {
            page: parseInt(page),
            limit: parseInt(limit),
            total,
            totalPages
          }
        };
      }

      /**
       * Validate pagination parameters
       */
      static validatePagination(page, limit) {
        const pageNum = Math.max(1, parseInt(page) || 1);
        const limitNum = Math.min(100, Math.max(1, parseInt(limit) || 10));

        return {
          page: pageNum,
          limit: limitNum,
          offset: (pageNum - 1) * limitNum
        };
      }
    }

    module.exports = BaseController;