added comments to db
This commit is contained in:
34
api/db.php
34
api/db.php
@@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
// Database Helper
|
// Database Helper
|
||||||
// Provides the PDO Connection and shared Utility Functions for all
|
// Provides PDO Connection to Database and shared miscellaneous
|
||||||
// API Endpoints. Include this File at the Top of every Endpoint.
|
// Functions for all API Endpoints.
|
||||||
// =====================================================================
|
// =====================================================================
|
||||||
|
|
||||||
require_once __DIR__ . '/init.php';
|
require_once __DIR__ . '/init.php';
|
||||||
@@ -10,12 +10,16 @@ require_once __DIR__ . '/init.php';
|
|||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// JSON Response
|
// JSON Response
|
||||||
// Sends a JSON-encoded Response with the given HTTP Status Code and
|
// Creates JSON Response including HTTP Status Code and HTTP Header
|
||||||
// terminates the Script. Every API Endpoint uses this for Output.
|
// for every API Endpoint and terminates the Script.
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
function json_response($data, $status_code = 200) {
|
function json_response($data, $status_code = 200) {
|
||||||
|
// Defines HTTP Status Code and HTTP Header
|
||||||
|
// 1XX Informational, 2XX Successful, 3XX Redirection,
|
||||||
|
// 4XX Client Error, 5XX Server Error
|
||||||
http_response_code($status_code);
|
http_response_code($status_code);
|
||||||
header('Content-Type: application/json; charset=utf-8');
|
header('Content-Type: application/json; charset=utf-8');
|
||||||
|
// Converts PHP-Array to JSON-String
|
||||||
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
echo json_encode($data, JSON_UNESCAPED_UNICODE);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
@@ -23,8 +27,8 @@ function json_response($data, $status_code = 200) {
|
|||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Error Response
|
// Error Response
|
||||||
// Sends a standardized Error Response with a Message and HTTP Status
|
// Creates standardized Error Responses with Error Message and HTTP Status
|
||||||
// Code. Uses json_response() internally for consistent Formatting.
|
// Code. Uses json_response() for consistent Formatting.
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
function error_response($message, $status_code = 400) {
|
function error_response($message, $status_code = 400) {
|
||||||
json_response(['error' => $message], $status_code);
|
json_response(['error' => $message], $status_code);
|
||||||
@@ -33,7 +37,7 @@ function error_response($message, $status_code = 400) {
|
|||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Validate Required Fields
|
// Validate Required Fields
|
||||||
// Checks if all specified Fields exist in the given Data Array and are
|
// Checks if specified Fields exist in the given Data Array and are
|
||||||
// non-empty. Returns an Array of missing Field Names, or an empty
|
// non-empty. Returns an Array of missing Field Names, or an empty
|
||||||
// Array if all Fields are present.
|
// Array if all Fields are present.
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
@@ -41,28 +45,28 @@ function validate_required($data, $fields) {
|
|||||||
$missing = [];
|
$missing = [];
|
||||||
|
|
||||||
foreach ($fields as $field) {
|
foreach ($fields as $field) {
|
||||||
|
// Checks if Fields exists in Data Array and are not empty
|
||||||
if (!isset($data[$field]) || trim($data[$field]) === '') {
|
if (!isset($data[$field]) || trim($data[$field]) === '') {
|
||||||
$missing[] = $field;
|
$missing[] = $field;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// Returns Array of missing Fields or emty Array
|
||||||
return $missing;
|
return $missing;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Get POST Input
|
// Get POST Input
|
||||||
// Reads and trims all POST Parameters. Returns an associative Array.
|
// Reads POST Parameters. Returns an associative Array.
|
||||||
// Falls back to JSON Request Body if no POST Data is present
|
// Fallback to JSON Request Body if no POST Data is present.
|
||||||
// (for Clients that send JSON instead of Form Data).
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
function get_input() {
|
function get_input() {
|
||||||
// Check for standard POST Form Data first
|
// Checks for standard POST Requests
|
||||||
if (!empty($_POST)) {
|
if (!empty($_POST)) {
|
||||||
return array_map('trim', $_POST);
|
return array_map('trim', $_POST);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fall back to JSON Request Body
|
// Fall back for JSON POST Requests
|
||||||
$json = file_get_contents('php://input');
|
$json = file_get_contents('php://input');
|
||||||
$data = json_decode($json, true);
|
$data = json_decode($json, true);
|
||||||
|
|
||||||
@@ -76,8 +80,8 @@ function get_input() {
|
|||||||
|
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
// Get PDO Connection
|
// Get PDO Connection
|
||||||
// Returns the PDO Instance created in init.php. Wrapped in a Function
|
// Returns PDO Instance wrapped in a Function to prevent global
|
||||||
// to avoid global Variable Dependencies in Endpoint Files.
|
// Variable Dependencies in Endpoint Files.
|
||||||
// ---------------------------------------------------------------------
|
// ---------------------------------------------------------------------
|
||||||
function get_db() {
|
function get_db() {
|
||||||
global $pdo;
|
global $pdo;
|
||||||
|
|||||||
Reference in New Issue
Block a user