Merge pull request #42 from jbrooksuk/api-update-component
API call to update existing components
This commit is contained in:
@@ -44,18 +44,24 @@
|
||||
public function postComponents() {
|
||||
$component = new Component(Input::all());
|
||||
$component->user_id = $this->auth->user()->id;
|
||||
if ($component->isValid()) {
|
||||
try {
|
||||
$component->saveOrFail();
|
||||
return $component;
|
||||
} catch (Exception $e) {
|
||||
App::abort(500, $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
App::abort(404, $component->getErrors()->first());
|
||||
}
|
||||
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
|
||||
*
|
||||
@@ -130,6 +136,26 @@
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
*
|
||||
|
||||
@@ -15,6 +15,7 @@
|
||||
Route::post('incidents', 'ApiController@postIncidents');
|
||||
Route::post('metrics', 'ApiController@postMetrics');
|
||||
|
||||
Route::put('components/{id}', 'ApiController@putComponent');
|
||||
Route::put('incidents/{id}', 'ApiController@putIncident');
|
||||
Route::put('metrics/{id}', 'ApiController@putMetric');
|
||||
});
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
'name' => $component->name,
|
||||
'description' => $component->description,
|
||||
'status_id' => (int) $component->status,
|
||||
'status' => $component->getHumanStatusAttribute(),
|
||||
'status' => $component->humanStatus,
|
||||
'incident_count' => $component->incidents()->count(),
|
||||
'created_at' => $component->created_at->timestamp,
|
||||
'updated_at' => $component->updated_at->timestamp,
|
||||
|
||||
Reference in New Issue
Block a user