This commit is contained in:
manavo
2014-11-26 23:45:30 +00:00
11 changed files with 244 additions and 233 deletions

View File

@@ -1,51 +1,53 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
<?php
use Input;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
use CachetHq\Cachet\Repositories\Component\ComponentRepository;
namespace CachetHQ\Cachet\Controllers\Api;
class ComponentController extends DingoController {
use Input;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
protected $auth;
class ComponentController extends DingoController {
protected $component;
protected $auth;
public function __construct(Shield $auth, ComponentRepository $component) {
$this->auth = $auth;
$this->component = $component;
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->with($id, ['incidents']);
}
/**
* Create a new component
*
* @return \Component
*/
public function postComponents() {
return $this->component->create($this->auth->user()->id, Input::all());
}
}
/**
* 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->with($id, ['incidents']);
}
/**
* Create a new component
*
* @return \Component
*/
public function postComponents() {
return $this->component->create($this->auth->user()->id, Input::all());
}
}

View File

@@ -1,68 +1,70 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
<?php
use Input, Incident;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
namespace CachetHQ\Cachet\Controllers\Api;
class IncidentController extends DingoController {
use Input, Incident;
use Dingo\Api\Routing\Controller as DingoController;
use Dingo\Api\Auth\Shield;
protected $auth;
class IncidentController extends DingoController {
public function __construct(Shield $auth) {
$this->auth = $auth;
}
/**
* Get all incidents
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getIncidents() {
return Incident::all();
}
protected $auth;
/**
* 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');
public function __construct(Shield $auth) {
$this->auth = $auth;
}
}
/**
* 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);
}
/**
* Get all incidents
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function getIncidents() {
return Incident::all();
}
/**
* Update an existing incident
*
* @param int $id
*
* @return Incident
*/
public function putIncident($id) {
$incident = $this->getIncident($id);
/**
* 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');
}
}
$incident->fill(Input::all());
/**
* 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);
}
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);
}
/**
* Function for saving the incident, and returning appropriate error codes
*
* @param Incident $incident
@@ -86,4 +88,4 @@ class IncidentController extends DingoController {
App::abort(404, $incident->getErrors()->first());
}
}
}
}

View File

@@ -1,9 +1,11 @@
<?php namespace CachetHq\Cachet\Controllers\Api;
<?php
use Input, Metric;
use Dingo\Api\Routing\Controller as DingoController;
namespace CachetHQ\Cachet\Controllers\Api;
class MetricController extends DingoController {
use Input, Metric;
use Dingo\Api\Routing\Controller as DingoController;
class MetricController extends DingoController {
/**
* Get all metrics
@@ -71,4 +73,4 @@ class MetricController extends DingoController {
App::abort(404, $metric->getErrors()->first());
}
}
}
}

View File

@@ -1,12 +1,14 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
<?php
interface ComponentRepository {
namespace CachetHQ\Cachet\Repositories\Component;
public function all();
interface ComponentRepository {
public function create($id, array $array);
public function all();
public function findOrFail($id);
public function create($id, array $array);
public function with($id, array $with);
}
public function findOrFail($id);
public function with($id, array $with);
}

View File

@@ -1,26 +1,28 @@
<?php namespace CachetHq\Cachet\Repositories\Component;
<?php
use CachetHq\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
namespace CachetHQ\Cachet\Repositories\Component;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
use CachetHQ\Cachet\Repositories\EloquentRepository;
use Component;
use Exception;
protected $model;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository {
public function __construct(Component $model) {
$this->model = $model;
}
protected $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());
public function __construct(Component $model) {
$this->model = $model;
}
$component->saveOrFail();
return $component;
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,101 +1,103 @@
<?php namespace CachetHq\Cachet\Repositories;
<?php
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
namespace CachetHQ\Cachet\Repositories;
abstract class EloquentRepository {
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Exception;
/**
* Returns all models
* @return object
*/
public function all() {
return $this->model->all();
}
abstract class EloquentRepository {
/**
* 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);
}
/**
* Sets the model to query against a user id
* @param integer $id
* @param string $column
* @return $this
*/
public function withAuth($id, $column = 'user_id') {
$this->model = $this->model->where($column, $id);
return $this;
}
/**
* Finds a model by ID
* @param int $id
* @return object
*/
public function find(int $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
* @param string $key
* @param string $value
* @param array $columns
* @return object|ModelNotFoundException
*/
public function findByOrFail($key, $value, $columns = ['*']) {
if (! is_null($item = $this->model->where($key, $value)->first($columns))) {
return $item;
/**
* Returns all models
* @return object
*/
public function all() {
return $this->model->all();
}
throw new ModelNotFoundException;
}
/**
* Counts the number of rows returned
* @param string $key
* @param string $value
* @return integer
*/
public function count($key = null, $value = null) {
if (is_null($key) || is_null($value)) {
return $this->model->where($key, $value)->count();
/**
* 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);
}
return $this->model->count();
}
/**
* Deletes a model by ID
* @param inetegr $id
*/
public function destroy($id) {
$this->model->delete($id);
}
/**
* Sets the model to query against a user id
* @param integer $id
* @param string $column
* @return $this
*/
public function withAuth($id, $column = 'user_id') {
$this->model = $this->model->where($column, $id);
return $this;
}
/**
* 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);
/**
* Finds a model by ID
* @param int $id
* @return object
*/
public function find(int $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
* @param string $key
* @param string $value
* @param array $columns
* @return object|ModelNotFoundException
*/
public function findByOrFail($key, $value, $columns = ['*']) {
if (! is_null($item = $this->model->where($key, $value)->first($columns))) {
return $item;
}
throw new ModelNotFoundException;
}
/**
* Counts the number of rows returned
* @param string $key
* @param string $value
* @return integer
*/
public function count($key = null, $value = null) {
if (is_null($key) || is_null($value)) {
return $this->model->where($key, $value)->count();
}
return $this->model->count();
}
/**
* Deletes a model by ID
* @param inetegr $id
*/
public function destroy($id) {
$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);
}
}
}

View File

@@ -1,12 +1,11 @@
<?php namespace CachetHq\Cachet\Support\ServiceProviders;
<?php
use Illuminate\Support\ServiceProvider;
namespace CachetHQ\Cachet\Support\ServiceProviders;
class RepositoryServiceProvider extends ServiceProvider {
use Illuminate\Support\ServiceProvider;
public function register()
{
$this->app->bind('CachetHq\Cachet\Repositories\Component\ComponentRepository', 'CachetHq\Cachet\Repositories\Component\EloquentComponentRepository');
class RepositoryServiceProvider extends ServiceProvider {
public function register() {
$this->app->bind('CachetHQ\Cachet\Repositories\Component\ComponentRepository', 'CachetHQ\Cachet\Repositories\Component\EloquentComponentRepository');
}
}
}

View File

@@ -124,7 +124,7 @@ return array(
'Dingo\Api\ApiServiceProvider',
'CachetHq\Cachet\Support\ServiceProviders\RepositoryServiceProvider',
'CachetHQ\Cachet\Support\ServiceProviders\RepositoryServiceProvider',
),

View File

@@ -1,6 +1,6 @@
<?php
Route::api(['version' => 'v1', 'prefix' => 'api', 'namespace' => 'CachetHq\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');