Abstract presenter, timestampstrait, update api responses

This commit is contained in:
Joseph Cohen 2015-05-20 12:57:22 -05:00
parent 10bcbc6169
commit 088b54a5ce
12 changed files with 211 additions and 142 deletions

View File

@ -44,9 +44,9 @@ class Repository
/** /**
* Returns a setting from the database. * Returns a setting from the database.
* *
* @param string $name * @param string $name
* @param string $default * @param string|null $default
* @param bool $checkEnv * @param bool $checkEnv
* *
* @return string|null * @return string|null
*/ */

View File

@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Models\Tag; use CachetHQ\Cachet\Models\Tag;
use CachetHQ\Cachet\Repositories\Component\ComponentRepository; use CachetHQ\Cachet\Repositories\Component\ComponentRepository;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class ComponentController extends AbstractApiController class ComponentController extends AbstractApiController
@ -58,18 +59,20 @@ class ComponentController extends AbstractApiController
*/ */
public function getComponent($id) public function getComponent($id)
{ {
return $this->component->findOrFail($id); return $this->item($this->component->findOrFail($id));
} }
/** /**
* Create a new component. * Create a new component.
* *
* @param \Illuminate\Contracts\Auth\Guard $auth
*
* @return \CachetHQ\Cachet\Models\Component * @return \CachetHQ\Cachet\Models\Component
*/ */
public function postComponents() public function postComponents(Guard $auth)
{ {
$component = $this->component->create( $component = $this->component->create(
$this->auth->user()->id, $auth->user()->id,
Binput::except('tags') Binput::except('tags')
); );
@ -87,7 +90,7 @@ class ComponentController extends AbstractApiController
$component->tags()->sync($componentTags); $component->tags()->sync($componentTags);
} }
return $component; return $this->item($component);
} }
/** /**
@ -114,7 +117,7 @@ class ComponentController extends AbstractApiController
$component->tags()->sync($componentTags); $component->tags()->sync($componentTags);
} }
return $component; return $this->item($component);
} }
/** /**

View File

@ -15,6 +15,7 @@ namespace CachetHQ\Cachet\Http\Controllers\Api;
use CachetHQ\Cachet\Repositories\Incident\IncidentRepository; use CachetHQ\Cachet\Repositories\Incident\IncidentRepository;
use GrahamCampbell\Binput\Facades\Binput; use GrahamCampbell\Binput\Facades\Binput;
use Illuminate\Contracts\Auth\Guard;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class IncidentController extends AbstractApiController class IncidentController extends AbstractApiController
@ -57,17 +58,19 @@ class IncidentController extends AbstractApiController
*/ */
public function getIncident($id) public function getIncident($id)
{ {
return $this->incident->findOrFail($id); return $this->item($this->incident->findOrFail($id));
} }
/** /**
* Create a new incident. * Create a new incident.
* *
* @param \Illuminate\Contracts\Auth\Guard $auth
*
* @return \CachetHQ\Cachet\Models\Incident * @return \CachetHQ\Cachet\Models\Incident
*/ */
public function postIncidents() public function postIncidents(Guard $auth)
{ {
return $this->incident->create($this->auth->user()->id, Binput::all()); return $this->item($this->incident->create($auth->user()->id, Binput::all()));
} }
/** /**
@ -79,7 +82,7 @@ class IncidentController extends AbstractApiController
*/ */
public function putIncident($id) public function putIncident($id)
{ {
return $this->incident->update($id, Binput::all()); return $this->item($this->incident->update($id, Binput::all()));
} }
/** /**

View File

@ -79,7 +79,7 @@ class MetricController extends AbstractApiController
*/ */
public function postMetrics() public function postMetrics()
{ {
return $this->metric->create(Binput::all()); return $this->item($this->metric->create(Binput::all()));
} }
/** /**
@ -91,7 +91,7 @@ class MetricController extends AbstractApiController
*/ */
public function putMetric($id) public function putMetric($id)
{ {
return $this->metric->update($id, Binput::all()); return $this->item($this->metric->update($id, Binput::all()));
} }
/** /**

View File

@ -44,7 +44,7 @@ class MetricPointController extends AbstractApiController
*/ */
public function getMetricPoints($id) public function getMetricPoints($id)
{ {
return $this->metricPoint->findOrFail($id); return $this->item($this->metricPoint->findOrFail($id));
} }
/** /**
@ -56,7 +56,7 @@ class MetricPointController extends AbstractApiController
*/ */
public function postMetricPoints($id) public function postMetricPoints($id)
{ {
return $this->metricPoint->create($id, Binput::all()); return $this->item($this->metricPoint->create($id, Binput::all()));
} }
/** /**
@ -72,7 +72,7 @@ class MetricPointController extends AbstractApiController
$metricPoint = $this->metricPoint->findOrFail($pointId); $metricPoint = $this->metricPoint->findOrFail($pointId);
$metricPoint->update(Binput::all()); $metricPoint->update(Binput::all());
return $metricPoint; return $this->item($metricPoint);
} }
/** /**

View File

@ -16,6 +16,7 @@ namespace CachetHQ\Cachet\Models;
use Illuminate\Database\Eloquent\Builder; use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes; use Illuminate\Database\Eloquent\SoftDeletes;
use McCool\LaravelAutoPresenter\HasPresenter;
use Watson\Validating\ValidatingTrait; use Watson\Validating\ValidatingTrait;
/** /**
@ -31,7 +32,7 @@ use Watson\Validating\ValidatingTrait;
* @property \Carbon\Carbon $updated_at * @property \Carbon\Carbon $updated_at
* @property \Carbon\Carbon $deleted_at * @property \Carbon\Carbon $deleted_at
*/ */
class Component extends Model class Component extends Model implements HasPresenter
{ {
use SoftDeletes, ValidatingTrait; use SoftDeletes, ValidatingTrait;
@ -161,4 +162,14 @@ class Component extends Model
return implode(', ', $tags->toArray()); return implode(', ', $tags->toArray());
} }
/**
* Get the presenter class.
*
* @return string
*/
public function getPresenterClass()
{
return 'CachetHQ\Cachet\Presenters\ComponentPresenter';
}
} }

View File

@ -0,0 +1,40 @@
<?php
/*
* This file is part of Cachet.
*
* (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Presenters;
use CachetHQ\Cachet\Facades\Setting;
use Illuminate\Contracts\Support\Arrayable;
use McCool\LaravelAutoPresenter\BasePresenter as BaseLaravelAutoPresenter;
abstract class AbstractPresenter extends BaseLaravelAutoPresenter implements Arrayable
{
/**
* The setting repository.
*
* @var \CachetHQ\Cachet\Config\Repository
*/
protected $setting;
/**
* Create a incident presenter instance.
*
* @param object $resource
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->setting = app('setting');
}
}

View File

@ -0,0 +1,34 @@
<?php
/*
* This file is part of Cachet.
*
* (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Presenters;
use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
class ComponentPresenter extends AbstractPresenter
{
use TimestampsTrait;
/**
* Convert the presenter instance to an array.
*
* @return string[]
*/
public function toArray()
{
return array_merge($this->wrappedObject->toArray(), [
'created_at' => $this->created_at(),
'updated_at' => $this->updated_at(),
]);
}
}

View File

@ -14,32 +14,13 @@
namespace CachetHQ\Cachet\Presenters; namespace CachetHQ\Cachet\Presenters;
use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Facades\Setting;
use CachetHQ\Cachet\Models\Incident; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
use GrahamCampbell\Markdown\Facades\Markdown; use GrahamCampbell\Markdown\Facades\Markdown;
use Jenssegers\Date\Date; use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\BasePresenter;
class IncidentPresenter extends BasePresenter class IncidentPresenter extends AbstractPresenter
{ {
/** use TimestampsTrait;
* Time zone setting.
*
* @var string
*/
protected $tz;
/**
* Create a incident presenter instance.
*
* @param object $resource
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->tz = Setting::get('app_timezone');
$this->format = Setting::get('incident_date_format') ?: 'l jS F Y H:i:s';
}
/** /**
* Renders the message from Markdown into HTML. * Renders the message from Markdown into HTML.
@ -59,7 +40,7 @@ class IncidentPresenter extends BasePresenter
public function created_at_diff() public function created_at_diff()
{ {
return (new Date($this->wrappedObject->created_at)) return (new Date($this->wrappedObject->created_at))
->setTimezone($this->tz) ->setTimezone($this->setting->get('app_timezone'))
->diffForHumans(); ->diffForHumans();
} }
@ -71,8 +52,8 @@ class IncidentPresenter extends BasePresenter
public function created_at_formatted() public function created_at_formatted()
{ {
return ucfirst((new Date($this->wrappedObject->created_at)) return ucfirst((new Date($this->wrappedObject->created_at))
->setTimezone($this->tz) ->setTimezone($this->setting->get('app_timezone'))
->format($this->format)); ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s')));
} }
/** /**
@ -82,7 +63,7 @@ class IncidentPresenter extends BasePresenter
*/ */
public function created_at_datetimepicker() public function created_at_datetimepicker()
{ {
return $this->wrappedObject->created_at->setTimezone($this->tz)->format('d/m/Y H:i'); return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
} }
/** /**
@ -92,7 +73,18 @@ class IncidentPresenter extends BasePresenter
*/ */
public function created_at_iso() public function created_at_iso()
{ {
return $this->wrappedObject->created_at->setTimezone($this->tz)->toISO8601String(); return $this->wrappedObject->created_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
}
/**
* Present formatted date time.
*
* @return string
*/
public function scheduled_at()
{
return (new Date($this->wrappedObject->scheduled_at))
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
} }
/** /**
@ -103,7 +95,7 @@ class IncidentPresenter extends BasePresenter
public function scheduled_at_diff() public function scheduled_at_diff()
{ {
return (new Date($this->wrappedObject->scheduled_at)) return (new Date($this->wrappedObject->scheduled_at))
->setTimezone($this->tz) ->setTimezone($this->setting->get('app_timezone'))
->diffForHumans(); ->diffForHumans();
} }
@ -115,8 +107,8 @@ class IncidentPresenter extends BasePresenter
public function scheduled_at_formatted() public function scheduled_at_formatted()
{ {
return ucfirst((new Date($this->wrappedObject->scheduled_at)) return ucfirst((new Date($this->wrappedObject->scheduled_at))
->setTimezone($this->tz) ->setTimezone($this->setting->get('app_timezone'))
->format($this->format)); ->format($this->setting->get('incident_date_format', 'l jS F Y H:i:s')));
} }
/** /**
@ -126,7 +118,7 @@ class IncidentPresenter extends BasePresenter
*/ */
public function scheduled_at_iso() public function scheduled_at_iso()
{ {
return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->toISO8601String(); return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->toISO8601String();
} }
/** /**
@ -136,7 +128,7 @@ class IncidentPresenter extends BasePresenter
*/ */
public function scheduled_at_datetimepicker() public function scheduled_at_datetimepicker()
{ {
return $this->wrappedObject->scheduled_at->setTimezone($this->tz)->format('d/m/Y H:i'); return $this->wrappedObject->scheduled_at->setTimezone($this->setting->get('app_timezone'))->format('d/m/Y H:i');
} }
/** /**
@ -161,4 +153,18 @@ class IncidentPresenter extends BasePresenter
return ''; return '';
} }
} }
/**
* Convert the presenter instance to an array.
*
* @return string[]
*/
public function toArray()
{
return array_merge($this->wrappedObject->toArray(), [
'scheduled_at' => $this->created_at(),
'created_at' => $this->created_at(),
'updated_at' => $this->updated_at(),
]);
}
} }

View File

@ -4,6 +4,8 @@
* This file is part of Cachet. * This file is part of Cachet.
* *
* (c) James Brooks <james@cachethq.io> * (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@ -11,58 +13,16 @@
namespace CachetHQ\Cachet\Presenters; namespace CachetHQ\Cachet\Presenters;
use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
use Illuminate\Contracts\Support\Arrayable;
use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\BasePresenter;
class MetricPointPresenter extends BasePresenter implements Arrayable class MetricPointPresenter extends AbstractPresenter
{ {
/** use TimestampsTrait;
* Time zone setting.
*
* @var string
*/
protected $tz;
/**
* Create a incident presenter instance.
*
* @param object $resource
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->tz = Setting::get('app_timezone');
}
/**
* Present formatted date time.
*
* @return string
*/
public function created_at()
{
return (new Date($this->wrappedObject->created_at))
->setTimezone($this->tz)->toDateTimeString();
}
/**
* Present formatted date time.
*
* @return string
*/
public function updated_at()
{
return (new Date($this->wrappedObject->updated_at))
->setTimezone($this->tz)->toDateTimeString();
}
/** /**
* Convert the presenter instance to an array. * Convert the presenter instance to an array.
* *
* @return array * @return string[]
*/ */
public function toArray() public function toArray()
{ {

View File

@ -4,6 +4,8 @@
* This file is part of Cachet. * This file is part of Cachet.
* *
* (c) James Brooks <james@cachethq.io> * (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
* *
* For the full copyright and license information, please view the LICENSE * For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code. * file that was distributed with this source code.
@ -11,58 +13,16 @@
namespace CachetHQ\Cachet\Presenters; namespace CachetHQ\Cachet\Presenters;
use CachetHQ\Cachet\Facades\Setting; use CachetHQ\Cachet\Presenters\Traits\TimestampsTrait;
use Illuminate\Contracts\Support\Arrayable;
use Jenssegers\Date\Date;
use McCool\LaravelAutoPresenter\BasePresenter;
class MetricPresenter extends BasePresenter implements Arrayable class MetricPresenter extends AbstractPresenter
{ {
/** use TimestampsTrait;
* Time zone setting.
*
* @var string
*/
protected $tz;
/**
* Create a incident presenter instance.
*
* @param object $resource
*/
public function __construct($resource)
{
parent::__construct($resource);
$this->tz = Setting::get('app_timezone');
}
/**
* Present formatted date time.
*
* @return string
*/
public function created_at()
{
return (new Date($this->wrappedObject->created_at))
->setTimezone($this->tz)->toDateTimeString();
}
/**
* Present formatted date time.
*
* @return string
*/
public function updated_at()
{
return (new Date($this->wrappedObject->updated_at))
->setTimezone($this->tz)->toDateTimeString();
}
/** /**
* Convert the presenter instance to an array. * Convert the presenter instance to an array.
* *
* @return array * @return string[]
*/ */
public function toArray() public function toArray()
{ {

View File

@ -0,0 +1,52 @@
<?php
/*
* This file is part of Cachet.
*
* (c) James Brooks <james@cachethq.io>
* (c) Joseph Cohen <joseph.cohen@dinkbit.com>
* (c) Graham Campbell <graham@mineuk.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace CachetHQ\Cachet\Presenters\Traits;
use Jenssegers\Date\Date;
trait TimestampsTrait
{
/**
* Present formatted date time.
*
* @return string
*/
public function created_at()
{
return (new Date($this->wrappedObject->created_at))
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
}
/**
* Present formatted date time.
*
* @return string
*/
public function updated_at()
{
return (new Date($this->wrappedObject->updated_at))
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
}
/**
* Present formatted date time.
*
* @return string
*/
public function deleted_at()
{
return (new Date($this->wrappedObject->deleted_at))
->setTimezone($this->setting->get('app_timezone'))->toDateTimeString();
}
}