Skip to content

Instantly share code, notes, and snippets.

@pkavoo
Created May 18, 2019 09:10
Show Gist options
  • Save pkavoo/2be0c1cb9302e23839be4d5543632cf1 to your computer and use it in GitHub Desktop.
Save pkavoo/2be0c1cb9302e23839be4d5543632cf1 to your computer and use it in GitHub Desktop.
<?php
//getting the dboperation class
require_once '../includes/DbOperation.php';
//function validating all the paramters are available
//we will pass the required parameters to this function
function isTheseParametersAvailable($params){
//assuming all parameters are available
$available = true;
$missingparams = "";
foreach($params as $param){
if(!isset($_POST[$param]) || strlen($_POST[$param])<=0){
$available = false;
$missingparams = $missingparams . ", " . $param;
}
}
//if parameters are missing
if(!$available){
$response = array();
$response['error'] = true;
$response['message'] = 'Parameters ' . substr($missingparams, 1, strlen($missingparams)) . ' missing';
//displaying error
echo json_encode($response);
//stopping further execution
die();
}
}
//an array to display response
$response = array();
//if it is an api call
//that means a get parameter named api call is set in the URL
//and with this parameter we are concluding that it is an api call
if(isset($_GET['apicall'])){
switch($_GET['apicall']){
//the CREATE operation
//if the api call value is 'createhero'
//we will create a record in the database
case 'createhero':
//first check the parameters required for this request are available or not
isTheseParametersAvailable(array('name','realname','rating','teamaffiliation'));
//creating a new dboperation object
$db = new DbOperation();
//creating a new record in the database
$result = $db->createHero(
$_POST['name'],
$_POST['realname'],
$_POST['rating'],
$_POST['teamaffiliation']
);
//if the record is created adding success to response
if($result){
//record is created means there is no error
$response['error'] = false;
//in message we have a success message
$response['message'] = 'Hero addedd successfully';
//and we are getting all the heroes from the database in the response
$response['heroes'] = $db->getHeroes();
}else{
//if record is not added that means there is an error
$response['error'] = true;
//and we have the error message
$response['message'] = 'Some error occurred please try again';
}
break;
//the READ operation
//if the call is getheroes
case 'getheroes':
$db = new DbOperation();
$response['error'] = false;
$response['message'] = 'Request successfully completed';
$response['heroes'] = $db->getHeroes();
break;
//the UPDATE operation
case 'updatehero':
isTheseParametersAvailable(array('id','name','realname','rating','teamaffiliation'));
$db = new DbOperation();
$result = $db->updateHero(
$_POST['id'],
$_POST['name'],
$_POST['realname'],
$_POST['rating'],
$_POST['teamaffiliation']
);
if($result){
$response['error'] = false;
$response['message'] = 'Hero updated successfully';
$response['heroes'] = $db->getHeroes();
}else{
$response['error'] = true;
$response['message'] = 'Some error occurred please try again';
}
break;
//the delete operation
case 'deletehero':
//for the delete operation we are getting a GET parameter from the url having the id of the record to be deleted
if(isset($_GET['id'])){
$db = new DbOperation();
if($db->deleteHero($_GET['id'])){
$response['error'] = false;
$response['message'] = 'Hero deleted successfully';
$response['heroes'] = $db->getHeroes();
}else{
$response['error'] = true;
$response['message'] = 'Some error occurred please try again';
}
}else{
$response['error'] = true;
$response['message'] = 'Nothing to delete, provide an id please';
}
break;
}
}else{
//if it is not api call
//pushing appropriate values to response array
$response['error'] = true;
$response['message'] = 'Invalid API Call';
}
//displaying the response in json structure
echo json_encode($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment