Merge pull request #40 from jbrooksuk/metric-api

Create and update a metric.
This commit is contained in:
Philip Manavopoulos
2014-11-25 21:52:36 +00:00
4 changed files with 88 additions and 1 deletions

View File

@@ -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());
}
}
}

View File

@@ -1,5 +1,32 @@
<?php
class Metric extends Eloquent {
use \Watson\Validating\ValidatingTrait;
class Metric extends Eloquent implements \Dingo\Api\Transformer\TransformableInterface {
use ValidatingTrait;
protected $rules = [
'name' => '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();
}
}

View File

@@ -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');
});
});

View File

@@ -0,0 +1,15 @@
<?php
class MetricTransformer extends \League\Fractal\TransformerAbstract {
public function transform(Metric $metric) {
return [
'id' => (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,
];
}
}