RUD on jobs

master
Matt Huntington 8 years ago
parent 76366695e0
commit d7480b6295

@ -21,5 +21,11 @@ RewriteCond %{REQUEST_METHOD} ^PUT$
RewriteRule ^companies/([0-9]+)$ controllers/companies.php?action=update&id=$1 RewriteRule ^companies/([0-9]+)$ controllers/companies.php?action=update&id=$1
# jobs routes # jobs routes
RewriteCond %{REQUEST_METHOD} ^GET$
RewriteRule ^jobs$ controllers/jobs.php?action=index
RewriteCond %{REQUEST_METHOD} ^POST$ RewriteCond %{REQUEST_METHOD} ^POST$
RewriteRule ^jobs$ controllers/jobs.php?action=post RewriteRule ^jobs$ controllers/jobs.php?action=post
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

@ -2,7 +2,9 @@
header('Content-Type: application/json'); header('Content-Type: application/json');
include_once __DIR__ . '/../models/job.php'; include_once __DIR__ . '/../models/job.php';
if ($_REQUEST['action'] === 'post'){ if($_REQUEST['action'] === 'index'){
echo json_encode(Jobs::find());
} else if ($_REQUEST['action'] === 'post'){
$requestBody = file_get_contents('php://input'); $requestBody = file_get_contents('php://input');
$body = json_decode($requestBody); $body = json_decode($requestBody);
@ -10,5 +12,15 @@ if ($_REQUEST['action'] === 'post'){
$allJobs = Jobs::create($newJob); $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->company_id);
$allJobs = Jobs::update($_REQUEST['id'], $updatedJob);
echo json_encode($allJobs); echo json_encode($allJobs);
} }

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

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

@ -27,6 +27,18 @@ class Jobs {
$query = file_get_contents(__DIR__ . '/../database/sql/jobs/create.sql'); $query = file_get_contents(__DIR__ . '/../database/sql/jobs/create.sql');
$result = pg_query_params($query, array($job->person_id, $job->company_id)); $result = pg_query_params($query, array($job->person_id, $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->company_id, $id));
return self::find(); return self::find();
} }
} }

Loading…
Cancel
Save