Create components and incidents from the API

This commit is contained in:
manavo
2014-11-25 11:06:28 +00:00
parent 0c62f0534a
commit e104a9a317
6 changed files with 140 additions and 2 deletions
+58
View File
@@ -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;
@@ -19,10 +36,37 @@
return $component->incidents;
}
/**
* Create a new component
*
* @return Component
*/
public function postComponents() {
$component = new Component(Input::all());
if ($component->isValid()) {
$component->saveOrFail();
return $component;
} 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;
@@ -31,4 +75,18 @@
}
}
/**
* Create a new incident
*
* @return Incident
*/
public function postIncidents() {
$incident = new Incident(Input::all());
if ($incident->isValid()) {
$incident->saveOrFail();
return $incident;
} else {
App::abort(404, $incident->getErrors()->first());
}
}
}
+11
View File
@@ -1,6 +1,17 @@
<?php
use Watson\Validating\ValidatingTrait;
class Component extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
use ValidatingTrait;
protected $rules = [
'name' => 'required',
'status' => 'required|integer'
];
protected $fillable = ['name', 'description', 'status'];
/**
* Lookup all of the incidents reported on the component.
* @return Illuminate\Database\Eloquent\Relations
+14
View File
@@ -1,6 +1,20 @@
<?php
use Watson\Validating\ValidatingTrait;
class Incident extends Eloquent implements Dingo\Api\Transformer\TransformableInterface {
use ValidatingTrait;
use \Illuminate\Database\Eloquent\SoftDeletingTrait;
protected $rules = [
'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
+5
View File
@@ -8,4 +8,9 @@
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');
});
});