Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create and update a metric. #40

Merged
merged 3 commits into from
Nov 25, 2014
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions app/controllers/ApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,47 @@ private function _saveIncident($incident) {
}
}

/**
* Create a new metric
*
* @return Metric
*/
public function postIncidents() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy paste error? Should be postMetrics?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.

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

}
29 changes: 28 additions & 1 deletion app/models/Metric.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
<?php

class Metric extends Eloquent {
use \Watson\Validating\ValidatingTrait;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No leading slash is required.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found whilst working on Hippo that if you start namespacing things, it's good to prefix it with a slash as to make it "fully qualified". We had issues before.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still not required for imports.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fully qualified names don't have the leading slashes by definition.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But that's exactly where the problem lay last time.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okey dokey, we can get them removed, but I'm still 100% sure that there was a reason we ended up having to do that (not for traits, since we don't use them).


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();
}
}
2 changes: 2 additions & 0 deletions app/routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
});

});
15 changes: 15 additions & 0 deletions app/transformers/MetricTransformer.php
Original file line number Diff line number Diff line change
@@ -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,
];
}
}