static find, update, delete, create

master
Matt Huntington 8 years ago
parent 0d3511c406
commit 93f34ac85c

@ -3,25 +3,24 @@
include __DIR__ . '/../data/people.php'; include __DIR__ . '/../data/people.php';
if($_REQUEST['action'] === 'index'){ if($_REQUEST['action'] === 'index'){
echo json_encode($people); echo json_encode(People::find());
} else if ($_REQUEST['action'] === 'post'){ } else if ($_REQUEST['action'] === 'post'){
$requestBody = file_get_contents('php://input'); $requestBody = file_get_contents('php://input');
$body = json_decode($requestBody); $body = json_decode($requestBody);
$zagthar = new Person($body->name, $body->age); $newPerson = new Person($body->name, $body->age);
$people[] = $zagthar; $allPeople = People::create($newPerson);
echo json_encode($people); echo json_encode($allPeople);
} else if ($_REQUEST['action'] === 'delete'){ } else if ($_REQUEST['action'] === 'delete'){
array_splice($people, $_REQUEST['id'], 1); $allPeople = People::delete($_REQUEST['id']);
echo json_encode($people); echo json_encode($allPeople);
} else if ($_REQUEST['action'] === 'update'){ } else if ($_REQUEST['action'] === 'update'){
$requestBody = file_get_contents('php://input'); $requestBody = file_get_contents('php://input');
$body = json_decode($requestBody); $body = json_decode($requestBody);
$zagthar = new Person($body->name, $body->age); $updatedPerson = new Person($body->name, $body->age);
$allPeople = People::update($_REQUEST['id'], $updatedPerson);
$people[$_REQUEST['id']] = $zagthar; echo json_encode($allPeople);
echo json_encode($people);
} }
?> ?>

@ -7,4 +7,47 @@ class Person {
$this->age = $age; $this->age = $age;
} }
} }
class People {
static function find(){
$people = array();
$people[] = new Person('joni', 52);
$people[] = new Person('bob', 34);
$people[] = new Person('sally', 21);
$people[] = new Person('matt', 37);
return $people;
}
static function add($person){
$people = array();
$people[] = new Person('joni', 52);
$people[] = new Person('bob', 34);
$people[] = new Person('sally', 21);
$people[] = new Person('matt', 37);
$people[] = $person;
return $people;
}
static function delete($index){
$people = array();
$people[] = new Person('joni', 52);
$people[] = new Person('bob', 34);
$people[] = new Person('sally', 21);
$people[] = new Person('matt', 37);
array_splice($people, $index, 1);
return $people;
}
static function update($index, $updatedPerson){
$people = array();
$people[] = new Person('joni', 52);
$people[] = new Person('bob', 34);
$people[] = new Person('sally', 21);
$people[] = new Person('matt', 37);
$people[$index] = $updatedPerson;
return $people;
}
}
?> ?>

Loading…
Cancel
Save