setting up files for locations

master
Matt Huntington 8 years ago
parent 25eab6a92d
commit d55f5ff35b

@ -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

@ -0,0 +1,26 @@
<?php
header('Content-Type: application/json');
include_once __DIR__ . '/../models/job.php';
if($_REQUEST['action'] === 'index'){
echo json_encode(Jobs::find());
} else if ($_REQUEST['action'] === 'post'){
$requestBody = file_get_contents('php://input');
$body = json_decode($requestBody);
$newJob = new Job(null, $body->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);
}

@ -0,0 +1 @@
INSERT INTO jobs (person_id, job_type, company_id) VALUES ($1, $2, $3)

@ -0,0 +1 @@
DELETE FROM jobs WHERE id = $1

@ -0,0 +1 @@
SELECT * FROM jobs

@ -0,0 +1 @@
UPDATE jobs SET person_id = $1, job_type = $2, company_id = $3 WHERE id = $4

@ -0,0 +1,49 @@
<?
include_once __DIR__ . '/../database/db.php';
class Job {
public $id;
public $job_type;
public function __construct($id, $person_id, $job_type, $company_id) {
$this->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();
}
}
?>
Loading…
Cancel
Save