diff --git a/.htaccess b/.htaccess index e4650f9..4d7d420 100644 --- a/.htaccess +++ b/.htaccess @@ -29,3 +29,13 @@ RewriteCond %{REQUEST_METHOD} ^DELETE$ RewriteRule ^jobs/([0-9]+)$ controllers/jobs.php?action=delete&id=$1 RewriteCond %{REQUEST_METHOD} ^PUT$ RewriteRule ^jobs/([0-9]+)$ controllers/jobs.php?action=update&id=$1 + +# location routes +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} ^DELETE$ +RewriteRule ^locations/([0-9]+)$ controllers/locations.php?action=delete&id=$1 +RewriteCond %{REQUEST_METHOD} ^PUT$ +RewriteRule ^locations/([0-9]+)$ controllers/locations.php?action=update&id=$1 diff --git a/controllers/locations.php b/controllers/locations.php new file mode 100644 index 0000000..b9b1b66 --- /dev/null +++ b/controllers/locations.php @@ -0,0 +1,26 @@ +person_id, $body->job_type, $body->company_id); + + $allJobs = Jobs::create($newJob); + + echo json_encode($allJobs); +} else if ($_REQUEST['action'] === 'delete'){ + $allJobs = Jobs::delete($_REQUEST['id']); + echo json_encode($allJobs); +} else if ($_REQUEST['action'] === 'update'){ + $requestBody = file_get_contents('php://input'); + $body = json_decode($requestBody); + $updatedJob = new Job(null, $body->person_id, $body->job_type, $body->company_id); + $allJobs = Jobs::update($_REQUEST['id'], $updatedJob); + + echo json_encode($allJobs); +} diff --git a/database/sql/locations/create.sql b/database/sql/locations/create.sql new file mode 100644 index 0000000..546737f --- /dev/null +++ b/database/sql/locations/create.sql @@ -0,0 +1 @@ +INSERT INTO jobs (person_id, job_type, company_id) VALUES ($1, $2, $3) diff --git a/database/sql/locations/delete.sql b/database/sql/locations/delete.sql new file mode 100644 index 0000000..837e636 --- /dev/null +++ b/database/sql/locations/delete.sql @@ -0,0 +1 @@ +DELETE FROM jobs WHERE id = $1 diff --git a/database/sql/locations/find.sql b/database/sql/locations/find.sql new file mode 100644 index 0000000..898df78 --- /dev/null +++ b/database/sql/locations/find.sql @@ -0,0 +1 @@ +SELECT * FROM jobs diff --git a/database/sql/locations/update.sql b/database/sql/locations/update.sql new file mode 100644 index 0000000..1d9c94e --- /dev/null +++ b/database/sql/locations/update.sql @@ -0,0 +1 @@ +UPDATE jobs SET person_id = $1, job_type = $2, company_id = $3 WHERE id = $4 diff --git a/models/location.php b/models/location.php new file mode 100644 index 0000000..9d66df1 --- /dev/null +++ b/models/location.php @@ -0,0 +1,49 @@ +id = $id; + if($person_id){ + $this->person_id = $person_id; + } + $this->job_type = $job_type; + if($company_id){ + $this->company_id = $company_id; + } + } +} + +class Jobs { + static function find(){ + $query = file_get_contents(__DIR__ . '/../database/sql/jobs/find.sql'); + $result = pg_query($query); + $jobs = array(); + while($data = pg_fetch_object($result)){ + $jobs[] = new Job(intval($data->id), intval($data->person_id), $data->job_type, intval($data->company_id)); + } + + return $jobs; + } + static function create($job){ + $query = file_get_contents(__DIR__ . '/../database/sql/jobs/create.sql'); + $result = pg_query_params($query, array($job->person_id, $job->job_type, $job->company_id)); + + return self::find(); + } + static function delete($id){ + $query = file_get_contents(__DIR__ . '/../database/sql/jobs/delete.sql'); + $result = pg_query_params($query, array($id)); + + return self::find(); + } + static function update($id, $updatedJob){ + $query = file_get_contents(__DIR__ . '/../database/sql/jobs/update.sql'); + $result = pg_query_params($query, array($updatedJob->person_id, $updatedJob->job_type, $updatedJob->company_id, $id)); + + return self::find(); + } +} +?>