Programming Standards: Difference between revisions
No edit summary |
|||
| (50 intermediate revisions by the same user not shown) | |||
| Line 5: | Line 5: | ||
ROW_VALUE_DESCRIPTION_COLUMN_NAME_TABLE_NAME | ROW_VALUE_DESCRIPTION_COLUMN_NAME_TABLE_NAME | ||
==== | ==== CFI ==== | ||
* ObjectType | * ObjectType | ||
::- DESCRIPTION_EN_OBJECT_TYPE_CODE | ::- DESCRIPTION_EN_OBJECT_TYPE_CODE | ||
| Line 11: | Line 11: | ||
:: DESCRIPTION_EN_[TABLENAME]_TYPE_CODE | :: DESCRIPTION_EN_[TABLENAME]_TYPE_CODE | ||
::i.e (FileType - MotionType - Code MOTION_FILE_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: | |||
<pre> | |||
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); | |||
} | |||
</pre> | |||
Por eso in the services should be handled like this: | |||
<pre> | |||
[service] | |||
.then((response) => { | |||
// Handle successful service | |||
}) | |||
.catch((err) => { | |||
console.log("Error: ", err); | |||
// Handle not successful service | |||
}); | |||
</pre> | |||
== Parameters == | |||
=== Javascript === | |||
Function, classes and methods... | |||
<pre> | |||
/** | |||
* @param {AppProps} parentProps | |||
* @param {object} settings | |||
**/ | |||
name(parentProps, {} = {}) { | |||
} | |||
</pre> | |||
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: <code>Closure::fromCallable('fileName')</code> | |||
* The function must include the standard service header. | |||
<pre> | |||
/** | |||
* fileName.php | |||
* @author [FirstName LastName] <firstName.lastName@seedburysquare.com> | |||
* @copyright Seedbury Square, LLC. All Rights Reserved. | |||
* | |||
* @version YYYY-MM-DD Initial Version | |||
*/ | |||
</pre> | |||
* Inside the function, include the following structure: | |||
** A validation section | |||
<pre> | |||
$validation = validate[FileName]Params($params); | |||
if (!$validation['success']) return $validation; | |||
</pre> | |||
:* The service logic (body) | |||
:* A return statement that returns a <code>uniformReturnObject</code> | |||
Additionally, everything inside the function should be wrapped in a <code>try-catch</code> block. | |||
In the <code>catch</code> block: | |||
* Log the error using <code>error_log()</code> | |||
* Return a <code>uniformReturnObject</code> where: | |||
** <code>success</code> is set to <code>false</code> | |||
** <code>errorMsg</code> and <code>displayDialog</code> contain the value of <code>$e->getMessage()</code> | |||
<pre> | |||
catch (\Throwable $e) { | |||
error_log($e); | |||
return uniformReturnObject(false, null, $e->getMessage(), $e->getMessage()); | |||
} | |||
</pre> | |||
Here is a full example of how a getExampleDescriptionByExampleId would look like: | |||
<pre> | |||
<?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 | |||
); | |||
} | |||
</pre> | |||
Latest revision as of 20:34, 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
uniformReturnObjectwhere:successis set tofalseerrorMsganddisplayDialogcontain 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
);
}