Added Component Repository into controller

This commit is contained in:
Elliot Hesp
2014-11-26 15:00:36 +00:00
parent 7c0b80f6a4
commit af10821ef5
8 changed files with 114 additions and 82 deletions

View File

@@ -1,5 +1,6 @@
<?php namespace CachetHq\Cachet\Controllers\Api; <?php namespace CachetHq\Cachet\Controllers\Api;
use Input;
use Dingo\Api\Routing\Controller as DingoController; use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield; use Dingo\Api\Auth\Shield;
use CachetHq\Cachet\Repositories\Component\ComponentRepository; use CachetHq\Cachet\Repositories\Component\ComponentRepository;
@@ -36,7 +37,7 @@ class ComponentController extends DingoController {
} }
public function getComponentIncidents($id) { 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 * @return Component
*/ */
public function postComponents() { public function postComponents() {
$component = new Component(Input::all()); return $this->component->create($this->auth->user()->id, 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());
}
} }
} }

View 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);
}

View File

@@ -4,7 +4,9 @@ interface ComponentRepository {
public function all(); public function all();
public function create($id, array $array);
public function findOrFail($id); public function findOrFail($id);
public function incidents($id); public function with($id, array $with);
} }

View File

@@ -1,9 +1,26 @@
<?php namespace CachetHq\Cachet\Repositories\Component; <?php namespace CachetHq\Cachet\Repositories\Component;
use CachetHq\Cachet\Repositories\EloquentRepository; use CachetHq\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository { 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;
}
} }

View File

@@ -1,15 +1,26 @@
<?php namespace CachetHq\Cachet\Repositories; <?php namespace CachetHq\Cachet\Repositories;
use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
abstract class EloquentRepository { abstract class EloquentRepository {
/** /**
* Creates a model with a given array of items * Returns all models
* @param array $array * @return object
*/ */
public function create(array $array) { public function all() {
$this->model->create($array); 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); 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 * Finds a model by type
* @param string $key * @param string $key

View File

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

View File

@@ -1,6 +1,6 @@
<?php <?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', 'ComponentController@getComponents');
Route::get('components/{id}', 'ComponentController@getComponent'); Route::get('components/{id}', 'ComponentController@getComponent');