crud on locations

master
Matt Huntington 7 years ago
parent 09477690f4
commit dd044ce2ca

@ -8,3 +8,12 @@ RewriteCond %{REQUEST_METHOD} ^PUT$
RewriteRule ^people/([0-9]+)$ controllers/people.php?action=update&id=$1 RewriteRule ^people/([0-9]+)$ controllers/people.php?action=update&id=$1
RewriteCond %{REQUEST_METHOD} ^DELETE$ RewriteCond %{REQUEST_METHOD} ^DELETE$
RewriteRule ^people/([0-9]+)$ controllers/people.php?action=delete&id=$1 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

@ -0,0 +1,26 @@
<?php
header('Content-Type: application/json');
include_once __DIR__ . '/../models/location.php';
if($_REQUEST['action'] === 'index'){
echo json_encode(Locations::all());
} else if ($_REQUEST['action'] === 'post'){
$request_body = file_get_contents('php://input');
$body_object = json_decode($request_body);
$newLocation = new Location(null, $body_object->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);
}
?>

@ -8,9 +8,8 @@ if($_REQUEST['action'] === 'index'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');
$body_object = json_decode($request_body); $body_object = json_decode($request_body);
$newPerson = new Person(null, $body_object->name, $body_object->age); $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); echo json_encode($allPeople);
} else if ($_REQUEST['action'] === 'update'){ } else if ($_REQUEST['action'] === 'update'){
$request_body = file_get_contents('php://input'); $request_body = file_get_contents('php://input');

@ -0,0 +1,59 @@
<?php
$dbconn = pg_connect("host=localhost dbname=contacts2");
class Location {
public $id;
public $street;
public $city;
public $state;
public function __construct($id, $street, $city, $state) {
$this->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;
}
}
?>

@ -16,7 +16,7 @@ class People {
$query = "INSERT INTO people (name, age) VALUES ($1, $2)"; $query = "INSERT INTO people (name, age) VALUES ($1, $2)";
$query_params = array($person->name, $person->age); $query_params = array($person->name, $person->age);
pg_query_params($query, $query_params); pg_query_params($query, $query_params);
return self::all(); //find all people and return them return self::all();
} }
static function delete($id){ static function delete($id){
$query = "DELETE FROM people WHERE id = $1"; $query = "DELETE FROM people WHERE id = $1";
@ -33,10 +33,8 @@ class People {
return self::all(); return self::all();
} }
static function all(){ static function all(){
//create an empty array
$people = array(); $people = array();
//query the database
$results = pg_query("SELECT * FROM people"); $results = pg_query("SELECT * FROM people");
$row_object = pg_fetch_object($results); $row_object = pg_fetch_object($results);
@ -47,7 +45,7 @@ class People {
$row_object->name, $row_object->name,
intval($row_object->age) intval($row_object->age)
); );
$people[] = $new_person; //push new person object onto $people array $people[] = $new_person;
$row_object = pg_fetch_object($results); $row_object = pg_fetch_object($results);
} }

Loading…
Cancel
Save