Merge pull request #27 from jbrooksuk/api-improvements

Incidents API
This commit is contained in:
James Brooks 2014-11-25 10:36:26 +00:00
commit 0c62f0534a
6 changed files with 47 additions and 4 deletions

View File

@ -19,4 +19,16 @@
return $component->incidents;
}
public function getIncidents() {
return Incident::all();
}
public function getIncident($id) {
if ($incident = Incident::find($id)) {
return $incident;
} else {
App::abort(404, 'Incident not found');
}
}
}

View File

@ -20,7 +20,7 @@
/**
* Get the transformer instance.
*
* @return mixed
* @return ComponentTransformer
*/
public function getTransformer() {
return new ComponentTransformer();

View File

@ -1,6 +1,6 @@
<?php
class Incident extends Eloquent {
class Incident extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
/**
* An incident belongs to a component.
* @return Illuminate\Database\Eloquent\Relations\BelongsTo
@ -29,4 +29,13 @@
case 4: return 'glyphicon-ok';
}
}
/**
* Get the transformer instance.
*
* @return IncidentTransformer
*/
public function getTransformer() {
return new IncidentTransformer();
}
}

View File

@ -5,5 +5,7 @@
Route::get('components', 'ApiController@getComponents');
Route::get('components/{id}', 'ApiController@getComponent');
Route::get('components/{id}/incidents', 'ApiController@getComponentIncidents');
Route::get('incidents', 'ApiController@getIncidents');
Route::get('incidents/{id}', 'ApiController@getIncident');
});

View File

@ -8,8 +8,9 @@
'description' => $component->description,
'status_id' => (int) $component->status,
'status' => $component->getHumanStatusAttribute(),
'incident_count' => $component->incidents()->count()
'incident_count' => $component->incidents()->count(),
'created_at' => $component->created_at->timestamp,
'updated_at' => $component->updated_at->timestamp,
];
}
}

View File

@ -0,0 +1,19 @@
<?php
class IncidentTransformer extends \League\Fractal\TransformerAbstract {
public function transform(Incident $incident) {
$component = $incident->parent;
$transformer = $component->getTransformer();
return [
'id' => (int) $incident->id,
'name' => $incident->name,
'message' => $incident->message,
'status_id' => (int) $incident->status,
'status' => $incident->getHumanStatusAttribute(),
'component' => $transformer->transform($component),
'created_at' => $incident->created_at->timestamp,
'updated_at' => $incident->updated_at->timestamp,
];
}
}