Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
/.idea/
composer.lock
21 changes: 21 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"name": "andre/teste",
"type": "project",
"require": {
"silex/silex": "^2.2",
"illuminate/database": "^5.4",
"illuminate/events": "^5.4"
},
"authors": [
{
"name": "Andre Luis Zipf",
"email": "andrezipf94@github.com"
}
],
"autoload": {
"psr-4": {
"Symfony\\Component\\Routing\\": ""
},
"classmap": ["model/"]
}
}
11 changes: 11 additions & 0 deletions model/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* @author Andre Luis Zipf (andrezipf94 at github dot com)
* @since 05/08/17
*/
namespace Model;
class Job extends \Illuminate\Database\Eloquent\Model
{
protected $primaryKey = "idjob";
protected $fillable = ["name", "description", "requirements", "initial_salary"];
}
20 changes: 20 additions & 0 deletions public/.htaccess
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>

RewriteEngine On

# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]

# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
160 changes: 160 additions & 0 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php
define("APP_ROOT", dirname(__DIR__));
chdir(APP_ROOT);
require_once "vendor/autoload.php";

use Silex\Application;

use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Events\Dispatcher;
use Illuminate\Container\Container;
use Symfony\Component\HttpFoundation\Request;

$app = new Application();
$app["debug"] = true;

// Setup DB Connectivity through Illuminate\Database
$capsule = new Capsule();
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'app',
'username' => 'andre',
'password' => '7035489',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
'prefix' => '',
]);

// Set the event dispatcher used by Eloquent models
$capsule->setEventDispatcher(new Dispatcher(new Container));
// Make this Capsule instance available globally via static methods
$capsule->setAsGlobal();
// Setup the Eloquent ORM
$capsule->bootEloquent();

$app->before(function() use ($app) {
// Check whether there is auth data or not in the request
if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] == "") {
header('WWW-Authenticate: Basic realm=\'TestApi\'');
return $app->json(['Message' => 'Not Authorised'], 401);
} else {

// If there is, check whether the user is allowed to proceed.
$users = [
'andre' => '1234'
];

if($users[$_SERVER['PHP_AUTH_USER']] !== $_SERVER['PHP_AUTH_PW']) {
return $app->json(['Message' => 'Forbidden'], 403);
}
}
});

$app->get("/", function(Application $app) {
return $app->json(["API Running"]);
});

/** Get all jobs */
$app->get("/jobs", function() use ($app) {
return $app->json(Model\Job::all()->toArray());
});

/** Get a job */
$app->get("/jobs/{id}", function($id) use ($app) {
return $app->json(Model\Job::find($id));
})->convert("id", function($id) {
return (int) $id;
});

/** Insert a job */
$app->post("/jobs", function(Request $request) use ($app) {
$required_attr = ["name", "description", "requirements", "initial_salary"];
$data = json_decode($request->getContent());
$job = new Model\Job();

foreach($required_attr as $attr) {
if($data->$attr != "") {
$job->$attr = $data->$attr;
} else {
return $app->json([
"status" => 0,
"message" => "Required parameter \"{$attr}\" not informed."
]);
}
}

if($job->save()) {
return $app->json([
"status" => 1,
"message" => "Job successfully added."
]);
} else {
return $app->json([
"status" => 0,
"message" => "Failed to add the job."
]);
}
});

/** Update a job */
$app->put("/jobs", function(Request $request) use ($app) {
$required_attr = ["name", "description", "requirements", "initial_salary"];
$data = json_decode($request->getContent());

if($data->id != "") {
$job = Model\Job::find($data->id);
} else {
return $app->json([
"status" => 0,
"message" => "Required parameter 'id' not informed."
]);
}

if(!($job instanceof Model\Job)) {
return $app->json([
"status" => 0,
"message" => "No Job could be found with the informed id."
]);
}

foreach($required_attr as $attr) {
if ($data->$attr != "") {
$job->$attr = $data->$attr;
} else {
return $app->json([
"status" => 0,
"message" => "Required parameter '{$attr}' not informed."
]);
}
}

if ($job->update()) {
return $app->json([
"status" => 1,
"message" => "Job successfully updated."
]);
} else {
return $app->json([
"status" => 0,
"message" => "Failed to update the job."
]);
}
});

/** Delete a job */
$app->delete("/jobs/{id}", function($id) use ($app) {
if(Model\Job::destroy($id)) {
return $app->json([
"status" => 1,
"message" => "Job successfully deleted."
]);
} else {
return $app->json([
"status" => 0,
"message" => "Failed to delete the job."
]);
}
});

$app->run();
11 changes: 11 additions & 0 deletions setup.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
create schema app;
use app;
create table jobs (
idjob int auto_increment primary key,
name varchar(100) not null,
description text,
requirements text,
initial_salary double not null,
updated_at timestamp not null,
created_at timestamp not null
);