diff --git a/controllers/people.php b/controllers/people.php index d4d696a..535ed24 100644 --- a/controllers/people.php +++ b/controllers/people.php @@ -3,25 +3,24 @@ include __DIR__ . '/../data/people.php'; if($_REQUEST['action'] === 'index'){ - echo json_encode($people); + echo json_encode(People::find()); } else if ($_REQUEST['action'] === 'post'){ $requestBody = file_get_contents('php://input'); $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'){ - array_splice($people, $_REQUEST['id'], 1); - echo json_encode($people); + $allPeople = People::delete($_REQUEST['id']); + echo json_encode($allPeople); } else if ($_REQUEST['action'] === 'update'){ $requestBody = file_get_contents('php://input'); $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($people); + echo json_encode($allPeople); } ?> diff --git a/models/person.php b/models/person.php index 3908b2d..b3c7db9 100644 --- a/models/person.php +++ b/models/person.php @@ -7,4 +7,47 @@ class Person { $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; + } +} ?>