From dd044ce2ca12430cee9fca13018b1ac85f488b89 Mon Sep 17 00:00:00 2001 From: Matt Huntington Date: Sat, 26 Jan 2019 16:35:31 -0500 Subject: [PATCH] crud on locations --- .htaccess | 9 ++++++ controllers/locations.php | 26 +++++++++++++++++ controllers/people.php | 3 +- models/location.php | 59 +++++++++++++++++++++++++++++++++++++++ models/person.php | 6 ++-- 5 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 controllers/locations.php create mode 100644 models/location.php diff --git a/.htaccess b/.htaccess index ae1ce6e..f890977 100644 --- a/.htaccess +++ b/.htaccess @@ -8,3 +8,12 @@ RewriteCond %{REQUEST_METHOD} ^PUT$ RewriteRule ^people/([0-9]+)$ controllers/people.php?action=update&id=$1 RewriteCond %{REQUEST_METHOD} ^DELETE$ RewriteRule ^people/([0-9]+)$ controllers/people.php?action=delete&id=$1 + +RewriteCond %{REQUEST_METHOD} ^GET$ +RewriteRule ^locations$ controllers/locations.php?action=index +RewriteCond %{REQUEST_METHOD} ^POST$ +RewriteRule ^locations$ controllers/locations.php?action=post +RewriteCond %{REQUEST_METHOD} ^PUT$ +RewriteRule ^locations/([0-9]+)$ controllers/locations.php?action=update&id=$1 +RewriteCond %{REQUEST_METHOD} ^DELETE$ +RewriteRule ^locations/([0-9]+)$ controllers/locations.php?action=delete&id=$1 diff --git a/controllers/locations.php b/controllers/locations.php new file mode 100644 index 0000000..272870e --- /dev/null +++ b/controllers/locations.php @@ -0,0 +1,26 @@ +street, $body_object->city, $body_object->state); + $allLocations = Locations::create($newLocation); + + echo json_encode($allLocations); +} else if ($_REQUEST['action'] === 'update'){ + $request_body = file_get_contents('php://input'); + $body_object = json_decode($request_body); + $updatedLocation = new Location($_REQUEST['id'], $body_object->street, $body_object->city, $body_object->state); + $allLocations = Locations::update($updatedLocation); + + echo json_encode($allLocations); +} else if ($_REQUEST['action'] === 'delete'){ + $allLocations = Locations::delete($_REQUEST['id']); + echo json_encode($allLocations); +} + +?> diff --git a/controllers/people.php b/controllers/people.php index c1eadc5..4954e4c 100644 --- a/controllers/people.php +++ b/controllers/people.php @@ -8,9 +8,8 @@ if($_REQUEST['action'] === 'index'){ $request_body = file_get_contents('php://input'); $body_object = json_decode($request_body); $newPerson = new Person(null, $body_object->name, $body_object->age); - $allPeople = People::create($newPerson); //store the return value of People::create into a var + $allPeople = People::create($newPerson); - //send the return value of People::create (all people in the db) back to the user echo json_encode($allPeople); } else if ($_REQUEST['action'] === 'update'){ $request_body = file_get_contents('php://input'); diff --git a/models/location.php b/models/location.php new file mode 100644 index 0000000..190f40a --- /dev/null +++ b/models/location.php @@ -0,0 +1,59 @@ +id = $id; + $this->street = $street; + $this->city = $city; + $this->state = $state; + } +} +class Locations { + static function create($location){ + $query = "INSERT INTO locations (street, city, state) VALUES ($1, $2, $3)"; + $query_params = array($location->street, $location->city, $location->state); + pg_query_params($query, $query_params); + return self::all(); + } + static function delete($id){ + $query = "DELETE FROM locations WHERE id = $1"; + $query_params = array($id); + $result = pg_query_params($query, $query_params); + + return self::all(); + } + static function update($updatedLocation){ + $query = "UPDATE locations SET street = $1, city = $2, state = $3 WHERE id = $4"; + $query_params = array($updatedLocation->street, $updatedLocation->city, $updatedLocation->state, $updatedLocation->id); + $result = pg_query_params($query, $query_params); + + return self::all(); + } + static function all(){ + $locations = array(); + + $results = pg_query("SELECT * FROM locations"); + + $row_object = pg_fetch_object($results); + while($row_object){ + + $new_location = new Location( + intval($row_object->id), + $row_object->street, + $row_object->city, + $row_object->state + ); + $locations[] = $new_location; + + $row_object = pg_fetch_object($results); + } + + return $locations; + } +} +?> diff --git a/models/person.php b/models/person.php index 40d00a3..6c54d02 100644 --- a/models/person.php +++ b/models/person.php @@ -16,7 +16,7 @@ class People { $query = "INSERT INTO people (name, age) VALUES ($1, $2)"; $query_params = array($person->name, $person->age); pg_query_params($query, $query_params); - return self::all(); //find all people and return them + return self::all(); } static function delete($id){ $query = "DELETE FROM people WHERE id = $1"; @@ -33,10 +33,8 @@ class People { return self::all(); } static function all(){ - //create an empty array $people = array(); - //query the database $results = pg_query("SELECT * FROM people"); $row_object = pg_fetch_object($results); @@ -47,7 +45,7 @@ class People { $row_object->name, intval($row_object->age) ); - $people[] = $new_person; //push new person object onto $people array + $people[] = $new_person; $row_object = pg_fetch_object($results); }