From 39bc6a164860162c6a20d7f5a0dc8ea7a1cfffca Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 25 Nov 2014 21:35:52 +0000 Subject: [PATCH 1/3] Create and update a metric. --- app/controllers/ApiController.php | 43 ++++++++++++++++++++++++++ app/models/Metric.php | 29 ++++++++++++++++- app/routes/api.php | 2 ++ app/transformers/MetricTransformer.php | 15 +++++++++ 4 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 app/transformers/MetricTransformer.php diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 3364a174..96c04e98 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -155,4 +155,47 @@ } } + /** + * Create a new metric + * + * @return Metric + */ + public function postIncidents() { + $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/models/Metric.php b/app/models/Metric.php index 0cba94e7..3f70de1d 100644 --- a/app/models/Metric.php +++ b/app/models/Metric.php @@ -1,5 +1,32 @@ 'required', + 'suffix' => 'required', + 'display_chart' => 'boolean', + ]; + + protected $fillable = ['name', 'suffix', 'description', 'display_chart']; + + /** + * Determines whether a chart should be shown. + * @return bool + */ + public function getShouldDisplayAttribute() { + return $this->display_chart === 1; + } + + /** + * Get the transformer instance. + * + * @return ComponentTransformer + */ + public function getTransformer() { + return new MetricTransformer(); + } } diff --git a/app/routes/api.php b/app/routes/api.php index 397ab254..00d2ffbb 100644 --- a/app/routes/api.php +++ b/app/routes/api.php @@ -13,8 +13,10 @@ Route::group(['protected' => true], function() { Route::post('components', 'ApiController@postComponents'); Route::post('incidents', 'ApiController@postIncidents'); + Route::post('metrics', 'ApiController@postMetrics'); Route::put('incidents/{id}', 'ApiController@putIncident'); + Route::put('metrics/{id}', 'ApiController@putMetric'); }); }); diff --git a/app/transformers/MetricTransformer.php b/app/transformers/MetricTransformer.php new file mode 100644 index 00000000..d1f2411a --- /dev/null +++ b/app/transformers/MetricTransformer.php @@ -0,0 +1,15 @@ + (int) $component->id, + 'name' => $component->name, + 'description' => $component->description, + 'suffix' => $component->suffix, + 'display' => $component->shouldDisplay, + 'created_at' => $component->created_at->timestamp, + 'updated_at' => $component->updated_at->timestamp, + ]; + } + } From a32c2106113f62b8934171da670b95c1c579eb71 Mon Sep 17 00:00:00 2001 From: James Brooks Date: Tue, 25 Nov 2014 21:51:36 +0000 Subject: [PATCH 2/3] Fixed bad method name --- app/controllers/ApiController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 96c04e98..006da1a9 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -160,7 +160,7 @@ * * @return Metric */ - public function postIncidents() { + public function postMetric() { $metric = new Metric(Input::all()); return $this->_saveMetric($metric); } From 1a8c3ac559bba1d29e845f7a9b3a6f9f198131f9 Mon Sep 17 00:00:00 2001 From: Philip Manavopoulos Date: Tue, 25 Nov 2014 21:52:26 +0000 Subject: [PATCH 3/3] Fix function name --- app/controllers/ApiController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 006da1a9..09852260 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -160,7 +160,7 @@ * * @return Metric */ - public function postMetric() { + public function postMetrics() { $metric = new Metric(Input::all()); return $this->_saveMetric($metric); }