From e104a9a3171587ecf6f4d13846e1792789be2ef3 Mon Sep 17 00:00:00 2001 From: manavo Date: Tue, 25 Nov 2014 11:06:28 +0000 Subject: [PATCH 1/4] Create components and incidents from the API --- app/controllers/ApiController.php | 58 +++++++++++++++++++++++++++++++ app/models/Component.php | 11 ++++++ app/models/Incident.php | 14 ++++++++ app/routes/api.php | 5 +++ composer.json | 3 +- composer.lock | 51 ++++++++++++++++++++++++++- 6 files changed, 140 insertions(+), 2 deletions(-) diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 98960ad2..3ad2cbf0 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -1,11 +1,28 @@ 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()); + } + } } diff --git a/app/models/Component.php b/app/models/Component.php index 337cf371..4f246b4a 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -1,6 +1,17 @@ 'required', + 'status' => 'required|integer' + ]; + + protected $fillable = ['name', 'description', 'status']; + /** * Lookup all of the incidents reported on the component. * @return Illuminate\Database\Eloquent\Relations diff --git a/app/models/Incident.php b/app/models/Incident.php index c0aa4b1c..2807312f 100644 --- a/app/models/Incident.php +++ b/app/models/Incident.php @@ -1,6 +1,20 @@ '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 diff --git a/app/routes/api.php b/app/routes/api.php index 3f55953f..3522a5f0 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -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'); + }); + }); diff --git a/composer.json b/composer.json index 3345983b..e6933ee4 100644 --- a/composer.json +++ b/composer.json @@ -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": [ diff --git a/composer.lock b/composer.lock index 3bc2e6cb..90fe5eac 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "f1b0b35a64f38b74527150925a720e8d", + "hash": "582a0ffe83dfc08436525ef3419162a7", "packages": [ { "name": "classpreloader/classpreloader", @@ -1874,6 +1874,55 @@ "description": "Symfony Translation Component", "homepage": "http://symfony.com", "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": [], From e994f2a88a3d009f249bccc03b8437e6cd62a2bf Mon Sep 17 00:00:00 2001 From: manavo Date: Tue, 25 Nov 2014 11:50:15 +0000 Subject: [PATCH 2/4] Save the user ID of who created the component or incident --- app/controllers/ApiController.php | 2 ++ ...11_25_114040_UserIdColumnForComponents.php | 36 +++++++++++++++++++ ..._11_25_114101_UserIdColumnForIncidents.php | 36 +++++++++++++++++++ app/models/Component.php | 3 +- app/models/Incident.php | 3 +- 5 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php create mode 100644 app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 3ad2cbf0..ce2778fa 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -43,6 +43,7 @@ */ public function postComponents() { $component = new Component(Input::all()); + $component->user_id = $this->auth->user()->id; if ($component->isValid()) { $component->saveOrFail(); return $component; @@ -82,6 +83,7 @@ */ public function postIncidents() { $incident = new Incident(Input::all()); + $incident->user_id = $this->auth->user()->id; if ($incident->isValid()) { $incident->saveOrFail(); return $incident; diff --git a/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php b/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php new file mode 100644 index 00000000..7f676f33 --- /dev/null +++ b/app/database/migrations/2014_11_25_114040_UserIdColumnForComponents.php @@ -0,0 +1,36 @@ +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'); + }); + } + +} diff --git a/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php b/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php new file mode 100644 index 00000000..1fa032be --- /dev/null +++ b/app/database/migrations/2014_11_25_114101_UserIdColumnForIncidents.php @@ -0,0 +1,36 @@ +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'); + }); + } + +} diff --git a/app/models/Component.php b/app/models/Component.php index 4f246b4a..e1aa495d 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -6,11 +6,12 @@ use ValidatingTrait; protected $rules = [ + 'user_id' => 'required|integer', 'name' => 'required', 'status' => 'required|integer' ]; - protected $fillable = ['name', 'description', 'status']; + protected $fillable = ['user_id', 'name', 'description', 'status']; /** * Lookup all of the incidents reported on the component. diff --git a/app/models/Incident.php b/app/models/Incident.php index 2807312f..0f9ab1f3 100644 --- a/app/models/Incident.php +++ b/app/models/Incident.php @@ -7,13 +7,14 @@ 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']; + protected $fillable = ['user_id', 'component', 'name', 'status', 'message']; /** * An incident belongs to a component. From 325ed033ba3454c721a510ce322bf637adb38722 Mon Sep 17 00:00:00 2001 From: manavo Date: Tue, 25 Nov 2014 12:27:34 +0000 Subject: [PATCH 3/4] Better validation --- app/controllers/ApiController.php | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index ce2778fa..cd1ea87e 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -45,8 +45,12 @@ $component = new Component(Input::all()); $component->user_id = $this->auth->user()->id; if ($component->isValid()) { - $component->saveOrFail(); - return $component; + try { + $component->saveOrFail(); + return $component; + } catch (Exception $e) { + App::abort(500, $e->getMessage()); + } } else { App::abort(404, $component->getErrors()->first()); } @@ -85,8 +89,17 @@ $incident = new Incident(Input::all()); $incident->user_id = $this->auth->user()->id; if ($incident->isValid()) { - $incident->saveOrFail(); - return $incident; + 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()); } From a4d3928916478c6ca78debf035f313890e133a61 Mon Sep 17 00:00:00 2001 From: manavo Date: Tue, 25 Nov 2014 12:40:12 +0000 Subject: [PATCH 4/4] Allow updating incidents from the API --- app/controllers/ApiController.php | 27 +++++++++++++++++++++++++++ app/models/Component.php | 2 +- app/models/Incident.php | 2 +- app/routes/api.php | 2 ++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index cd1ea87e..1772858a 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -88,6 +88,32 @@ 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; @@ -104,4 +130,5 @@ App::abort(404, $incident->getErrors()->first()); } } + } diff --git a/app/models/Component.php b/app/models/Component.php index e1aa495d..9e2209d0 100644 --- a/app/models/Component.php +++ b/app/models/Component.php @@ -11,7 +11,7 @@ 'status' => 'required|integer' ]; - protected $fillable = ['user_id', 'name', 'description', 'status']; + protected $fillable = ['name', 'description', 'status']; /** * Lookup all of the incidents reported on the component. diff --git a/app/models/Incident.php b/app/models/Incident.php index 0f9ab1f3..1bcc8bea 100644 --- a/app/models/Incident.php +++ b/app/models/Incident.php @@ -14,7 +14,7 @@ 'message' => 'required', ]; - protected $fillable = ['user_id', 'component', 'name', 'status', 'message']; + protected $fillable = ['component', 'name', 'status', 'message']; /** * An incident belongs to a component. diff --git a/app/routes/api.php b/app/routes/api.php index 3522a5f0..13685a4c 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -11,6 +11,8 @@ Route::group(['protected' => true], function() { Route::post('components', 'ApiController@postComponents'); Route::post('incidents', 'ApiController@postIncidents'); + + Route::put('incidents/{id}', 'ApiController@putIncident'); }); });