Programming Standards: Difference between revisions

From Seedbury Square
Tag: Manual revert
Line 75: Line 75:
* Inside the function, include the following structure:
* Inside the function, include the following structure:
** A validation section
** A validation section
<pre>
    <pre>
$validation = validate[FileName]Params($params);
    $validation = validate[FileName]Params($params);
if (!$validation['success']) return $validation;
    if (!$validation['success']) return $validation;
</pre>
    </pre>
** The service logic (body)
** The service logic (body)
** A return statement that returns a <code>uniformReturnObject</code>
** A return statement that returns a <code>uniformReturnObject</code>

Revision as of 20:30, 31 March 2025

Naming Convention

Constants

ROW_VALUE_DESCRIPTION_COLUMN_NAME_TABLE_NAME

CFI

  • ObjectType
- DESCRIPTION_EN_OBJECT_TYPE_CODE
  • [Table]Type
DESCRIPTION_EN_[TABLENAME]_TYPE_CODE
i.e (FileType - MotionType - Code MOTION_FILE_TYPE_CODE)

Handling Service Response

Standards how to handle service response. Every main in any app will have algo asi:

 async request(params, { transition = false, showMsg = true, setStatus = undefined, fullResponse = false } = {}) {
        const serverResponse = await httpRequest(params, setStatus, REQUEST_BRIDGE_SERVICE);
        if (!serverResponse?.success) console.error(serverResponse?.errorMsg ?? 'Backend service NULL');
        return serverResponse?.success
            ? Promise.resolve(fullResponse
                ? serverResponse
                : serverResponse.response)
            : Promise.reject(serverResponse);
    }

Por eso in the services should be handled like this:

[service]
    .then((response) => {
        // Handle successful service
    })
    .catch((err) => {
        console.log("Error: ", err);
        // Handle not successful service
    });

Parameters

Javascript

Function, classes and methods...

/**
 * @param {AppProps} parentProps
 * @param {object} settings
 **/
name(parentProps, {} = {}) {

}

Where settings means all other props.

PHP

PHP Service Function Guidelines

Every PHP service function should follow these rules:

  • It must be in its own file.
  • The file name and the function name should be the same.
  • It should return: Closure::fromCallable('fileName')
  • The function must include the standard service header.
/**
 * fileName.php
 * @author [FirstName LastName] <firstName.lastName@seedburysquare.com>
 * @copyright Seedbury Square, LLC. All Rights Reserved.
 *
 * @version YYYY-MM-DD Initial Version
 */
  • Inside the function, include the following structure:
    • A validation section
    $validation = validate[FileName]Params($params);
    if (!$validation['success']) return $validation;
    
    • The service logic (body)
    • A return statement that returns a uniformReturnObject

Additionally, everything inside the function should be wrapped in a try-catch block.

In the catch block:

  • Log the error using error_log()
  • Return a uniformReturnObject where:
    • success is set to false
    • errorMsg and displayDialog contain the value of $e->getMessage()
catch (\Throwable $e) {
        error_log($e);
        return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage());
    }

Here is a full example of how a getExampleDescriptionByExampleId would look like:

<?php

/**
 * getExampleDescriptionByExampleId.php
 * @author [FirstName LastName] <firstName.lastName@seedburysquare.com>
 * @copyright Seedbury Square, LLC. All Rights Reserved.
 *
 * @version YYYY-MM-DD Initial Version
 */
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../databaseConnection/DatabaseDriver.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/constants/databaseConstants.php';
require_once __DIR__ . DIRECTORY_SEPARATOR . '../../utilities/helpers/helpers.php';
return Closure::fromCallable('getExampleDescriptionByExampleId');
function getExampleDescriptionByExampleId($params, $dbDriver = null)
{
    try {
        $validation = validateGetExampleDescriptionByExampleIdParams($params);
        if (!$validation['success']) return $validation;

        $dbDriver = $dbDriver ?? new DatabaseDriver([DATABASE_CONNECTION_CONSTANT]);
        $response = $dbDriver->(
            SELECT 
            ex.description
            FROM examples ex WITH(NOLOCK)
            WHERE ex.explampleId = ?
            AND ex.isDeleted = 0;',
            [$params['exampleId']]
        );
        return uniformReturnObject(
            $response['success'],
            $response['success'] ? $response['result'] : null,
            $response['success'] ? false : $response['errorMsg'],
            $response['success'] ? false : $response['errorMsg']
        );
    } catch (\Throwable $e) {
        error_log($e);
        return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage());
    }
};
function validateGetExampleDescriptionByExampleIdParams($params)
{
    $errors = [];
    $mandatory = ['exampleId'];
    foreach ($mandatory as $prop) {
        if (!isset($params[$prop])) {
            $errors[] = "<li>Missing: $prop</li>";
        }
    }
    $success = empty($errors);
    $errorMsg = 'getExampleDescriptionByExampleId<br>Errors: <br><ol>' . implode('', $errors) . '</ol>';
    return uniformReturnObject(
        $success,
        null,
        $success ? null : $errorMsg,
        $success ? null : $errorMsg
    );
}