finishing crud on locations

master
Matt Huntington 8 years ago
parent d55f5ff35b
commit c7b4db9332

@ -1,26 +1,26 @@
<?php
header('Content-Type: application/json');
include_once __DIR__ . '/../models/job.php';
include_once __DIR__ . '/../models/location.php';
if($_REQUEST['action'] === 'index'){
echo json_encode(Jobs::find());
echo json_encode(Locations::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);
$newLocation = new Location(null, $body->street, $body->city, $body->state);
$allJobs = Jobs::create($newJob);
$allLocations = Locations::create($newLocation);
echo json_encode($allJobs);
echo json_encode($allLocations);
} else if ($_REQUEST['action'] === 'delete'){
$allJobs = Jobs::delete($_REQUEST['id']);
echo json_encode($allJobs);
$allLocations = Locations::delete($_REQUEST['id']);
echo json_encode($allLocations);
} 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);
$updatedLocation = new Location(null, $body->street, $body->city, $body->state);
$allLocations = Locations::update($_REQUEST['id'], $updatedLocation);
echo json_encode($allJobs);
echo json_encode($allLocations);
}

@ -1 +1 @@
INSERT INTO jobs (person_id, job_type, company_id) VALUES ($1, $2, $3)
INSERT INTO locations (street, city, state) VALUES ($1, $2, $3)

@ -1 +1 @@
DELETE FROM jobs WHERE id = $1
DELETE FROM locations WHERE id = $1

@ -1 +1 @@
SELECT * FROM jobs
SELECT * FROM locations

@ -1 +1 @@
UPDATE jobs SET person_id = $1, job_type = $2, company_id = $3 WHERE id = $4
UPDATE locations SET street = $1, city = $2, state = $3 WHERE id = $4

@ -1,47 +1,45 @@
<?
include_once __DIR__ . '/../database/db.php';
class Job {
class Location {
public $id;
public $job_type;
public function __construct($id, $person_id, $job_type, $company_id) {
public $street;
public $city;
public $state;
public function __construct($id, $street, $city, $state) {
$this->id = $id;
if($person_id){
$this->person_id = $person_id;
}
$this->job_type = $job_type;
if($company_id){
$this->company_id = $company_id;
}
$this->street = $street;
$this->city = $city;
$this->state = $state;
}
}
class Jobs {
class Locations {
static function find(){
$query = file_get_contents(__DIR__ . '/../database/sql/jobs/find.sql');
$query = file_get_contents(__DIR__ . '/../database/sql/locations/find.sql');
$result = pg_query($query);
$jobs = array();
$locations = array();
while($data = pg_fetch_object($result)){
$jobs[] = new Job(intval($data->id), intval($data->person_id), $data->job_type, intval($data->company_id));
$locations[] = new Location(intval($data->id), $data->street, $data->city, $data->state);
}
return $jobs;
return $locations;
}
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));
static function create($location){
$query = file_get_contents(__DIR__ . '/../database/sql/locations/create.sql');
$result = pg_query_params($query, array($location->street, $location->city, $location->state));
return self::find();
}
static function delete($id){
$query = file_get_contents(__DIR__ . '/../database/sql/jobs/delete.sql');
$query = file_get_contents(__DIR__ . '/../database/sql/locations/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));
static function update($id, $updatedLocation){
$query = file_get_contents(__DIR__ . '/../database/sql/locations/update.sql');
$result = pg_query_params($query, array($updatedLocation->street, $updatedLocation->city, $updatedLocation->state, $id));
return self::find();
}

Loading…
Cancel
Save