From 26e516f7395914f5ffa3c815de2b7b89c0fc3239 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 11:31:52 +0000 Subject: [PATCH 1/6] Breaking controllers down into namespaces --- app/controllers/api/ComponentController.php | 61 +++++++++++++++++++++ app/controllers/api/IncidentController.php | 0 2 files changed, 61 insertions(+) create mode 100644 app/controllers/api/ComponentController.php create mode 100644 app/controllers/api/IncidentController.php diff --git a/app/controllers/api/ComponentController.php b/app/controllers/api/ComponentController.php new file mode 100644 index 00000000..5a1ad843 --- /dev/null +++ b/app/controllers/api/ComponentController.php @@ -0,0 +1,61 @@ +auth = $auth; + $this->component = $component; + } + + /** + * Get all components + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getComponents() { + return $this->component->all(); + } + + /** + * Get a single component + * + * @param int $id + * + * @return Component + */ + public function getComponent($id) { + return $this->component->findOrFail($id); + } + + public function getComponentIncidents($id) { + return $this->component->incidents($id); + } + + /** + * 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()); + } + } +} diff --git a/app/controllers/api/IncidentController.php b/app/controllers/api/IncidentController.php new file mode 100644 index 00000000..e69de29b From 058d219733709b1e54f5245d4656f3b5e2016cd4 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 11:38:20 +0000 Subject: [PATCH 2/6] Added repository files --- .../Component/ComponentRepository.php | 10 +++ .../Component/EloquentComponentRepository.php | 9 +++ .../Repositories/EloquentRepository.php | 81 +++++++++++++++++++ .../RepositoryServiceProvider.php | 12 +++ 4 files changed, 112 insertions(+) create mode 100644 app/Cachet/Repositories/Component/ComponentRepository.php create mode 100644 app/Cachet/Repositories/Component/EloquentComponentRepository.php create mode 100644 app/Cachet/Repositories/EloquentRepository.php create mode 100644 app/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php diff --git a/app/Cachet/Repositories/Component/ComponentRepository.php b/app/Cachet/Repositories/Component/ComponentRepository.php new file mode 100644 index 00000000..1a239c67 --- /dev/null +++ b/app/Cachet/Repositories/Component/ComponentRepository.php @@ -0,0 +1,10 @@ +model->create($array); + } + + /** + * Sets the model to query against a user id + * @param integer $id + * @param string $column + * @return $this + */ + public function withAuth($id, $column = 'user_id') { + $this->model = $this->model->where($column, $id); + return $this; + } + + /** + * Finds a model by ID + * @param int $id + * @return object + */ + public function find(int $id) { + return $this->model->find($id); + } + + /** + * Finds a model by type + * @param string $key + * @param string $value + * @param array $columns + * @return object|ModelNotFoundException + */ + public function findByOrFail($key, $value, $columns = ['*']) { + if (! is_null($item = $this->model->where($key, $value)->first($columns))) { + return $item; + } + + throw new ModelNotFoundException; + } + + /** + * Counts the number of rows returned + * @param string $key + * @param string $value + * @return integer + */ + public function count($key = null, $value = null) { + if (is_null($key) || is_null($value)) { + return $this->model->where($key, $value)->count(); + } + + return $this->model->count(); + } + + /** + * Deletes a model by ID + * @param inetegr $id + */ + public function destroy($id) { + $this->model->delete($id); + } + + /** + * Updates a given model by ID with an array of updates + * @param inetegr $id + * @param array $array Key Value pairs to update + */ + public function update($id, array $array) { + $model = $this->model->whereId($id)->first(['id']); + $model->update($array); + } +} \ No newline at end of file diff --git a/app/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php b/app/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php new file mode 100644 index 00000000..adc6a0a9 --- /dev/null +++ b/app/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php @@ -0,0 +1,12 @@ +app->bind('Cachet\Repositories\Component\ComponentRepository', 'Cachet\Repositories\Component\EloquentComponentRepository'); + } + +} \ No newline at end of file From e454a3395b95462d52801a542091b1e46f196f04 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 11:39:03 +0000 Subject: [PATCH 3/6] added Cachet to autoloading and loading of service provider --- app/config/app.php | 2 ++ composer.json | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/config/app.php b/app/config/app.php index bdc08ade..36d5d287 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -124,6 +124,8 @@ return array( 'Dingo\Api\ApiServiceProvider', + 'Cachet\Support\ServiceProviders\RepositoryServiceProvider', + ), /* diff --git a/composer.json b/composer.json index e6933ee4..d33b3b98 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,10 @@ "app/database/seeds", "app/tests/TestCase.php", "app/filters" - ] + ], + "psr-4": { + "Cachet\\": "app/Cachet" + } }, "extra": { "heroku": { From 7c0b80f6a46067b4ac09db7f484eeec768aae866 Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 13:01:07 +0000 Subject: [PATCH 4/6] Changed namespacing to master CachetHq --- .../Component/EloquentComponentRepository.php | 9 --- .../RepositoryServiceProvider.php | 12 ---- .../Controllers/Api/ComponentController.php | 61 +++++++++++++++++++ .../Component/ComponentRepository.php | 2 +- .../Component/EloquentComponentRepository.php | 9 +++ .../Repositories/EloquentRepository.php | 2 +- .../RepositoryServiceProvider.php | 12 ++++ app/config/app.php | 2 +- app/controllers/api/ComponentController.php | 4 +- app/routes/api.php | 28 ++++----- composer.json | 2 +- 11 files changed, 102 insertions(+), 41 deletions(-) delete mode 100644 app/Cachet/Repositories/Component/EloquentComponentRepository.php delete mode 100644 app/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php create mode 100644 app/CachetHq/Cachet/Controllers/Api/ComponentController.php rename app/{ => CachetHq}/Cachet/Repositories/Component/ComponentRepository.php (69%) create mode 100644 app/CachetHq/Cachet/Repositories/Component/EloquentComponentRepository.php rename app/{ => CachetHq}/Cachet/Repositories/EloquentRepository.php (97%) create mode 100644 app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php diff --git a/app/Cachet/Repositories/Component/EloquentComponentRepository.php b/app/Cachet/Repositories/Component/EloquentComponentRepository.php deleted file mode 100644 index 91c6cd6f..00000000 --- a/app/Cachet/Repositories/Component/EloquentComponentRepository.php +++ /dev/null @@ -1,9 +0,0 @@ -app->bind('Cachet\Repositories\Component\ComponentRepository', 'Cachet\Repositories\Component\EloquentComponentRepository'); - } - -} \ No newline at end of file diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php new file mode 100644 index 00000000..3e9ef024 --- /dev/null +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -0,0 +1,61 @@ +auth = $auth; + $this->component = $component; + } + + /** + * Get all components + * + * @return \Illuminate\Database\Eloquent\Collection + */ + public function getComponents() { + return $this->component->all(); + } + + /** + * Get a single component + * + * @param int $id + * + * @return Component + */ + public function getComponent($id) { + return $this->component->findOrFail($id); + } + + public function getComponentIncidents($id) { + return $this->component->incidents($id); + } + + /** + * 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()); + } + } +} diff --git a/app/Cachet/Repositories/Component/ComponentRepository.php b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php similarity index 69% rename from app/Cachet/Repositories/Component/ComponentRepository.php rename to app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php index 1a239c67..ce8f15fe 100644 --- a/app/Cachet/Repositories/Component/ComponentRepository.php +++ b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php @@ -1,4 +1,4 @@ -app->bind('CachetHq\Cachet\Repositories\Component\ComponentRepository', 'CachetHq\Cachet\Repositories\Component\EloquentComponentRepository'); + } + +} \ No newline at end of file diff --git a/app/config/app.php b/app/config/app.php index 36d5d287..4e16e1e0 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -124,7 +124,7 @@ return array( 'Dingo\Api\ApiServiceProvider', - 'Cachet\Support\ServiceProviders\RepositoryServiceProvider', + 'CachetHq\Cachet\Support\ServiceProviders\RepositoryServiceProvider', ), diff --git a/app/controllers/api/ComponentController.php b/app/controllers/api/ComponentController.php index 5a1ad843..3ec64ae6 100644 --- a/app/controllers/api/ComponentController.php +++ b/app/controllers/api/ComponentController.php @@ -1,6 +1,6 @@ - 'v1', 'prefix' => 'api'], function() { + Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'Cachet\Controllers\Api'], function() { - 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'); - Route::get('metrics', 'ApiController@getMetrics'); - Route::get('metrics/{id}', 'ApiController@getMetric'); + Route::get('components', 'ComponentController@getComponents'); + Route::get('components/{id}', 'ComponentController@getComponent'); + Route::get('components/{id}/incidents', 'ComponentController@getComponentIncidents'); + Route::get('incidents', 'IncidentController@getIncidents'); + Route::get('incidents/{id}', 'IncidentController@getIncident'); + Route::get('metrics', 'IncidentController@getMetrics'); + Route::get('metrics/{id}', 'IncidentController@getMetric'); Route::group(['protected' => true], function() { - Route::post('components', 'ApiController@postComponents'); - Route::post('incidents', 'ApiController@postIncidents'); - Route::post('metrics', 'ApiController@postMetrics'); + Route::post('components', 'ComponentController@postComponents'); + Route::post('incidents', 'IncidentController@postIncidents'); + Route::post('metrics', 'IncidentController@postMetrics'); - Route::put('components/{id}', 'ApiController@putComponent'); - Route::put('incidents/{id}', 'ApiController@putIncident'); - Route::put('metrics/{id}', 'ApiController@putMetric'); + Route::put('components/{id}', 'ComponentController@putComponent'); + Route::put('incidents/{id}', 'IncidentController@putIncident'); + Route::put('metrics/{id}', 'IncidentController@putMetric'); }); }); diff --git a/composer.json b/composer.json index d33b3b98..99e44f29 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "app/filters" ], "psr-4": { - "Cachet\\": "app/Cachet" + "CachetHq\\": "app/CachetHq" } }, "extra": { From af10821ef59b37b3b96390a28ae24a5a8c41ee3b Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 15:00:36 +0000 Subject: [PATCH 5/6] Added Component Repository into controller --- .../Controllers/Api/ComponentController.php | 16 +---- .../Controllers/Api/IncidentController.php | 64 +++++++++++++++++++ .../Controllers/Api/MetricController.php} | 0 .../Component/ComponentRepository.php | 4 +- .../Component/EloquentComponentRepository.php | 21 +++++- .../Repositories/EloquentRepository.php | 28 ++++++-- app/controllers/api/ComponentController.php | 61 ------------------ app/routes/api.php | 2 +- 8 files changed, 114 insertions(+), 82 deletions(-) create mode 100644 app/CachetHq/Cachet/Controllers/Api/IncidentController.php rename app/{controllers/api/IncidentController.php => CachetHq/Cachet/Controllers/Api/MetricController.php} (100%) delete mode 100644 app/controllers/api/ComponentController.php diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php index 3e9ef024..0e5be1b5 100644 --- a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -1,5 +1,6 @@ component->incidents($id); + return $this->component->with($id, ['incidents']); } /** @@ -45,17 +46,6 @@ class ComponentController extends DingoController { * @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()); - } + return $this->component->create($this->auth->user()->id, Input::all()); } } diff --git a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php new file mode 100644 index 00000000..9dda89bd --- /dev/null +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -0,0 +1,64 @@ +auth = $auth; + } + + /** + * 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; + } else { + App::abort(404, 'Incident not found'); + } + } + + /** + * 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); + } \ No newline at end of file diff --git a/app/controllers/api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/MetricController.php similarity index 100% rename from app/controllers/api/IncidentController.php rename to app/CachetHq/Cachet/Controllers/Api/MetricController.php diff --git a/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php index ce8f15fe..cf02022d 100644 --- a/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php +++ b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php @@ -4,7 +4,9 @@ interface ComponentRepository { public function all(); + public function create($id, array $array); + public function findOrFail($id); - public function incidents($id); + public function with($id, array $with); } diff --git a/app/CachetHq/Cachet/Repositories/Component/EloquentComponentRepository.php b/app/CachetHq/Cachet/Repositories/Component/EloquentComponentRepository.php index 36586cbc..565db2f8 100644 --- a/app/CachetHq/Cachet/Repositories/Component/EloquentComponentRepository.php +++ b/app/CachetHq/Cachet/Repositories/Component/EloquentComponentRepository.php @@ -1,9 +1,26 @@ model = $model; + } + + public function create($user_id, array $array) { + $component = new $this->model($array); + $component->user_id = $user_id; + + if ($component->isInvalid()) { + throw new Exception('Invalid model validation', $component->getErrors()); + } + + $component->saveOrFail(); + return $component; + } } diff --git a/app/CachetHq/Cachet/Repositories/EloquentRepository.php b/app/CachetHq/Cachet/Repositories/EloquentRepository.php index 8f844c09..9d018737 100644 --- a/app/CachetHq/Cachet/Repositories/EloquentRepository.php +++ b/app/CachetHq/Cachet/Repositories/EloquentRepository.php @@ -1,15 +1,26 @@ model->create($array); + public function all() { + return $this->model->all(); + } + + /** + * Returns an object with related relationships + * @param id $id + * @param array $with Array of model relationships + * @return object|ModelNotFoundException + */ + public function with($id, array $with = []) { + return $this->model->with($with)->findOrFail($id); } /** @@ -32,6 +43,15 @@ abstract class EloquentRepository { return $this->model->find($id); } + /** + * Finds a model by ID + * @param integer $id + * @return object|ModelNotFoundException + */ + public function findOrFail($id) { + return $this->model->findOrFail($id); + } + /** * Finds a model by type * @param string $key diff --git a/app/controllers/api/ComponentController.php b/app/controllers/api/ComponentController.php deleted file mode 100644 index 3ec64ae6..00000000 --- a/app/controllers/api/ComponentController.php +++ /dev/null @@ -1,61 +0,0 @@ -auth = $auth; - $this->component = $component; - } - - /** - * Get all components - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getComponents() { - return $this->component->all(); - } - - /** - * Get a single component - * - * @param int $id - * - * @return Component - */ - public function getComponent($id) { - return $this->component->findOrFail($id); - } - - public function getComponentIncidents($id) { - return $this->component->incidents($id); - } - - /** - * 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()); - } - } -} diff --git a/app/routes/api.php b/app/routes/api.php index 81485d68..a704d3cd 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -1,6 +1,6 @@ 'v1', 'prefix' => 'api', 'namespace' => 'Cachet\Controllers\Api'], function() { + Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHq\Cachet\Controllers\Api'], function() { Route::get('components', 'ComponentController@getComponents'); Route::get('components/{id}', 'ComponentController@getComponent'); From 1bd3d0f21fa3e532f644eee16cc54b0655168a4a Mon Sep 17 00:00:00 2001 From: Elliot Hesp Date: Wed, 26 Nov 2014 16:08:47 +0000 Subject: [PATCH 6/6] Clean controllers --- .../Controllers/Api/IncidentController.php | 118 +++++---- .../Controllers/Api/MetricController.php | 76 ++++++ app/controllers/ApiController.php | 227 ------------------ app/routes/api.php | 8 +- 4 files changed, 152 insertions(+), 277 deletions(-) delete mode 100644 app/controllers/ApiController.php diff --git a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php index 9dda89bd..cb21f0f9 100644 --- a/app/CachetHq/Cachet/Controllers/Api/IncidentController.php +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -5,60 +5,86 @@ use Dingo\Api\Routing\Controller as DingoController; use Dingo\Api\Auth\Shield; use CachetHq\Cachet\Repositories\Component\ComponentRepository; -class ComponentController extends DingoController { +class IncidentController extends DingoController { - protected $auth; + protected $auth; - public function __construct(Shield $auth) { - $this->auth = $auth; - } - - /** - * Get all incidents - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getIncidents() { - return Incident::all(); + public function __construct(Shield $auth) { + $this->auth = $auth; + } + + /** + * 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; + } else { + App::abort(404, 'Incident not found'); } + } - /** - * Get a single incident + /** + * 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 int $id + * @param Incident $incident * * @return Incident */ - public function getIncident($id) { - if ($incident = Incident::find($id)) { - 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 not found'); + App::abort(404, $incident->getErrors()->first()); } } - - /** - * 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); - } \ No newline at end of file +} \ No newline at end of file diff --git a/app/CachetHq/Cachet/Controllers/Api/MetricController.php b/app/CachetHq/Cachet/Controllers/Api/MetricController.php index e69de29b..a5cd83ff 100644 --- a/app/CachetHq/Cachet/Controllers/Api/MetricController.php +++ b/app/CachetHq/Cachet/Controllers/Api/MetricController.php @@ -0,0 +1,76 @@ +_saveMetric($metric); + } + + /** + * Update an existing metric + * + * @param int $id + * + * @return Metric + */ + public function putMetric($id) { + $metric = $this->getMetric($id); + $metric->fill(Input::all()); + return $this->_saveMetric($metric); + } + + /** + * Function for saving a metric, and returning appropriate error codes + * + * @param Metric $metric + * + * @return Metric + */ + private function _saveMetric($metric) { + if ($metric->isValid()) { + try { + $metric->saveOrFail(); + return $metric; + } catch (Exception $e) { + App::abort(500, $e->getMessage()); + } + } else { + App::abort(404, $metric->getErrors()->first()); + } + } +} \ No newline at end of file diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php deleted file mode 100644 index 683adaf6..00000000 --- a/app/controllers/ApiController.php +++ /dev/null @@ -1,227 +0,0 @@ -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; - } else { - App::abort(404, 'Component not found'); - } - } - - public function getComponentIncidents($id) { - $component = $this->getComponent($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; - return $this->_saveComponent($component); - } - - /** - * Update an existing component - * - * @param int $id - * - * @return Component - */ - public function putComponent($id) { - $component = $this->getComponent($id); - - $component->fill(Input::all()); - - return $this->_saveComponent($component); - } - - /** - * 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; - } else { - App::abort(404, 'Incident not found'); - } - } - - /** - * 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); - } - - /** - * Get all metrics - * - * @return \Illuminate\Database\Eloquent\Collection - */ - public function getMetrics() { - return Metric::all(); - } - - /** - * Get a single metric - * - * @param int $id - * - * @return Metric - */ - public function getMetric($id) { - if ($metric = Metric::find($id)) { - return $metric; - } else { - App::abort(404, 'Metric not found'); - } - } - - /** - * Function for saving the component, and returning appropriate error codes - * - * @param Component $component - * - * @return Component - */ - private function _saveComponent($component) { - if ($component->isValid()) { - try { - $component->saveOrFail(); - return $component; - } catch (Exception $e) { - App::abort(500, $e->getMessage()); - } - } else { - App::abort(404, $component->getErrors()->first()); - } - } - - /** - * 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()); - } - } - - /** - * Create a new metric - * - * @return Metric - */ - public function postMetrics() { - $metric = new Metric(Input::all()); - return $this->_saveMetric($metric); - } - - /** - * Update an existing metric - * - * @param int $id - * - * @return Metric - */ - public function putMetric($id) { - $metric = $this->getMetric($id); - $metric->fill(Input::all()); - return $this->_saveMetric($metric); - } - - /** - * Function for saving a metric, and returning appropriate error codes - * - * @param Metric $metric - * - * @return Metric - */ - private function _saveMetric($metric) { - if ($metric->isValid()) { - try { - $metric->saveOrFail(); - return $metric; - } catch (Exception $e) { - App::abort(500, $e->getMessage()); - } - } else { - App::abort(404, $metric->getErrors()->first()); - } - } - - } diff --git a/app/routes/api.php b/app/routes/api.php index a704d3cd..02497891 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -7,17 +7,17 @@ Route::get('components/{id}/incidents', 'ComponentController@getComponentIncidents'); Route::get('incidents', 'IncidentController@getIncidents'); Route::get('incidents/{id}', 'IncidentController@getIncident'); - Route::get('metrics', 'IncidentController@getMetrics'); - Route::get('metrics/{id}', 'IncidentController@getMetric'); + Route::get('metrics', 'MetricController@getMetrics'); + Route::get('metrics/{id}', 'MetricController@getMetric'); Route::group(['protected' => true], function() { Route::post('components', 'ComponentController@postComponents'); Route::post('incidents', 'IncidentController@postIncidents'); - Route::post('metrics', 'IncidentController@postMetrics'); + Route::post('metrics', 'MetricController@postMetrics'); Route::put('components/{id}', 'ComponentController@putComponent'); Route::put('incidents/{id}', 'IncidentController@putIncident'); - Route::put('metrics/{id}', 'IncidentController@putMetric'); + Route::put('metrics/{id}', 'MetricController@putMetric'); }); });