diff --git a/app/controllers/ApiController.php b/app/controllers/ApiController.php index 3364a174..09852260 100644 --- a/app/controllers/ApiController.php +++ b/app/controllers/ApiController.php @@ -155,4 +155,47 @@ } } + /** + * 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/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, + ]; + } + }