Skip to content

Commit

Permalink
Merge pull request #29 from jbrooksuk/write-api-started
Browse files Browse the repository at this point in the history
Write api started
  • Loading branch information
jbrooksuk committed Nov 25, 2014
2 parents 3202f02 + a4d3928 commit 2c58ff8
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 2 deletions.
100 changes: 100 additions & 0 deletions app/controllers/ApiController.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
<?php

class ApiController extends \Dingo\Api\Routing\Controller {
protected $auth;

public function __construct(Dingo\Api\Auth\Shield $auth) {
$this->auth = $auth;
}

/**
* Get all components
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getComponents() {
return Component::all();
}

/**
* Get a single component
*
* @param int $id
*
* @return Component
*/
public function getComponent($id) {
if ($component = Component::find($id)) {
return $component;
Expand All @@ -19,10 +36,42 @@ public function getComponentIncidents($id) {
return $component->incidents;
}

/**
* Create a new component
*
* @return Component
*/
public function postComponents() {
$component = new Component(Input::all());
$component->user_id = $this->auth->user()->id;
if ($component->isValid()) {
try {
$component->saveOrFail();
return $component;
} catch (Exception $e) {
App::abort(500, $e->getMessage());
}
} else {
App::abort(404, $component->getErrors()->first());
}
}

/**
* Get all incidents
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getIncidents() {
return Incident::all();
}

/**
* Get a single incident
*
* @param int $id
*
* @return Incident
*/
public function getIncident($id) {
if ($incident = Incident::find($id)) {
return $incident;
Expand All @@ -31,4 +80,55 @@ public function getIncident($id) {
}
}

/**
* Create a new incident
*
* @return Incident
*/
public function postIncidents() {
$incident = new Incident(Input::all());
$incident->user_id = $this->auth->user()->id;
return $this->saveIncident($incident);
}

/**
* Update an existing incident
*
* @param int $id
*
* @return Incident
*/
public function putIncident($id) {
$incident = $this->getIncident($id);

$incident->fill(Input::all());

return $this->saveIncident($incident);
}

/**
* Function for saving the incident, and returning appropriate error codes
*
* @param Incident $incident
*
* @return Incident
*/
private function saveIncident($incident) {
if ($incident->isValid()) {
try {
$component = $incident->parent;
if (!$component) {
App::abort(400, 'Invalid component specified');
}

$incident->saveOrFail();
return $incident;
} catch (Exception $e) {
App::abort(500, $e->getMessage());
}
} else {
App::abort(404, $incident->getErrors()->first());
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UserIdColumnForComponents extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('components', function(Blueprint $table)
{
$table->unsignedInteger('user_id')->nullable();

$table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('components', function(Blueprint $table)
{
$table->dropColumn('user_id');
});
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class UserIdColumnForIncidents extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('incidents', function(Blueprint $table)
{
$table->unsignedInteger('user_id')->nullable();

$table->foreign('user_id')->references('id')->on('users')->onDelete('SET NULL')->onUpdate('NO ACTION');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('incidents', function(Blueprint $table)
{
$table->dropColumn('user_id');
});
}

}
12 changes: 12 additions & 0 deletions app/models/Component.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
<?php

use Watson\Validating\ValidatingTrait;

class Component extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
use ValidatingTrait;

protected $rules = [
'user_id' => 'required|integer',
'name' => 'required',
'status' => 'required|integer'
];

protected $fillable = ['name', 'description', 'status'];

/**
* Lookup all of the incidents reported on the component.
* @return Illuminate\Database\Eloquent\Relations
Expand Down
15 changes: 15 additions & 0 deletions app/models/Incident.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,21 @@
<?php

use Watson\Validating\ValidatingTrait;

class Incident extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
use ValidatingTrait;
use \Illuminate\Database\Eloquent\SoftDeletingTrait;

protected $rules = [
'user_id' => 'required|integer',
'component' => 'required|integer',
'name' => 'required',
'status' => 'required|integer',
'message' => 'required',
];

protected $fillable = ['component', 'name', 'status', 'message'];

/**
* An incident belongs to a component.
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
Expand Down
7 changes: 7 additions & 0 deletions app/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,11 @@
Route::get('incidents', 'ApiController@getIncidents');
Route::get('incidents/{id}', 'ApiController@getIncident');

Route::group(['protected' => true], function() {
Route::post('components', 'ApiController@postComponents');
Route::post('incidents', 'ApiController@postIncidents');

Route::put('incidents/{id}', 'ApiController@putIncident');
});

});
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"require": {
"laravel/framework": "4.2.*",
"guzzlehttp/guzzle": "4.*",
"dingo/api": "~0.6"
"dingo/api": "~0.6",
"watson/validating": "0.10.*"
},
"autoload": {
"classmap": [
Expand Down
51 changes: 50 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2c58ff8

Please sign in to comment.