diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..d851bdbf --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +/vendor/ +/.idea/ +composer.lock \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 00000000..899ff829 --- /dev/null +++ b/composer.json @@ -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/"] + } +} diff --git a/model/Job.php b/model/Job.php new file mode 100644 index 00000000..8c26b747 --- /dev/null +++ b/model/Job.php @@ -0,0 +1,11 @@ + + + Options -MultiViews + + + 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}] + diff --git a/public/index.php b/public/index.php new file mode 100644 index 00000000..f0296f63 --- /dev/null +++ b/public/index.php @@ -0,0 +1,160 @@ +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(); \ No newline at end of file diff --git a/setup.sql b/setup.sql new file mode 100644 index 00000000..b6b5ce1d --- /dev/null +++ b/setup.sql @@ -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 +); \ No newline at end of file