diff --git a/app/CachetHq/Cachet/Controllers/Api/ComponentController.php b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php new file mode 100644 index 00000000..0e5be1b5 --- /dev/null +++ b/app/CachetHq/Cachet/Controllers/Api/ComponentController.php @@ -0,0 +1,51 @@ +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->with($id, ['incidents']); + } + + /** + * Create a new component + * + * @return Component + */ + public function postComponents() { + 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..cb21f0f9 --- /dev/null +++ b/app/CachetHq/Cachet/Controllers/Api/IncidentController.php @@ -0,0 +1,90 @@ +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); + } + + /** + * 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()); + } + } +} \ No newline at end of file diff --git a/app/CachetHq/Cachet/Controllers/Api/MetricController.php b/app/CachetHq/Cachet/Controllers/Api/MetricController.php new file mode 100644 index 00000000..a5cd83ff --- /dev/null +++ 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/CachetHq/Cachet/Repositories/Component/ComponentRepository.php b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php new file mode 100644 index 00000000..cf02022d --- /dev/null +++ b/app/CachetHq/Cachet/Repositories/Component/ComponentRepository.php @@ -0,0 +1,12 @@ +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 new file mode 100644 index 00000000..9d018737 --- /dev/null +++ b/app/CachetHq/Cachet/Repositories/EloquentRepository.php @@ -0,0 +1,101 @@ +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); + } + + /** + * 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 ID + * @param integer $id + * @return object|ModelNotFoundException + */ + public function findOrFail($id) { + return $this->model->findOrFail($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/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php b/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php new file mode 100644 index 00000000..3f772fdf --- /dev/null +++ b/app/CachetHq/Cachet/Support/ServiceProviders/RepositoryServiceProvider.php @@ -0,0 +1,12 @@ +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 bdc08ade..4e16e1e0 100644 --- a/app/config/app.php +++ b/app/config/app.php @@ -124,6 +124,8 @@ return array( 'Dingo\Api\ApiServiceProvider', + 'CachetHq\Cachet\Support\ServiceProviders\RepositoryServiceProvider', + ), /* 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 d7b3cd17..02497891 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -1,23 +1,23 @@ 'v1', 'prefix' => 'api'], function() { + Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHq\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', 'MetricController@getMetrics'); + Route::get('metrics/{id}', 'MetricController@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', 'MetricController@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}', 'MetricController@putMetric'); }); }); diff --git a/composer.json b/composer.json index 6a72df53..eaa655ac 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,10 @@ "app/database/seeds", "app/tests/TestCase.php", "app/filters" - ] + ], + "psr-4": { + "CachetHq\\": "app/CachetHq" + } }, "extra": { "heroku": {