Merge pull request #660 from cachethq/no-repositories

Remove repositories
This commit is contained in:
James Brooks
2015-06-09 11:04:32 +01:00
24 changed files with 222 additions and 939 deletions

View File

@@ -11,39 +11,26 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Models\Tag; use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Cachet\Repositories\Component\ComponentRepository; use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class ComponentController extends AbstractApiController class ComponentController extends AbstractApiController
{ {
/**
* The component repository instance.
*
* @var \CachetHQ\Cachet\Repositories\Component\ComponentRepository
*/
protected $component;
/**
* Create a new component controller instance.
*
* @param \CachetHQ\Cachet\Repositories\Component\ComponentRepository $component
*/
public function __construct(ComponentRepository $component)
{
$this->component = $component;
}
/** /**
* Get all components. * Get all components.
* *
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public function getComponents(Request $request) public function getComponents(Request $request)
{ {
$components = $this->component->paginate(Binput::get('per_page', 20)); $components = Component::paginate(Binput::get('per_page', 20));
return $this->paginator($components, $request); return $this->paginator($components, $request);
} }
@@ -51,13 +38,13 @@ class ComponentController extends AbstractApiController
/** /**
* Get a single component. * Get a single component.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Component $component
* *
* @return \CachetHQ\Cachet\Models\Component * @return \CachetHQ\Cachet\Models\Component
*/ */
public function getComponent($id) public function getComponent(Component $component)
{ {
return $this->item($this->component->findOrFail($id)); return $this->item($component);
} }
/** /**
@@ -69,10 +56,18 @@ class ComponentController extends AbstractApiController
*/ */
public function postComponents(Guard $auth) public function postComponents(Guard $auth)
{ {
$component = $this->component->create( $componentData = Binput::except('tags');
$auth->user()->id, $componentData['user_id'] = $auth->user()->id;
Binput::except('tags')
); try {
$component = Component::create($componentData);
} catch (Exception $e) {
throw new BadRequestHttpException();
}
if (!$component->isValid()) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) { if (Binput::has('tags')) {
// The component was added successfully, so now let's deal with the tags. // The component was added successfully, so now let's deal with the tags.
@@ -94,13 +89,17 @@ class ComponentController extends AbstractApiController
/** /**
* Update an existing component. * Update an existing component.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Componet $component
* *
* @return \CachetHQ\Cachet\Models\Component * @return \CachetHQ\Cachet\Models\Component
*/ */
public function putComponent($id) public function putComponent(Component $component)
{ {
$component = $this->component->update($id, Binput::except('tags')); $component->update(Binput::except('tags'));
if (!$component->isValid('updating')) {
throw new BadRequestHttpException();
}
if (Binput::has('tags')) { if (Binput::has('tags')) {
$tags = preg_split('/ ?, ?/', Binput::get('tags')); $tags = preg_split('/ ?, ?/', Binput::get('tags'));
@@ -121,13 +120,13 @@ class ComponentController extends AbstractApiController
/** /**
* Delete an existing component. * Delete an existing component.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Component $component
* *
* @return \Dingo\Api\Http\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function deleteComponent($id) public function deleteComponent(Component $component)
{ {
$this->component->destroy($id); $component->delete();
return $this->noContent(); return $this->noContent();
} }

View File

@@ -11,38 +11,26 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository; use CachetHQ\Cachet\Models\Incident;
use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard; use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class IncidentController extends AbstractApiController class IncidentController extends AbstractApiController
{ {
/**
* The incident repository instance.
*
* @var \CachetHQ\Cachet\Repositories\Incident\IncidentRepository
*/
protected $incident;
/**
* Create a new incident controller instance.
*
* @param \CachetHQ\Cachet\Repositories\Incident\IncidentRepository $incident
*/
public function __construct(IncidentRepository $incident)
{
$this->incident = $incident;
}
/** /**
* Get all incidents. * Get all incidents.
* *
* @param \Symfony\Component\HttpFoundation\Request $request
* @param \CachetHQ\Cachet\Models\Incident $incident
*
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public function getIncidents(Request $request) public function getIncidents(Request $request)
{ {
$incidents = $this->incident->paginate(Binput::get('per_page', 20)); $incidents = Incident::paginate(Binput::get('per_page', 20));
return $this->paginator($incidents, $request); return $this->paginator($incidents, $request);
} }
@@ -50,13 +38,13 @@ class IncidentController extends AbstractApiController
/** /**
* Get a single incident. * Get a single incident.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Incident $incident
* *
* @return \CachetHQ\Cachet\Models\Incident * @return \CachetHQ\Cachet\Models\Incident
*/ */
public function getIncident($id) public function getIncident(Incident $incident)
{ {
return $this->item($this->incident->findOrFail($id)); return $this->item($incident);
} }
/** /**
@@ -68,31 +56,50 @@ class IncidentController extends AbstractApiController
*/ */
public function postIncidents(Guard $auth) public function postIncidents(Guard $auth)
{ {
return $this->item($this->incident->create($auth->user()->id, Binput::all())); $incidentData = Binput::all();
$incidentData['user_id'] = $auth->user()->id;
try {
$incident = Incident::create($incidentData);
} catch (Exception $e) {
throw new BadRequestHttpException();
}
if ($incident->isValid()) {
return $this->item($incident);
}
throw new BadRequestHttpException();
} }
/** /**
* Update an existing incident. * Update an existing incident.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Inicdent $incident
* *
* @return \CachetHQ\Cachet\Models\Incident * @return \CachetHQ\Cachet\Models\Incident
*/ */
public function putIncident($id) public function putIncident(Incident $incident)
{ {
return $this->item($this->incident->update($id, Binput::all())); $incident->update(Binput::all());
if ($incident->isValid('updating')) {
return $this->item($incident);
}
throw new BadRequestHttpException();
} }
/** /**
* Delete an existing incident. * Delete an existing incident.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Inicdent $incident
* *
* @return \Dingo\Api\Http\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function deleteIncident($id) public function deleteIncident(Incident $incident)
{ {
$this->incident->destroy($id); $incident->delete();
return $this->noContent(); return $this->noContent();
} }

View File

@@ -11,37 +11,24 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Repositories\Metric\MetricRepository; use CachetHQ\Cachet\Models\Metric;
use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
class MetricController extends AbstractApiController class MetricController extends AbstractApiController
{ {
/**
* The metric repository instance.
*
* @var \CachetHQ\Cachet\Repositories\Metric\MetricRepository
*/
protected $metric;
/**
* Create a new metric controller instance.
*
* @param \CachetHQ\Cachet\Repositories\Metric\MetricRepository $metric
*/
public function __construct(MetricRepository $metric)
{
$this->metric = $metric;
}
/** /**
* Get all metrics. * Get all metrics.
* *
* @param \Symfony\Component\HttpFoundation\Request $request
*
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public function getMetrics(Request $request) public function getMetrics(Request $request)
{ {
$metrics = $this->metric->paginate(Binput::get('per_page', 20)); $metrics = Metric::paginate(Binput::get('per_page', 20));
return $this->paginator($metrics, $request); return $this->paginator($metrics, $request);
} }
@@ -49,25 +36,25 @@ class MetricController extends AbstractApiController
/** /**
* Get a single metric. * Get a single metric.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* *
* @return \CachetHQ\Cachet\Models\Metric * @return \CachetHQ\Cachet\Models\Metric
*/ */
public function getMetric($id) public function getMetric(Metric $metric)
{ {
return $this->item($this->metric->findOrFail($id)); return $this->item($metric);
} }
/** /**
* Get all metric points. * Get all metric points.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* *
* @return \Illuminate\Database\Eloquent\Collection * @return \Illuminate\Database\Eloquent\Collection
*/ */
public function getMetricPoints($id) public function getMetricPoints(Metric $metric)
{ {
return $this->collection($this->metric->points($id)); return $this->collection($metric->points);
} }
/** /**
@@ -77,31 +64,47 @@ class MetricController extends AbstractApiController
*/ */
public function postMetrics() public function postMetrics()
{ {
return $this->item($this->metric->create(Binput::all())); try {
$metric = Metric::create(Binput::all());
} catch (Exception $e) {
throw new BadRequestHttpException();
}
if ($metric->isValid()) {
return $this->item($metric);
}
throw new BadRequestHttpException();
} }
/** /**
* Update an existing metric. * Update an existing metric.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* *
* @return \CachetHQ\Cachet\Models\Metric * @return \CachetHQ\Cachet\Models\Metric
*/ */
public function putMetric($id) public function putMetric(Metric $metric)
{ {
return $this->item($this->metric->update($id, Binput::all())); $metric->update(Binput::all());
if ($metric->isValid('updating')) {
return $this->item($metric);
}
throw new BadRequestHttpException();
} }
/** /**
* Delete an existing metric. * Delete an existing metric.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* *
* @return \Dingo\Api\Http\Response * @return \Illuminate\Http\JsonResponse
*/ */
public function deleteMetric($id) public function deleteMetric(Metric $metric)
{ {
$this->metric->destroy($id); $metric->delete();
return $this->noContent(); return $this->noContent();
} }

View File

@@ -11,63 +11,57 @@
namespace CachetHQ\Cachet\Http\Controllers\Api; namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository; use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Models\MetricPoint;
use Exception;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
class MetricPointController extends AbstractApiController class MetricPointController extends AbstractApiController
{ {
/**
* The metric point repository instance.
*
* @var \CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository
*/
protected $metricPoint;
/**
* Create a new metric point controller instance.
*
* @param \CachetHQ\Cachet\Repositories\MetricPoint\MetricPointRepository $metricPoint
*/
public function __construct(MetricPointRepository $metricPoint)
{
$this->metricPoint = $metricPoint;
}
/** /**
* Get a single metric point. * Get a single metric point.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* @param \CachetHQ\Cachet\Models\MetricPoint $metricPoint
* *
* @return \CachetHQ\Cachet\Models\MetricPoint * @return \CachetHQ\Cachet\Models\MetricPoint
*/ */
public function getMetricPoints($id) public function getMetricPoints(Metric $metric, MetricPoint $metricPoint)
{ {
return $this->item($this->metricPoint->findOrFail($id)); return $this->item($metricPoint);
} }
/** /**
* Create a new metric point. * Create a new metric point.
* *
* @param int $id * @param \CachetHQ\Cachet\Models\Metric $metric
* *
* @return \CachetHQ\Cachet\Models\MetricPoint * @return \CachetHQ\Cachet\Models\MetricPoint
*/ */
public function postMetricPoints($id) public function postMetricPoints(Metric $metric)
{ {
return $this->item($this->metricPoint->create($id, Binput::all())); $metricPointData = Binput::all();
$metricPointData['metric_id'] = $metric->id;
try {
$metricPoint = MetricPoint::create($metricPointData);
} catch (Exception $e) {
throw new BadRequestHttpException();
}
return $this->item($metricPoint);
} }
/** /**
* Updates a metric point. * Updates a metric point.
* *
* @param int $metricId * @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $pointId * @param \CachetHQ\Cachet\Models\MetircPoint $metricPoint
* *
* @return \CachetHQ\Cachet\Models\MetricPoint * @return \CachetHQ\Cachet\Models\MetricPoint
*/ */
public function putMetricPoint($metricId, $pointId) public function putMetricPoint(Metric $metric, MetricPoint $metricPoint)
{ {
$metricPoint = $this->metricPoint->findOrFail($pointId);
$metricPoint->update(Binput::all()); $metricPoint->update(Binput::all());
return $this->item($metricPoint); return $this->item($metricPoint);
@@ -76,14 +70,14 @@ class MetricPointController extends AbstractApiController
/** /**
* Destroys a metric point. * Destroys a metric point.
* *
* @param int $metricId * @param \CachetHQ\Cachet\Models\Metric $metric
* @param int $pointId * @param \CachetHQ\Cachet\Models\MetricPoint $metricPoint
* *
* @return \Dingo\Api\Http\Response * @return \Dingo\Api\Http\Response
*/ */
public function deleteMetricPoint($metricId, $pointId) public function deleteMetricPoint(Metric $metric, MetricPoint $metricPoint)
{ {
$this->metricPoint->destroy($pointId); $metricPoint->delete();
return $this->noContent(); return $this->noContent();
} }

View File

@@ -31,33 +31,33 @@ class ApiRoutes
// Components // Components
$router->get('components', 'ComponentController@getComponents'); $router->get('components', 'ComponentController@getComponents');
$router->get('components/{id}', 'ComponentController@getComponent'); $router->get('components/{component}', 'ComponentController@getComponent');
// Incidents // Incidents
$router->get('incidents', 'IncidentController@getIncidents'); $router->get('incidents', 'IncidentController@getIncidents');
$router->get('incidents/{id}', 'IncidentController@getIncident'); $router->get('incidents/{incident}', 'IncidentController@getIncident');
// Metrics // Metrics
$router->get('metrics', 'MetricController@getMetrics'); $router->get('metrics', 'MetricController@getMetrics');
$router->get('metrics/{id}', 'MetricController@getMetric'); $router->get('metrics/{metric}', 'MetricController@getMetric');
$router->get('metrics/{id}/points', 'MetricController@getMetricPoints'); $router->get('metrics/{metric}/points', 'MetricController@getMetricPoints');
// Api protected // Api protected
$router->group(['middleware' => 'auth.api'], function ($router) { $router->group(['middleware' => 'auth.api'], function ($router) {
$router->post('components', 'ComponentController@postComponents'); $router->post('components', 'ComponentController@postComponents');
$router->post('incidents', 'IncidentController@postIncidents'); $router->post('incidents', 'IncidentController@postIncidents');
$router->post('metrics', 'MetricController@postMetrics'); $router->post('metrics', 'MetricController@postMetrics');
$router->post('metrics/{id}/points', 'MetricPointController@postMetricPoints'); $router->post('metrics/{metric}/points', 'MetricPointController@postMetricPoints');
$router->put('components/{id}', 'ComponentController@putComponent'); $router->put('components/{component}', 'ComponentController@putComponent');
$router->put('incidents/{id}', 'IncidentController@putIncident'); $router->put('incidents/{incident}', 'IncidentController@putIncident');
$router->put('metrics/{id}', 'MetricController@putMetric'); $router->put('metrics/{metric}', 'MetricController@putMetric');
$router->put('metrics/{id}/points/{metric_id}', 'MetricPointController@putMetricPoint'); $router->put('metrics/{metric}/points/{metric_point}', 'MetricPointController@putMetricPoint');
$router->delete('components/{id}', 'ComponentController@deleteComponent'); $router->delete('components/{component}', 'ComponentController@deleteComponent');
$router->delete('incidents/{id}', 'IncidentController@deleteIncident'); $router->delete('incidents/{incident}', 'IncidentController@deleteIncident');
$router->delete('metrics/{id}', 'MetricController@deleteMetric'); $router->delete('metrics/{metric}', 'MetricController@deleteMetric');
$router->delete('metrics/{id}/points/{metric_id}', 'MetricPointController@deleteMetricPoint'); $router->delete('metrics/{metric}/points/{metric_point}', 'MetricPointController@deleteMetricPoint');
}); });
}); });
} }

View File

@@ -41,7 +41,7 @@ class Component extends Model implements HasPresenter
*/ */
protected $rules = [ protected $rules = [
'user_id' => 'integer|required', 'user_id' => 'integer|required',
'name' => 'required', 'name' => 'required|string',
'status' => 'integer|required', 'status' => 'integer|required',
'link' => 'url', 'link' => 'url',
]; ];

View File

@@ -1,65 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Component;
interface ComponentRepository
{
/**
* Returns all models.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all();
/**
* Create a new model.
*
* @param int $userId
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($userId, array $data);
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data);
/**
* Finds a model by id.
*
* @param int $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrFail($id);
/**
* Returns an object with related relationships.
*
* @param int $id
* @param string[] $with
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function with($id, array $with = []);
}

View File

@@ -1,74 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Component;
use CachetHQ\Cachet\Models\Component;
use CachetHQ\Cachet\Repositories\EloquentRepository;
class EloquentComponentRepository extends EloquentRepository implements ComponentRepository
{
/**
* The eloquent model instance.
*
* @var \CachetHQ\Cachet\Models\Component
*/
protected $model;
/**
* Create a new eloquent component repository instance.
*
* @param \CachetHQ\Cachet\Models\Component $model
*/
public function __construct(Component $model)
{
$this->model = $model;
}
/**
* Create a new model.
*
* @param int $userId
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($userId, array $data)
{
$component = new $this->model($data);
$component->user_id = $userId;
$this->validate($component);
$component->saveOrFail();
return $component;
}
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data)
{
$component = $this->model->findOrFail($id);
$component->fill($data);
$this->validate($component);
$component->update($data);
return $component;
}
}

View File

@@ -1,182 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\ModelNotFoundException;
abstract class EloquentRepository
{
/**
* Returns all models.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all()
{
return $this->model->all();
}
/**
* Returns paginated result.
*
* @param int $perPage
*
* @return \Illuminate\Pagination\Paginator
*/
public function paginate($perPage = 20)
{
return $this->model->paginate($perPage);
}
/**
* Returns an object with related relationships.
*
* @param int $id
* @param string[] $with
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function with($id, array $with = [])
{
return $this->model->with($with)->findOrFail($id);
}
/**
* Sets the model to query against a user id.
*
* @param int $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 \Illuminate\Database\Eloquent\Model
*/
public function find($id)
{
return $this->model->find($id);
}
/**
* Finds a model by id.
*
* @param int $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrFail($id)
{
return $this->model->findOrFail($id);
}
/**
* Finds a model by type.
*
* @param string $key
* @param string $value
* @param array $columns
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findByOrFail($key, $value, $columns = ['*'])
{
$model = $this->model->where($key, $value)->first($columns);
if ($model === null) {
throw new ModelNotFoundException();
}
return $model;
}
/**
* Counts the number of rows returned.
*
* @param string|null $key
* @param string|null $value
*
* @return int
*/
public function count($key = null, $value = null)
{
if ($key === null || $value === null) {
return $this->model->count();
}
return $this->model->where($key, $value)->count();
}
/**
* Deletes a model by id.
*
* @param int $id
*/
public function destroy($id)
{
$this->model->destroy($id);
}
/**
* Validate a given model with Watson validation.
*
* @param \Illuminate\Database\Eloquent\Model $model
*
* @throws \CachetHQ\Cachet\Repositories\InvalidModelValidationException
*
* @return $this
*/
public function validate(Model $model)
{
if ($model->isInvalid()) {
throw new InvalidModelValidationException('Validation failed on the model.');
}
return $this;
}
/**
* Validate whether a model has a correct relationship.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @param string $relationship
*
* @throws \CachetHQ\Cachet\Repositories\InvalidRelationshipException
*
* @return $this
*/
public function hasRelationship(Model $model, $relationship)
{
if ($model->$relationship === null) {
throw new InvalidRelationshipException('The relationship was not valid.');
}
return $this;
}
}

View File

@@ -1,81 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Incident;
use CachetHQ\Cachet\Models\Incident;
use CachetHQ\Cachet\Repositories\EloquentRepository;
class EloquentIncidentRepository extends EloquentRepository implements IncidentRepository
{
/**
* The eloquent model instance.
*
* @var \CachetHQ\Cachet\Models\Incident
*/
protected $model;
/**
* Create a new eloquent incident repository instance.
*
* @param \CachetHQ\Cachet\Models\Incident $model
*/
public function __construct(Incident $model)
{
$this->model = $model;
}
/**
* Create a new model.
*
* @param int $userId
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($userId, array $data)
{
$incident = new $this->model($data);
$incident->user_id = $userId;
$this->validate($incident);
if (isset($data['component_id'])) {
$this->hasRelationship($incident, 'component');
}
$incident->saveOrFail();
return $incident;
}
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data)
{
$incident = $this->model->findOrFail($id);
$incident->fill($data);
$this->validate($incident);
if (isset($data['component_id'])) {
$this->hasRelationship($incident, 'component');
}
$incident->update($data);
return $incident;
}
}

View File

@@ -1,62 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Incident;
interface IncidentRepository
{
/**
* Returns all models.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all();
/**
* Returns paginated result.
*
* @param int $perPage
*
* @return \Illuminate\Pagination\Paginator
*/
public function paginate($perPage = 20);
/**
* Create a new model.
*
* @param int $userId
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($userId, array $data);
/**
* Finds a model by id.
*
* @param int $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrFail($id);
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data);
}

View File

@@ -1,19 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories;
use Exception;
class InvalidModelValidationException extends Exception
{
//
}

View File

@@ -1,19 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories;
use Exception;
class InvalidRelationshipException extends Exception
{
//
}

View File

@@ -1,87 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Metric;
use CachetHQ\Cachet\Models\Metric;
use CachetHQ\Cachet\Repositories\EloquentRepository;
class EloquentMetricRepository extends EloquentRepository implements MetricRepository
{
/**
* The eloquent model instance.
*
* @var \CachetHQ\Cachet\Models\Metric
*/
protected $model;
/**
* Create a new eloquent metric repository instance.
*
* @param \CachetHQ\Cachet\Models\Metric $model
*/
public function __construct(Metric $model)
{
$this->model = $model;
}
/**
* Create a new model.
*
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $data)
{
$metric = new $this->model($data);
$this->validate($metric);
$metric->saveOrFail();
return $metric;
}
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data)
{
$metric = $this->model->findOrFail($id);
$metric->fill($data);
$this->validate($metric);
$metric->update($data);
return $metric;
}
/**
* Returns all metric point models.
*
* @param int $id
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function points($id)
{
$metric = $this->model->findOrFail($id);
return $metric->points;
}
}

View File

@@ -1,61 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\Metric;
interface MetricRepository
{
/**
* Returns all models.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all();
/**
* Returns all metric point models.
*
* @param int $id
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function points($id);
/**
* Create a new model.
*
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create(array $data);
/**
* Finds a model by id.
*
* @param int $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrFail($id);
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data);
}

View File

@@ -1,75 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\MetricPoint;
use CachetHQ\Cachet\Models\MetricPoint;
use CachetHQ\Cachet\Repositories\EloquentRepository;
class EloquentMetricPointRepository extends EloquentRepository implements MetricPointRepository
{
/**
* The eloquent model instance.
*
* @var \CachetHQ\Cachet\Models\MetricPoint
*/
protected $model;
/**
* Create a new eloquent metric point repository instance.
*
* @param \CachetHQ\Cachet\Models\MetricPoint $model
*/
public function __construct(MetricPoint $model)
{
$this->model = $model;
}
/**
* Create a new model.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($id, array $data)
{
$data['metric_id'] = $id;
$metric = new $this->model($data);
$this->validate($metric);
$metric->saveOrFail();
return $metric;
}
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data)
{
$metric = $this->model->findOrFail($id);
$metric->fill($data);
$this->validate($metric);
$metric->update($data);
return $metric;
}
}

View File

@@ -1,53 +0,0 @@
<?php
/*
* This file is part of Cachet.
*
* (c) Cachet HQ <support@cachethq.io>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Repositories\MetricPoint;
interface MetricPointRepository
{
/**
* Returns all models.
*
* @return \Illuminate\Database\Eloquent\Collection
*/
public function all();
/**
* Create a new model.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function create($id, array $data);
/**
* Finds a model by id.
*
* @param int $id
*
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function findOrFail($id);
/**
* Update a model by id.
*
* @param int $id
* @param array $data
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function update($id, array $data);
}

8
composer.lock generated
View File

@@ -1567,12 +1567,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/laravel/framework.git", "url": "https://github.com/laravel/framework.git",
"reference": "3f3307c911f2876447ce61d8c3aea33a99389441" "reference": "6a785aeeeff0739e67b79b015c337fe834133a48"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/laravel/framework/zipball/3f3307c911f2876447ce61d8c3aea33a99389441", "url": "https://api.github.com/repos/laravel/framework/zipball/6a785aeeeff0739e67b79b015c337fe834133a48",
"reference": "3f3307c911f2876447ce61d8c3aea33a99389441", "reference": "6a785aeeeff0739e67b79b015c337fe834133a48",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@@ -1687,7 +1687,7 @@
"framework", "framework",
"laravel" "laravel"
], ],
"time": "2015-06-07 02:48:12" "time": "2015-06-07 20:48:54"
}, },
{ {
"name": "league/commonmark", "name": "league/commonmark",

View File

@@ -89,7 +89,7 @@ return [
'key' => env('APP_KEY', 'SomeRandomString'), 'key' => env('APP_KEY', 'SomeRandomString'),
'cipher' => 'AES-256-CBC', 'cipher' => 'rijndael-256',
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
@@ -170,7 +170,6 @@ return [
'CachetHQ\Cachet\Providers\ConfigServiceProvider', 'CachetHQ\Cachet\Providers\ConfigServiceProvider',
'CachetHQ\Cachet\Providers\ConsoleServiceProvider', 'CachetHQ\Cachet\Providers\ConsoleServiceProvider',
'CachetHQ\Cachet\Providers\EventServiceProvider', 'CachetHQ\Cachet\Providers\EventServiceProvider',
'CachetHQ\Cachet\Providers\RepositoryServiceProvider',
'CachetHQ\Cachet\Providers\RouteServiceProvider', 'CachetHQ\Cachet\Providers\RouteServiceProvider',
'CachetHQ\Cachet\Providers\SegmentServiceProvider', 'CachetHQ\Cachet\Providers\SegmentServiceProvider',
'CachetHQ\Segment\SegmentServiceProvider', 'CachetHQ\Segment\SegmentServiceProvider',

21
public/build/dist/css/all-c8eb2347.css vendored Normal file

File diff suppressed because one or more lines are too long

View File

@@ -20,8 +20,12 @@ class ComponentTest extends AbstractTestCase
public function testGetComponents() public function testGetComponents()
{ {
$components = factory('CachetHQ\Cachet\Models\Component', 3)->create();
$this->get('/api/v1/components'); $this->get('/api/v1/components');
$this->seeJson(['data' => []]); $this->seeJson(['id' => (string) $components[0]->id]);
$this->seeJson(['id' => (string) $components[1]->id]);
$this->seeJson(['id' => (string) $components[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }

View File

@@ -20,14 +20,18 @@ class IncidentTest extends AbstractTestCase
public function testGetIncidents() public function testGetIncidents()
{ {
$incidents = factory('CachetHQ\Cachet\Models\Incident', 3)->create();
$this->get('/api/v1/incidents'); $this->get('/api/v1/incidents');
$this->seeJson(['data' => []]); $this->seeJson(['id' => (string) $incidents[0]->id]);
$this->seeJson(['id' => (string) $incidents[1]->id]);
$this->seeJson(['id' => (string) $incidents[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }
public function testGetInvalidIncident() public function testGetInvalidIncident()
{ {
$this->get('/api/v1/incidents/1'); $this->get('/api/v1/incidents/0');
$this->assertResponseStatus(404); $this->assertResponseStatus(404);
} }

View File

@@ -18,10 +18,26 @@ class MetricPointTest extends AbstractTestCase
{ {
use DatabaseMigrations; use DatabaseMigrations;
public function testGetMetricPoint()
{
$metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint', 3)->create([
'metric_id' => $metric->id,
]);
$this->get("/api/v1/metrics/{$metric->id}/points");
$this->seeJson(['id' => (string) $metricPoint[0]->id]);
$this->seeJson(['id' => (string) $metricPoint[1]->id]);
$this->seeJson(['id' => (string) $metricPoint[2]->id]);
}
public function testPostMetricPointUnauthorized() public function testPostMetricPointUnauthorized()
{ {
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create(); $metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$this->post('/api/v1/metrics/1/points'); $metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
$this->post("/api/v1/metrics/{$metric->id}/points");
$this->assertResponseStatus(401); $this->assertResponseStatus(401);
$this->seeJson(['message' => 'You are not authorized to view this content.', 'status_code' => 401]); $this->seeJson(['message' => 'You are not authorized to view this content.', 'status_code' => 401]);
@@ -30,17 +46,24 @@ class MetricPointTest extends AbstractTestCase
public function testPostMetricPoint() public function testPostMetricPoint()
{ {
$this->beUser(); $this->beUser();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create();
$this->post('/api/v1/metrics/1/points', $metricPoint->toArray()); $metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->make([
'metric_id' => $metric->id,
]);
$this->post("/api/v1/metrics/{$metric->id}/points", $metricPoint->toArray());
$this->seeJson(['value' => (string) $metricPoint->value]); $this->seeJson(['value' => (string) $metricPoint->value]);
} }
public function testPutMetricPoint() public function testPutMetricPoint()
{ {
$this->beUser(); $this->beUser();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create(); $metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$this->put('/api/v1/metrics/1/points/1', [ $metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
$this->put("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}", [
'value' => 999, 'value' => 999,
]); ]);
$this->seeJson(['value' => '999']); $this->seeJson(['value' => '999']);
@@ -49,8 +72,11 @@ class MetricPointTest extends AbstractTestCase
public function testDeleteMetricPoint() public function testDeleteMetricPoint()
{ {
$this->beUser(); $this->beUser();
$metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create(); $metric = factory('CachetHQ\Cachet\Models\Metric')->create();
$this->delete('/api/v1/metrics/1/points/1'); $metricPoint = factory('CachetHQ\Cachet\Models\MetricPoint')->create([
'metric_id' => $metric->id,
]);
$this->delete("/api/v1/metrics/{$metric->id}/points/{$metricPoint->id}");
$this->assertResponseStatus(204); $this->assertResponseStatus(204);
} }
} }

View File

@@ -20,14 +20,18 @@ class MetricTest extends AbstractTestCase
public function testGetMetrics() public function testGetMetrics()
{ {
$metrics = factory('CachetHQ\Cachet\Models\Metric', 3)->create();
$this->get('/api/v1/metrics'); $this->get('/api/v1/metrics');
$this->seeJson(['data' => []]); $this->seeJson(['id' => (string) $metrics[0]->id]);
$this->seeJson(['id' => (string) $metrics[1]->id]);
$this->seeJson(['id' => (string) $metrics[2]->id]);
$this->assertResponseOk(); $this->assertResponseOk();
} }
public function testGetInvalidMetric() public function testGetInvalidMetric()
{ {
$this->get('/api/v1/metrics/1'); $this->get('/api/v1/metrics/0');
$this->assertResponseStatus(404); $this->assertResponseStatus(404);
} }