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

View File

@ -1,11 +1,28 @@
<?php <?php
class ApiController extends \Dingo\Api\Routing\Controller { 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() { public function getComponents() {
return Component::all(); return Component::all();
} }
/**
* Get a single component
*
* @param int $id
*
* @return Component
*/
public function getComponent($id) { public function getComponent($id) {
if ($component = Component::find($id)) { if ($component = Component::find($id)) {
return $component; return $component;
@ -19,10 +36,37 @@
return $component->incidents; 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() { public function getIncidents() {
return Incident::all(); return Incident::all();
} }
/**
* Get a single incident
*
* @param int $id
*
* @return Incident
*/
public function getIncident($id) { public function getIncident($id) {
if ($incident = Incident::find($id)) { if ($incident = Incident::find($id)) {
return $incident; 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());
}
}
} }

View File

@ -1,6 +1,17 @@
<?php <?php
use Watson\Validating\ValidatingTrait;
class Component extends Eloquent implements Dingo\Api\Transformer\TransformableInterface { 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. * Lookup all of the incidents reported on the component.
* @return Illuminate\Database\Eloquent\Relations * @return Illuminate\Database\Eloquent\Relations

View File

@ -1,6 +1,20 @@
<?php <?php
use Watson\Validating\ValidatingTrait;
class Incident extends Eloquent implements Dingo\Api\Transformer\TransformableInterface { 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. * An incident belongs to a component.
* @return Illuminate\Database\Eloquent\Relations\BelongsTo * @return Illuminate\Database\Eloquent\Relations\BelongsTo

View File

@ -8,4 +8,9 @@
Route::get('incidents', 'ApiController@getIncidents'); Route::get('incidents', 'ApiController@getIncidents');
Route::get('incidents/{id}', 'ApiController@getIncident'); Route::get('incidents/{id}', 'ApiController@getIncident');
Route::group(['protected' => true], function() {
Route::post('components', 'ApiController@postComponents');
Route::post('incidents', 'ApiController@postIncidents');
});
}); });

View File

@ -7,7 +7,8 @@
"require": { "require": {
"laravel/framework": "4.2.*", "laravel/framework": "4.2.*",
"guzzlehttp/guzzle": "4.*", "guzzlehttp/guzzle": "4.*",
"dingo/api": "~0.6" "dingo/api": "~0.6",
"watson/validating": "0.10.*"
}, },
"autoload": { "autoload": {
"classmap": [ "classmap": [

51
composer.lock generated
View File

@ -4,7 +4,7 @@
"Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file",
"This file is @generated automatically" "This file is @generated automatically"
], ],
"hash": "f1b0b35a64f38b74527150925a720e8d", "hash": "582a0ffe83dfc08436525ef3419162a7",
"packages": [ "packages": [
{ {
"name": "classpreloader/classpreloader", "name": "classpreloader/classpreloader",
@ -1874,6 +1874,55 @@
"description": "Symfony Translation Component", "description": "Symfony Translation Component",
"homepage": "http://symfony.com", "homepage": "http://symfony.com",
"time": "2014-10-26 07:41:27" "time": "2014-10-26 07:41:27"
},
{
"name": "watson/validating",
"version": "0.10.6",
"source": {
"type": "git",
"url": "https://github.com/dwightwatson/validating.git",
"reference": "673b165b4391942a7fae1e85a84f21b5f367f33f"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/dwightwatson/validating/zipball/673b165b4391942a7fae1e85a84f21b5f367f33f",
"reference": "673b165b4391942a7fae1e85a84f21b5f367f33f",
"shasum": ""
},
"require": {
"illuminate/database": "~4.2.6",
"illuminate/events": "~4.2.6",
"illuminate/support": "~4.2.6",
"illuminate/validation": "~4.2.6",
"php": ">=5.4.0"
},
"require-dev": {
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "4.1.*"
},
"type": "library",
"autoload": {
"psr-4": {
"Watson\\Validating\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Dwight Watson",
"email": "dwight@studiousapp.com"
}
],
"description": "Eloquent model validating trait.",
"keywords": [
"eloquent",
"laravel",
"validation"
],
"time": "2014-11-20 02:09:08"
} }
], ],
"packages-dev": [], "packages-dev": [],