Added Component Repository into controller
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
<?php namespace CachetHq\Cachet\Controllers\Api;
|
||||
|
||||
use Input;
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Dingo\Api\Auth\Shield;
|
||||
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
|
||||
@@ -36,7 +37,7 @@ class ComponentController extends DingoController {
|
||||
}
|
||||
|
||||
public function getComponentIncidents($id) {
|
||||
return $this->component->incidents($id);
|
||||
return $this->component->with($id, ['incidents']);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -45,17 +46,6 @@ class ComponentController extends DingoController {
|
||||
* @return Component
|
||||
*/
|
||||
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->component->create($this->auth->user()->id, Input::all());
|
||||
}
|
||||
}
|
||||
|
||||
64
app/CachetHq/Cachet/Controllers/Api/IncidentController.php
Normal file
64
app/CachetHq/Cachet/Controllers/Api/IncidentController.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?php namespace CachetHq\Cachet\Controllers\Api;
|
||||
|
||||
use Input, Incident;
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Dingo\Api\Auth\Shield;
|
||||
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
|
||||
|
||||
class ComponentController extends DingoController {
|
||||
|
||||
protected $auth;
|
||||
|
||||
public function __construct(Shield $auth) {
|
||||
$this->auth = $auth;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all incidents
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getIncidents() {
|
||||
return Incident::all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single incident
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Incident
|
||||
*/
|
||||
public function getIncident($id) {
|
||||
if ($incident = Incident::find($id)) {
|
||||
return $incident;
|
||||
} else {
|
||||
App::abort(404, 'Incident not found');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new incident
|
||||
*
|
||||
* @return Incident
|
||||
*/
|
||||
public function postIncidents() {
|
||||
$incident = new Incident(Input::all());
|
||||
$incident->user_id = $this->auth->user()->id;
|
||||
return $this->_saveIncident($incident);
|
||||
}
|
||||
|
||||
/**
|
||||
* Update an existing incident
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Incident
|
||||
*/
|
||||
public function putIncident($id) {
|
||||
$incident = $this->getIncident($id);
|
||||
|
||||
$incident->fill(Input::all());
|
||||
|
||||
return $this->_saveIncident($incident);
|
||||
}
|
||||
@@ -4,7 +4,9 @@ interface ComponentRepository {
|
||||
|
||||
public function all();
|
||||
|
||||
public function create($id, array $array);
|
||||
|
||||
public function findOrFail($id);
|
||||
|
||||
public function incidents($id);
|
||||
public function with($id, array $with);
|
||||
}
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
<?php namespace CachetHq\Cachet\Repositories\Component;
|
||||
|
||||
use CachetHq\Cachet\Repositories\EloquentRepository;
|
||||
use Component;
|
||||
use Exception;
|
||||
|
||||
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
|
||||
|
||||
|
||||
|
||||
protected $model;
|
||||
|
||||
public function __construct(Component $model) {
|
||||
$this->model = $model;
|
||||
}
|
||||
|
||||
public function create($user_id, array $array) {
|
||||
$component = new $this->model($array);
|
||||
$component->user_id = $user_id;
|
||||
|
||||
if ($component->isInvalid()) {
|
||||
throw new Exception('Invalid model validation', $component->getErrors());
|
||||
}
|
||||
|
||||
$component->saveOrFail();
|
||||
return $component;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,15 +1,26 @@
|
||||
<?php namespace CachetHq\Cachet\Repositories;
|
||||
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Exception;
|
||||
|
||||
abstract class EloquentRepository {
|
||||
|
||||
/**
|
||||
* Creates a model with a given array of items
|
||||
* @param array $array
|
||||
* Returns all models
|
||||
* @return object
|
||||
*/
|
||||
public function create(array $array) {
|
||||
$this->model->create($array);
|
||||
public function all() {
|
||||
return $this->model->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an object with related relationships
|
||||
* @param id $id
|
||||
* @param array $with Array of model relationships
|
||||
* @return object|ModelNotFoundException
|
||||
*/
|
||||
public function with($id, array $with = []) {
|
||||
return $this->model->with($with)->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -32,6 +43,15 @@ abstract class EloquentRepository {
|
||||
return $this->model->find($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a model by ID
|
||||
* @param integer $id
|
||||
* @return object|ModelNotFoundException
|
||||
*/
|
||||
public function findOrFail($id) {
|
||||
return $this->model->findOrFail($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Finds a model by type
|
||||
* @param string $key
|
||||
|
||||
@@ -1,61 +0,0 @@
|
||||
<?php Controllers\Api;
|
||||
|
||||
use Dingo\Api\Routing\Controller as DingoController;
|
||||
use Dingo\Api\Auth\Shield;
|
||||
use Cachet\Repositories\Component\ComponentRepository;
|
||||
|
||||
class ComponentController extends DingoController {
|
||||
|
||||
protected $auth;
|
||||
|
||||
protected $component;
|
||||
|
||||
public function __construct(Shield $auth, ComponentRepository $component) {
|
||||
$this->auth = $auth;
|
||||
$this->component = $component;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all components
|
||||
*
|
||||
* @return \Illuminate\Database\Eloquent\Collection
|
||||
*/
|
||||
public function getComponents() {
|
||||
return $this->component->all();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a single component
|
||||
*
|
||||
* @param int $id
|
||||
*
|
||||
* @return Component
|
||||
*/
|
||||
public function getComponent($id) {
|
||||
return $this->component->findOrFail($id);
|
||||
}
|
||||
|
||||
public function getComponentIncidents($id) {
|
||||
return $this->component->incidents($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new component
|
||||
*
|
||||
* @return Component
|
||||
*/
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'Cachet\Controllers\Api'], function() {
|
||||
Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHq\Cachet\Controllers\Api'], function() {
|
||||
|
||||
Route::get('components', 'ComponentController@getComponents');
|
||||
Route::get('components/{id}', 'ComponentController@getComponent');
|
||||
|
||||
Reference in New Issue
Block a user