Updated DingoAPI to .7
Rename component.component to component_id with relationship name change Rename more instances of parent to component Working on Incident Repository Fix Incident seeder component_id is fillable within the model Fix bad relationship Finished Incident repository Added Metric repository Updated tests for new dingo release
This commit is contained in:
committed by
James Brooks
parent
692d2fc0fe
commit
b2e1d2750c
@@ -3,18 +3,17 @@
|
||||
namespace CachetHQ\Cachet\Controllers\Api;
|
||||
|
||||
use Input;
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Dingo\Api\Auth\Shield;
|
||||
use Dingo\Api\Routing\ControllerTrait;
|
||||
use Illuminate\Routing\Controller;
|
||||
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
|
||||
|
||||
class ComponentController extends DingoController {
|
||||
class ComponentController extends Controller {
|
||||
|
||||
protected $auth;
|
||||
use ControllerTrait;
|
||||
|
||||
protected $component;
|
||||
|
||||
public function __construct(Shield $auth, ComponentRepository $component) {
|
||||
$this->auth = $auth;
|
||||
public function __construct(ComponentRepository $component) {
|
||||
$this->component = $component;
|
||||
}
|
||||
|
||||
@@ -38,6 +37,11 @@ class ComponentController extends DingoController {
|
||||
return $this->component->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return a component with incidents
|
||||
* @param [type] $id [description]
|
||||
* @return [type] [description]
|
||||
*/
|
||||
public function getComponentIncidents($id) {
|
||||
return $this->component->with($id, ['incidents']);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,19 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Controllers\Api;
|
||||
|
||||
use Input, Incident;
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Dingo\Api\Auth\Shield;
|
||||
use Input;
|
||||
use Dingo\Api\Routing\ControllerTrait;
|
||||
use Illuminate\Routing\Controller;
|
||||
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository;
|
||||
|
||||
class IncidentController extends DingoController {
|
||||
class IncidentController extends Controller {
|
||||
|
||||
protected $auth;
|
||||
use ControllerTrait;
|
||||
|
||||
public function __construct(Shield $auth) {
|
||||
$this->auth = $auth;
|
||||
protected $incident;
|
||||
|
||||
public function __construct(IncidentRepository $incident) {
|
||||
$this->incident = $incident;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -20,7 +23,7 @@ class IncidentController extends DingoController {
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getIncidents() {
|
||||
return Incident::all();
|
||||
return $this->incident->all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -31,11 +34,7 @@ class IncidentController extends DingoController {
|
||||
* @return Incident
|
||||
*/
|
||||
public function getIncident($id) {
|
||||
if ($incident = Incident::find($id)) {
|
||||
return $incident;
|
||||
} else {
|
||||
App::abort(404, 'Incident not found');
|
||||
}
|
||||
return $this->incident->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -44,9 +43,7 @@ class IncidentController extends DingoController {
|
||||
* @return Incident
|
||||
*/
|
||||
public function postIncidents() {
|
||||
$incident = new Incident(Input::all());
|
||||
$incident->user_id = $this->auth->user()->id;
|
||||
return $this->_saveIncident($incident);
|
||||
return $this->incident->create($this->auth->user()->id, Input::all());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -57,35 +54,6 @@ class IncidentController extends DingoController {
|
||||
* @return Incident
|
||||
*/
|
||||
public function putIncident($id) {
|
||||
$incident = $this->getIncident($id);
|
||||
|
||||
$incident->fill(Input::all());
|
||||
|
||||
return $this->_saveIncident($incident);
|
||||
}
|
||||
|
||||
/**
|
||||
* Function for saving the incident, and returning appropriate error codes
|
||||
*
|
||||
* @param Incident $incident
|
||||
*
|
||||
* @return Incident
|
||||
*/
|
||||
private function _saveIncident($incident) {
|
||||
if ($incident->isValid()) {
|
||||
try {
|
||||
$component = $incident->component;
|
||||
if (!$component) {
|
||||
App::abort(400, 'Invalid component specified');
|
||||
}
|
||||
|
||||
$incident->saveOrFail();
|
||||
return $incident;
|
||||
} catch (Exception $e) {
|
||||
App::abort(500, $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
App::abort(404, $incident->getErrors()->first());
|
||||
}
|
||||
return $this->incident->update($id, Input::all());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,18 +2,27 @@
|
||||
|
||||
namespace CachetHQ\Cachet\Controllers\Api;
|
||||
|
||||
use Input, Metric;
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Input;
|
||||
use Dingo\Api\Routing\ControllerTrait;
|
||||
use Illuminate\Routing\Controller;
|
||||
use CachetHQ\Cachet\Repositories\Metric\MetricRepository;
|
||||
|
||||
class MetricController extends DingoController {
|
||||
class MetricController extends Controller {
|
||||
|
||||
use ControllerTrait;
|
||||
|
||||
protected $metric;
|
||||
|
||||
public function __construct(MetricRepository $metric) {
|
||||
$this->metric = $metric;
|
||||
}
|
||||
/**
|
||||
* Get all metrics
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getMetrics() {
|
||||
return Metric::all();
|
||||
return $this->metric->all();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -24,11 +33,7 @@ class MetricController extends DingoController {
|
||||
* @return Metric
|
||||
*/
|
||||
public function getMetric($id) {
|
||||
if ($metric = Metric::find($id)) {
|
||||
return $metric;
|
||||
} else {
|
||||
App::abort(404, 'Metric not found');
|
||||
}
|
||||
return $this->metric->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -37,8 +42,7 @@ class MetricController extends DingoController {
|
||||
* @return Metric
|
||||
*/
|
||||
public function postMetrics() {
|
||||
$metric = new Metric(Input::all());
|
||||
return $this->_saveMetric($metric);
|
||||
return $this->metric->create(Input::all());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -49,28 +53,6 @@ class MetricController extends DingoController {
|
||||
* @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());
|
||||
}
|
||||
return $this->metric->update($id, Input::all());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -91,16 +91,6 @@ abstract class EloquentRepository {
|
||||
$this->model->delete($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a given model by ID with an array of updates
|
||||
* @param inetegr $id
|
||||
* @param array $array Key Value pairs to update
|
||||
*/
|
||||
public function update($id, array $array) {
|
||||
$model = $this->model->whereId($id)->first(['id']);
|
||||
$model->update($array);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate a given model with Watson validation
|
||||
* @param object $model
|
||||
@@ -110,5 +100,21 @@ abstract class EloquentRepository {
|
||||
if ($model->isInvalid()) {
|
||||
throw new Exception('Invalid model validation');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validate whether a model has a correct relationship
|
||||
* @param object $model
|
||||
* @param string $relationship Name of the relationship to validate against
|
||||
* @return Exception
|
||||
*/
|
||||
public function hasRelationship($model, $relationship) {
|
||||
if (! $model->$relationship) {
|
||||
throw new Exception('Invalid relationship exception');
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Incident;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
use Incident;
|
||||
|
||||
class EloquentIncidentRepository extends EloquentRepository implements IncidentRepository {
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(Incident $model) {
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function create($user_id, array $array) {
|
||||
$incident = new $this->model($array);
|
||||
$incident->user_id = $user_id;
|
||||
|
||||
$this->validate($incident)->hasRelationship($incident, 'component');
|
||||
|
||||
$incident->saveOrFail();
|
||||
return $incident;
|
||||
}
|
||||
|
||||
public function update($id, array $array) {
|
||||
$incident = $this->model->findOrFail($id);
|
||||
$incident->fill($array);
|
||||
|
||||
$this->validate($incident)->hasRelationship($incident, 'component');
|
||||
|
||||
$incident->update($array);
|
||||
return $incident;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Incident;
|
||||
|
||||
interface IncidentRepository {
|
||||
|
||||
public function all();
|
||||
|
||||
public function create($id, array $array);
|
||||
|
||||
public function findOrFail($id);
|
||||
|
||||
public function update($id, array $with);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Metric;
|
||||
|
||||
use CachetHQ\Cachet\Repositories\EloquentRepository;
|
||||
use Metric;
|
||||
|
||||
class EloquentMetricRepository extends EloquentRepository implements MetricRepository {
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(Metric $model) {
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function create(array $array) {
|
||||
$metric = new $this->model($array);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->saveOrFail();
|
||||
return $metric;
|
||||
}
|
||||
|
||||
public function update($id, array $array) {
|
||||
$metric = $this->model->findOrFail($id);
|
||||
$metric->fill($array);
|
||||
|
||||
$this->validate($metric);
|
||||
|
||||
$metric->update($array);
|
||||
return $metric;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?php
|
||||
|
||||
namespace CachetHQ\Cachet\Repositories\Metric;
|
||||
|
||||
interface MetricRepository {
|
||||
|
||||
public function all();
|
||||
|
||||
public function create(array $array);
|
||||
|
||||
public function findOrFail($id);
|
||||
|
||||
public function update($id, array $with);
|
||||
}
|
||||
@@ -10,5 +10,13 @@ class RepositoryServiceProvider extends ServiceProvider {
|
||||
'CachetHQ\Cachet\Repositories\Component\ComponentRepository',
|
||||
'CachetHQ\Cachet\Repositories\Component\EloquentComponentRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'CachetHQ\Cachet\Repositories\Incident\IncidentRepository',
|
||||
'CachetHQ\Cachet\Repositories\Incident\EloquentIncidentRepository'
|
||||
);
|
||||
$this->app->bind(
|
||||
'CachetHQ\Cachet\Repositories\Metric\MetricRepository',
|
||||
'CachetHQ\Cachet\Repositories\Metric\EloquentMetricRepository'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -122,7 +122,7 @@ return array(
|
||||
'Illuminate\View\ViewServiceProvider',
|
||||
'Illuminate\Workbench\WorkbenchServiceProvider',
|
||||
|
||||
'Dingo\Api\ApiServiceProvider',
|
||||
'Dingo\Api\Provider\ApiServiceProvider',
|
||||
'Thujohn\Rss\RssServiceProvider',
|
||||
|
||||
'CachetHQ\Cachet\Support\ServiceProviders\RepositoryServiceProvider',
|
||||
|
||||
@@ -14,7 +14,7 @@ class ComponentControllerTest extends TestCase {
|
||||
public function test_get_components_method() {
|
||||
$this->repo->shouldReceive('all')->once()->andReturn('foo');
|
||||
|
||||
$controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->dingo, $this->repo);
|
||||
$controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->repo);
|
||||
$response = $controller->getComponents();
|
||||
|
||||
$this->assertEquals('foo', $response);
|
||||
@@ -23,9 +23,10 @@ class ComponentControllerTest extends TestCase {
|
||||
public function test_get_component_method() {
|
||||
$this->repo->shouldReceive('findOrFail')->with(1)->once()->andReturn('foo');
|
||||
|
||||
$controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->dingo, $this->repo);
|
||||
$controller = new CachetHQ\Cachet\Controllers\Api\ComponentController($this->repo);
|
||||
$response = $controller->getComponent(1);
|
||||
|
||||
$this->assertEquals('foo', $response);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user